You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
62 lines
2.4 KiB
Python
62 lines
2.4 KiB
Python
6 years ago
|
from django.core.management.base import BaseCommand, CommandError
|
||
|
from factbook.models import Country
|
||
|
import json
|
||
|
|
||
|
class Command(BaseCommand):
|
||
|
help = 'Closes the specified poll for voting'
|
||
|
|
||
|
def add_arguments(self, parser):
|
||
|
parser.add_argument('json', nargs="+")
|
||
|
|
||
|
def handle(self, *args, **options):
|
||
|
for x in options["json"]:
|
||
|
try:
|
||
|
print("Opening {0}".format(x), file=self.stdout)
|
||
|
with open(x) as f:
|
||
|
data = json.load(f)
|
||
|
if 'Introduction' in data:
|
||
|
description = data['Introduction']['Background']['text']
|
||
|
else:
|
||
|
description = ""
|
||
|
|
||
|
cname = data['Government'].get("Country name", {})
|
||
|
|
||
|
if cname and 'conventional short form' in cname:
|
||
|
country_name = cname["conventional short form"]["text"]
|
||
|
elif cname and 'Dutch short form' in cname:
|
||
|
country_name = cname["Dutch short form"]["text"]
|
||
|
else:
|
||
|
print("skipping {0}".format(x), file=self.stderr)
|
||
|
continue
|
||
|
|
||
|
if 'conventional short form' in cname:
|
||
|
country_name_long = cname["conventional short form"]["text"]
|
||
|
|
||
|
c, created = Country.objects.get_or_create(name = country_name)
|
||
|
|
||
|
c.name = country_name
|
||
|
c.name_longform = country_name_long
|
||
|
c.description = description
|
||
|
|
||
|
coordinates_text = data["Geography"]["Geographic coordinates"]["text"]
|
||
|
c.coordinates = coordinates_text
|
||
|
latitude_text, longitude_text = coordinates_text.split(", ")
|
||
|
c.longitude_degrees,c.longitude_minutes,c.longitude_direction = longitude_text.split()
|
||
|
c.latitude_degrees,c.latitude_minutes,c.latitude_direction = latitude_text.split()
|
||
|
|
||
|
c.save()
|
||
|
except Exception as e:
|
||
|
print('skipping file {0} because of {1}'.format(x,e), file=self.stderr)
|
||
|
|
||
|
|
||
|
# for poll_id in options['poll_id']:
|
||
|
# try:
|
||
|
# poll = Poll.objects.get(pk=poll_id)
|
||
|
# except Poll.DoesNotExist:
|
||
|
# raise CommandError('Poll "%s" does not exist' % poll_id)
|
||
|
#
|
||
|
# poll.opened = False
|
||
|
# poll.save()
|
||
|
#
|
||
|
# self.stdout.write(self.style.SUCCESS('Successfully closed poll "%s"' % poll_id))
|