Refactor code to clean it up a bit.
This commit is contained in:
parent
ef77c8faed
commit
c28e26d5cc
3 changed files with 87 additions and 87 deletions
|
@ -1,4 +1,4 @@
|
|||
from django.core.management.base import BaseCommand, CommandError
|
||||
from django.core.management.base import BaseCommand
|
||||
|
||||
from events.models.locale import Country, SPR, City
|
||||
|
||||
|
@ -26,6 +26,7 @@ MODIFICATION_DATE=18
|
|||
COUNTRY_CACHE = dict()
|
||||
SPR_CACHE = dict()
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = 'Loads city data from GeoNames database file'
|
||||
|
||||
|
@ -39,7 +40,7 @@ class Command(BaseCommand):
|
|||
COUNTRY_CACHE[country.code] = country
|
||||
for spr in SPR.objects.all():
|
||||
SPR_CACHE["%s.%s" % (spr.country.code, spr.code)] = spr
|
||||
cities_file = open(options['file'], 'r')
|
||||
with open(options['file'], 'r') as cities_file:
|
||||
for city_line in cities_file.readlines():
|
||||
city = city_line.split("\t")
|
||||
if len(city) == 19:
|
||||
|
@ -48,12 +49,14 @@ class Command(BaseCommand):
|
|||
spr = SPR_CACHE.get("%s.%s" % (city[COUNTRY_CODE], city[ADMIN1]))
|
||||
if country is not None and spr is not None:
|
||||
try:
|
||||
City.objects.update_or_create(name=city[NAME], spr=spr, defaults={'tz':city[TIMEZONE], 'longitude':city[LONGITUDE], 'latitude':city[LATITUDE]})
|
||||
except:
|
||||
print("Warning: Failed to load city %s for %s" % (city[NAME], spr))
|
||||
City.objects.update_or_create(
|
||||
name=city[NAME],
|
||||
spr=spr,
|
||||
defaults={'tz': city[TIMEZONE], 'longitude': city[LONGITUDE], 'latitude': city[LATITUDE]})
|
||||
except Exception as e:
|
||||
print("Warning: Failed to load city %s for %s (%s)" % (city[NAME], spr, e))
|
||||
else:
|
||||
print("Short line (%s): %s" % (len(city), city_line))
|
||||
cities_file.close()
|
||||
|
||||
else:
|
||||
print("No File in options!")
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
from django.core.management.base import BaseCommand, CommandError
|
||||
from django.core.management.base import BaseCommand
|
||||
|
||||
from events.models.locale import Country
|
||||
|
||||
|
@ -23,6 +23,7 @@ GEONAMEID=16
|
|||
NEIGHBOURS = 17
|
||||
EQUIVALENTFIPSCODE = 18
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = 'Loads country data from GeoNames database file'
|
||||
|
||||
|
@ -31,7 +32,7 @@ class Command(BaseCommand):
|
|||
|
||||
def handle(self, *args, **options):
|
||||
if 'file' in options:
|
||||
countries_file = open(options['file'], 'r')
|
||||
with open(options['file'], 'r') as countries_file:
|
||||
for country_line in countries_file.readlines():
|
||||
if country_line.startswith("#"):
|
||||
continue
|
||||
|
@ -41,7 +42,5 @@ class Command(BaseCommand):
|
|||
Country.objects.get_or_create(name=country[COUNTRY], code=country[ISO])
|
||||
else:
|
||||
print("Short line (%s): %s" % (len(country), country_line))
|
||||
countries_file.close()
|
||||
|
||||
else:
|
||||
print("No File in options!")
|
||||
|
|
|
@ -22,7 +22,7 @@ class Command(BaseCommand):
|
|||
# Preload country cache
|
||||
for country in Country.objects.all():
|
||||
COUNTRY_CACHE[country.code] = country
|
||||
spr_file = open(options['file'], 'r')
|
||||
with open(options['file'], 'r') as spr_file:
|
||||
for spr_line in spr_file.readlines():
|
||||
if spr_line.startswith("#"):
|
||||
continue
|
||||
|
@ -35,7 +35,5 @@ class Command(BaseCommand):
|
|||
SPR.objects.get_or_create(name=spr[NAME], code=SPR_CODE, country=country)
|
||||
else:
|
||||
print("Short line (%s): %s" % (len(spr), spr_line))
|
||||
spr_file.close()
|
||||
|
||||
else:
|
||||
print("No File in options!")
|
||||
|
|
Loading…
Reference in a new issue