# Path: catalog/addresses/models.py
# class Region(models.Model):
#     code = models.SlugField(
#         _('Code'), max_length=128, unique=True, db_index=True)
#     name = models.CharField(_('Name'), max_length=255)
#     active = models.BooleanField(
#         _('Active'), default=True, help_text=scs.ACTIVE_FIELD_HELP_TEXT)
# 
#     class Meta:
#         db_table = 'catalog_addresses_regions'
#         verbose_name = _('Region')
#         verbose_name_plural = _('Regions')
#         ordering = ('name', )
# 
#     def __str__(self):
#         return '{}'.format(self.name)
# 
# class Country(models.Model):
#     region = models.ForeignKey(
#         Region, related_name='countries', verbose_name=_('Region'),
#         help_text=_('Select a region for this country. This is mostly used to '
#                     'define shipping rates per region.'))
# 
#     code = models.SlugField(
#         _('Code'), max_length=2, unique=True, db_index=True,
#         help_text=_('A 2 letter country code.'))
# 
#     name = models.CharField(_('Name'), max_length=255)
#     active = models.BooleanField(
#         _('Active'), default=True, help_text=scs.ACTIVE_FIELD_HELP_TEXT)
# 
#     def __str__(self):
#         return '{}'.format(self.name)
# 
#     class Meta:
#         db_table = 'catalog_addresses_countries'
#         verbose_name = _('Country')
#         verbose_name_plural = _('Countries')
#         ordering = ('name', )


import json
from urllib2 import urlopen
from django.utils.encoding import smart_str
from django.core.management.base import CommandError, BaseCommand
from catalog.addresses.models import Region, Country
    data = None

    def __init__(self, *args, **kwargs):
        super(Command, self).__init__(*args, **kwargs)

    def handle(self, *args, **options):
        print 'Querying database at {}.'.format(COUNTRIES_API_URL)

        api = urlopen(COUNTRIES_API_URL)
        data = json.loads(api.read()).get('geonames', None)
        if data is not None:
            self.data = dict((x['countryCode'], x) for x in data)
        else:
            raise CommandError('Error fetching %s.' % COUNTRIES_API_URL)

        codes = list(args) if len(args) else self.data.keys()
        codes = [x for x in codes if x in self.data]

        created_countries = []
        created_regions = []

        for code in codes:
            country = self.data.get(code)

            try:
                region = Region.objects.get(code=country['continent'])
            except Region.DoesNotExist:
                print 'Creating region: \'{}\' ({}).'.\
                    format(country['continent'],
                           self.get_unicode(country['continentName']))

# Path: catalog/addresses/models.py
# class Region(models.Model):
#     code = models.SlugField(
#         _('Code'), max_length=128, unique=True, db_index=True)
#     name = models.CharField(_('Name'), max_length=255)
#     active = models.BooleanField(
#         _('Active'), default=True, help_text=scs.ACTIVE_FIELD_HELP_TEXT)
# 
#     class Meta:
#         db_table = 'catalog_addresses_regions'
#         verbose_name = _('Region')
#         verbose_name_plural = _('Regions')
#         ordering = ('name', )
# 
#     def __str__(self):
#         return '{}'.format(self.name)
# 
# class Country(models.Model):
#     region = models.ForeignKey(
#         Region, related_name='countries', verbose_name=_('Region'),
#         help_text=_('Select a region for this country. This is mostly used to '
#                     'define shipping rates per region.'))
# 
#     code = models.SlugField(
#         _('Code'), max_length=2, unique=True, db_index=True,
#         help_text=_('A 2 letter country code.'))
# 
#     name = models.CharField(_('Name'), max_length=255)
#     active = models.BooleanField(
#         _('Active'), default=True, help_text=scs.ACTIVE_FIELD_HELP_TEXT)
# 
#     def __str__(self):
#         return '{}'.format(self.name)
# 
#     class Meta:
#         db_table = 'catalog_addresses_countries'
#         verbose_name = _('Country')
#         verbose_name_plural = _('Countries')
#         ordering = ('name', )


# Path: catalog/addresses/management/commands/initcountries.py
import json
from urllib2 import urlopen
from django.utils.encoding import smart_str
from django.core.management.base import CommandError, BaseCommand
from catalog.addresses.models import Region, Country

    data = None

    def __init__(self, *args, **kwargs):
        super(Command, self).__init__(*args, **kwargs)

    def handle(self, *args, **options):
        print 'Querying database at {}.'.format(COUNTRIES_API_URL)

        api = urlopen(COUNTRIES_API_URL)
        data = json.loads(api.read()).get('geonames', None)
        if data is not None:
            self.data = dict((x['countryCode'], x) for x in data)
        else:
            raise CommandError('Error fetching %s.' % COUNTRIES_API_URL)

        codes = list(args) if len(args) else self.data.keys()
        codes = [x for x in codes if x in self.data]

        created_countries = []
        created_regions = []

        for code in codes:
            country = self.data.get(code)

            try:
                region = Region.objects.get(code=country['continent'])
            except Region.DoesNotExist:
                print 'Creating region: \'{}\' ({}).'.\
                    format(country['continent'],
                           self.get_unicode(country['continentName']))

                region = Region.objects.create(code=country['continent'],