# coding=utf-8
# create by oldman at 2017/10/12
import json

from django.core.management import BaseCommand

from hippo.models import FinanceCity
from hippo.models.finance import FinanceProvince


class Command(BaseCommand):
    """
    添加对账单的省市
    python manage.py update_2017_10_12_add_financecity
    """

    def handle(self, *args, **options):
        with open('province_city.json') as json_file:
            data = json.load(json_file)
            print data
            for key, values in data.items():
                province, _ = FinanceProvince.objects.get_or_create(
                    name=key
                )
                for value in values:
                    city = FinanceCity.objects.get_or_create(
                        name=value,
                        province_id=province.id
                    )

            print ('Done')
