This repository has been archived by the owner on Jul 8, 2024. It is now read-only.
forked from SteveWaweru/mfl_api
-
Notifications
You must be signed in to change notification settings - Fork 1
/
dump_wards_and_sub_counties.py
executable file
·68 lines (51 loc) · 1.88 KB
/
dump_wards_and_sub_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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import json
from common.models import Ward, SubCounty, County
from users.models import MflUser
sub_counties = []
ward_sub_counties = []
sub_counties_file = "/tmp/sub_counties.json"
ward_sub_counties_file = "/tmp/ward_sub_counties.json"
sys_user = MflUser.objects.get(email="[email protected]")
def dump_wards():
for ward in Ward.objects.all():
record = {}
record['code'] = ward.code
if ward.sub_county:
record['sub_county'] = ward.sub_county.name
ward_sub_counties.append(record)
with open(ward_sub_counties_file, 'w+') as wards_file:
json.dump(ward_sub_counties, wards_file, indent=4)
def dump_sub_counties():
for sub_county in SubCounty.objects.all():
sub_counties.append(
{
"name": sub_county.name,
"county_code": sub_county.county.code
}
)
with open(sub_counties_file, 'w+') as scs_file:
json.dump(sub_counties, scs_file, indent=4)
def load_wards():
with open("ward_sub_counties.json") as wards_file:
data = json.load(wards_file)
for rec in data:
if rec.get('sub_county'):
ward = Ward.objects.get(code=rec.get("code"))
sub_county = SubCounty.objects.get(
name=rec.get('sub_county'))
ward.sub_county = sub_county
ward.save()
def load_sub_counties():
with open("sub_counties.json") as scs_file:
data = json.load(scs_file)
for rec in data:
county = County.objects.get(
code=rec.get('county_code'))
try:
SubCounty.objects.get(name=rec.get("name"))
except:
SubCounty.objects.create(
name=rec.get('name'),
county=county, created_by=sys_user, updated_by=sys_user)
load_sub_counties()
load_wards()