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
/
scripts.py
executable file
·103 lines (78 loc) · 3.04 KB
/
scripts.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import json
from facilities.models import Facility
from common.models import County, Ward
def fix_date_established():
bad_date = Facility.objects.all()[1].date_established
for fac in Facility.objects.filter(date_established=bad_date):
fac.date_established = None
fac.save(allow_save=True)
def fix_town_name():
for fac in Facility.objects.all():
if fac.town:
fac.town_name = fac.town.name
try:
fac.save(allow_save=True)
except:
print "unable to update town"
# from xlrd import open_workbook
# def read_facilities_with_wards_file(file_path=None, write_file=None):
# if not file_path:
# file_path = 'facitlities_with_wards.xlsx'
# wb = open_workbook(file_path)
# values = []
# append = values.append
# for sheet in wb.sheets():
# n_of_rows = sheet.nrows
# for row in xrange(1, n_of_rows):
# data = {
# "facility_code": sheet.cell(row, 0).value,
# "county": sheet.cell(row, 1).value,
# "ward": sheet.cell(row, 2).value
# }
# append(data)
# if not write_file:
# write_file = 'faciltiy_with_wards_json.json'
# with open(write_file, 'w+') as data_file:
# json.dump(values, data_file, indent=4)
def move_facitlies_to_correct_ward(file_path=None):
if not file_path:
file_path = 'misplaced_facilities.json'
with open(file_path) as data_file:
data = json.loads(data_file.read())
for record in data:
try:
fac = Facility.objects.get(code=int(record.get('facility_code')))
except Facility.DoesNotExist:
print "facility not found"
continue
if record.get('county'):
county_name = record.get('county').split()[0]
if county_name == 'Elgeyo':
county_name = 'Elegeyo'
try:
county = County.objects.filter(name__icontains=county_name)[0]
except IndexError:
;
prob_ward = Ward.objects.filter(constituency__county=county)
try:
ward = prob_ward.filter(name__icontains=record.get('ward'))[0]
except IndexError:
fac.ward = ward
print county, ward
try:
fac.save(allow_save=True)
except:
def determine_facilites_already_in_system():
file_path = 'new_missing_facilities.json'
with open(file_path) as data_file:
data = json.loads(data_file.read())
codes = []
for record in data:
try:
Facility.objects.get(code=int(record.get('facility_code')))
except Facility.DoesNotExist:
codes.append(record.get('facility_code'))
print "facility not found"
continue
with open('missing_but_in_system', 'w+') as data_file:
json.dump(codes, data_file, indent=4)