You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

oscar_populate_countries.py 1.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. # -*- coding: utf-8 -*-
  2. from optparse import make_option
  3. from django.core.management.base import BaseCommand, CommandError
  4. from oscar.core.loading import get_model
  5. Country = get_model('address', 'Country')
  6. class Command(BaseCommand):
  7. help = "Populates the list of countries with data from pycountry."
  8. # TODO: Allow setting locale to fetch country names in right locale
  9. # https://code.djangoproject.com/ticket/6376
  10. option_list = BaseCommand.option_list + (
  11. make_option(
  12. '--no-shipping',
  13. action='store_false',
  14. dest='is_shipping',
  15. default=True,
  16. help="Don't mark countries for shipping"),
  17. )
  18. def handle(self, *args, **options):
  19. try:
  20. import pycountry
  21. except ImportError:
  22. raise CommandError(
  23. "You are missing the pycountry library. Install it with "
  24. "'pip install pycountry'")
  25. if Country.objects.exists():
  26. raise CommandError(
  27. "You already have countries in your database. This command"
  28. "currently does not support updating existing countries.")
  29. countries = [
  30. Country(
  31. iso_3166_1_a2=country.alpha2,
  32. iso_3166_1_a3=country.alpha3,
  33. iso_3166_1_numeric=country.numeric,
  34. printable_name=country.name,
  35. name=getattr(country, 'official_name', ''),
  36. is_shipping_country=options['is_shipping'])
  37. for country in pycountry.countries]
  38. Country.objects.bulk_create(countries)
  39. self.stdout.write("Successfully added %s countries." % len(countries))