added addgeo command, and locations.json
This commit is contained in:
53
core/management/commands/addgeo.py
Normal file
53
core/management/commands/addgeo.py
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
import logging
|
||||||
|
|
||||||
|
from django.core.management.base import BaseCommand
|
||||||
|
from geo.models import City, Region, Province, Country
|
||||||
|
import json
|
||||||
|
|
||||||
|
class Command(BaseCommand):
|
||||||
|
|
||||||
|
def handle(self, *args, **kwargs):
|
||||||
|
|
||||||
|
logging.info('Deleting all instances of Country, Region, Province, City')
|
||||||
|
City.objecs.all().delete()
|
||||||
|
Province.objecs.all().delete()
|
||||||
|
Region.objecs.all().delete()
|
||||||
|
Country.objecs.all().delete()
|
||||||
|
|
||||||
|
# create country for spain
|
||||||
|
country = Country.objects.create(name='España')
|
||||||
|
|
||||||
|
path = 'locations.json'
|
||||||
|
locations = json.loads(open(path).read())
|
||||||
|
|
||||||
|
region_counter = 0
|
||||||
|
# Regions loop
|
||||||
|
for location in locations:
|
||||||
|
if location['model'] == 'locations.region':
|
||||||
|
logging.info(f"Creating Region Object {location['fields']['name']}...")
|
||||||
|
name = location['fields']['name']
|
||||||
|
Region.objects.create(name=name, country=country, id=location['pk'])
|
||||||
|
region_counter += 1
|
||||||
|
|
||||||
|
province_counter = 0
|
||||||
|
# Provinces loop
|
||||||
|
for location in locations:
|
||||||
|
if location['model'] == 'locations.province':
|
||||||
|
logging.info(f"Creating Province Object {location['fields']['name']}...")
|
||||||
|
name = location['fields']['name']
|
||||||
|
Province.objects.create(name=name, region=Region.objects.get(id=location['fields']['region']), id=location['pk'])
|
||||||
|
province_counter += 1
|
||||||
|
|
||||||
|
city_counter = 0
|
||||||
|
# Cities loop
|
||||||
|
print('Creating cities...')
|
||||||
|
for location in locations:
|
||||||
|
if location['model'] == 'locations.city':
|
||||||
|
name = location['fields']['name']
|
||||||
|
City.objects.create(name=name, province=Province.objects.get(id=location['fields']['province']), id=location['pk'])
|
||||||
|
city_counter += 1
|
||||||
|
|
||||||
|
logging.info(f"Region instances created: {region_counter}")
|
||||||
|
logging.info(f"Province instances created: {province_counter}")
|
||||||
|
logging.info(f"City instances created: {city_counter}")
|
||||||
|
|
||||||
73738
locations.json
Normal file
73738
locations.json
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user