-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathload_counties.py
46 lines (38 loc) · 1.42 KB
/
load_counties.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import json
import os
import django
from typing import Dict, List
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'main.settings')
django.setup()
from common.models import County, Locality
def sanitize(value: str) -> str:
return value.lower() \
.replace(' ', '-') \
.replace('ă', 'a') \
.replace('â', 'a') \
.replace('î', 'i') \
.replace('ş', 's') \
.replace('ţ', 't')
def main():
try:
with open('judete.json', 'r') as file:
print('Running counties loader...')
counties: List[Dict] = json.load(file)['judete']
for county in counties:
county_name: str = county['nume']
county_slug = sanitize(county_name)
county_obj = County.objects.get_or_create(slug=county_slug, name=county_name)[0]
localities: List[Dict] = county['localitati']
for locality in localities:
locality_name: str = locality['nume']
locality_slug = sanitize(locality_name)
locality_obj = \
Locality.objects.get_or_create(slug=locality_slug, name=locality_name, county=county_obj)[0]
print('Done!')
except KeyError as err:
print('You file is not properly configured!')
print(err)
except Exception as err:
print(f'Error occurred: {err}')
if __name__ == '__main__':
main()