from django.core.management.base import BaseCommand
from content.models import Contact
import re
import subprocess


class Command(BaseCommand):

    def handle(self, **kwargs):

        a = subprocess.check_output('rig', shell=True)

        m = re.match(r"(?P<first_name>\w+) (?P<last_name>\w+)[\n\r](?P<address>\w+(.*))[\n\r](?P<city>\w(.*))[\n\r](?P<phone>[\(]\d+[\)]\s+\w+(.*))", a)
        firstname = m.group('first_name')
        lastname = m.group('last_name')
        address = m.group('address')
        city = m.group('city')
        phone = m.group('phone')

        contact = Contact(firstname=firstname)
        contact.lastname = lastname
        contact.address = address
        contact.city = city
        contact.phone = phone
        contact.save()

        print 'Content successfully added'