forked from ertush/mfl_api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
load_all_facilities.py
executable file
·309 lines (241 loc) · 10.5 KB
/
load_all_facilities.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
import json
import pytz
from dateutil import parser
from django.utils import timezone
from facilities.models import Facility, Owner, RegulatingBody, FacilityType, FacilityStatus, KephLevel
from users.models import MflUser
from common.models import County, Ward
user = MflUser.objects.get(email='[email protected]')
def load_missed_facilities():
fac_wards = open('faciltiy_with_wards_json.json')
fac_wards_data = fac_wards.read()
facility_wards_data = json.loads(fac_wards_data)
facs_created = []
file_path = 'missed_codes_facility_records.json'
with open(file_path) as data_file:
data = data_file.read()
data = json.loads(data)
for record in data:
try:
Facility.objects.get(code=int(record.get('code')))
continue
except Facility.DoesNotExist:
pass
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:
print( county, record.get('county'))
reg_body = None
if record.get('regulatory_body'):
try:
reg_body = RegulatingBody.objects.get(name=record.get('regulatory_body').get('name'))
except RegulatingBody.DoesNotExist:
pass
facility_type = FacilityType.objects.get(name=record.get('facility_type').get('name'))
try:
owner = Owner.objects.get(name=record.get('owner').get('name'))
except Owner.DoesNotExist:
moh = Owner.objects.get(name='Ministry of Health')
owner = moh
try:
operation_status = FacilityStatus.objects.get(name=record.get('operation_status').get('name'))
except AttributeError:
facility_data = {
"code": int(record.get('code')),
"name": record.get('name'),
"created": pytz.utc.localize(parser.parse(record.get('created'))),
"owner": owner,
"facility_type": facility_type,
"operation_status": operation_status,
"county": county,
"created_by": user,
"updated_by": user
}
if reg_body:
facility_data['regulatory_body'] = reg_body
if record.get('official_name') != '':
facility_data['official_name'] = record.get('official_name')
if record.get('keph_level'):
try:
keph = KephLevel.objects.get(name=record.get('keph_level').get('name'))
facility_data['keph_level'] = keph
except KephLevel.DoesNotExist:
pass
for fac_ward in facility_wards_data:
if int(facility_data.get('code')) == int(fac_ward.get('faciilty_code')):
prob_ward = Ward.objects.filter(constituency__county=county)
try:
ward = prob_ward.filter(name__icontains=fac_ward.get('ward'))[0]
except:
facility_data['ward'] = ward
try:
Facility.objects.get(code=int(record.get('code')))
print( "facility exists")
except Facility.DoesNotExist:
try:
facility= Facility(**facility_data).save()
facs_created.append(facility)
print (facility)
except:
pass
# ;
break
return len(facs_created)
# # try:
# # Facility(**facility_data).save()
# # except:
# # ;
# try:
# facility = Facility.objects.get(code=record.get('code'))
# facility.county = county
# facility.save(allow_save=True)
# except Facility.DoesNotExist:
# pass
# def assign_psuedo_wards():
# fac_wards = open('faciltiy_with_wards_json.json')
# fac_wards_data = fac_wards.read()
# facility_data = json.loads(fac_wards_data)
# for facility in Facility.objects.exclude(county__isnull=True):
# for fac_ward in facility_data:
# if int(facility.code) == int(fac_ward.get('faciilty_code')):
# if facility.county:
# prob_ward = Ward.objects.filter(constituency__county=facility.county)
# ward = prob_ward.filter(name__icontains=fac_ward.get('ward'))[0]
# facility.ward = ward
# facility.save(allow_save=True)
def get_facilities_not_loaded_report():
fac_wards = open('faciltiy_with_wards_json.json')
fac_wards_data = fac_wards.read()
error_facilities = []
facility_wards_data = json.loads(fac_wards_data)
for fac_ward in facility_wards_data:
try:
Facility.objects.get(code=int(fac_ward.get('faciilty_code')))
except Facility.DoesNotExist:
error_facilities.append(fac_ward)
with open('facilities_not_loaded.json', 'w+') as data_file:
json.dump(error_facilities, data_file, indent=4)
return len(error_facilities)
from facilities.models import Facility, FacilityType
def map_facility_types():
facility_types_map = {
"Stand Alone": None,
"VCT Centre (Stand-Alone)": "VCT",
"Training Institution in Health (Stand-alone)": None,
"Sub-District Hospital": "Primary care hospitals",
"Rural Health Training Centre": "Comprehensive primary health care facility",
"Rural Health Demonstration Centre": "Comprehensive primary health care facility",
"Regional Blood Transfusion Centre": "Regional Blood Transfusion Centre",
"Radiology Unit": "Radiology Clinic",
"County Referral Hospitals": "Comprehensive Teaching &Referral",
"Other Hospital": "Primary care hospitals",
"Other": None,
"Nursing Home": "Basic primary health care facility",
"Not in List": "Basic primary health care facility",
"National Teaching & Tertiary Referral Hospitals": "Comprehensive Teaching &Referral",
"Medical Clinic": "Dispensaries and clinic-out patient only",
"Medical Centre": "Dispensaries and clinic-out patient only",
"Maternity Home": "Basic primary health care facility",
"Maternity and Nursing Home": "Basic primary health care facility",
"Laboratory (Stand-alone)": "Laboratory",
"Hospital": None,
"Funeral Home (Stand-alone)": "Farewell Home",
"Eye Clinic": "Dispensaries and clinic-out patient only",
"Eye Centre": "Basic primary health care facility",
"Eye": "Basic primary health care facility",
"District Hospital": "Secondary care hospitals",
"Dispensary": "Dispensaries and clinic-out patient only",
"Dental Clinic": "Dispensaries and clinic-out patient only",
"Blood Bank": "Blood Bank",
"Health Centre": "Basic primary health care facility",
"Not In List": "Dispensaries and Clinics",
"Health Project": "Administrative Offices",
"Health Programme": "Administrative Offices",
"Training Institution in Health (Stand-alone)": "Administrative Offices",
"District Health Office": "Administrative Offices"
}
for facility in Facility.objects.all():
new_type = facility_types_map.get(facility.facility_type.name)
if new_type:
try:
facility_type_obj = FacilityType.objects.get(name=new_type)
except FacilityType.DoesNotExist:
try:
facility.save(allow_save=True)
except:
print("not ok")
"""
Health Centre
Eye Clinic ==> Moved to eye
Eye Centre ==> Moved to eye
Not In List
Training Institution in Health (Stand-alone)
Health Project
Health Programme
[f.facility_type.name for f in Facility.objects.all() if f.facility_type.name not in facility_types_map.values()]
Ensure all stand alone have been created and assigned to the Stand Alone Parent
"""
# def update all facility types with parents with parent FK
def update_facility_types_with_parents():
for f_type in FacilityType.objects.all():
if f_type.sub_division:
parent_obj = FacilityType.objects.get(name=f_type.sub_division)
f_type.parent = parent_obj
f_type.save()
"""
Issues
Eye
Blood bank
Health Centre
Health clinic
"""
"""
0
:
37.64954663579162
1
:
-0.446732937212727
"""
def remove_invalid_and_unknown_status():
from facilities.models import Facility, FacilityStatus
closed = FacilityStatus.objects.get(name='Closed')
for facility in Facility.objects.filter(operation_status__name__in=['Invalid', 'Unknown']):
facility.operation_status = closed
facility.save(allow_save=True)
print (facility)
def undelete_facility_types():
from facilities.models import FacilityType
for facility_type in FacilityType.everything.filter(deleted=True):
facility_type.deleted = False
try:
facility_type.save()
except:
raise
def delete_facility_types():
from facilities.models import FacilityType, Facility
for facility_type in FacilityType.objects.all():
if Facility.objects.filter(facility_type=facility_type).count() == 0:
try:
facility_type.delete()
except:
pass
def remove_invalid_and_unknown_status():
from facilities.models import Facility, FacilityStatus
from django.utils import timezone
closed = FacilityStatus.objects.get(name='Closed')
for facility in Facility.objects.filter(closed=True):
facility.operation_status = closed
facility.save(allow_save=True)
print( facility)
def fix_closed_approved_facilities():
from facilities.models import Facility, FacilityStatus
closed_facilities = Facility.objects.filter(closed=False, has_edits=False, approved=False, rejected=True)
# for fac in closed_facilities:
# fac.approved = True
# fac.has_edits = False
# fac.save(allow_save=True)
return [fac.name for fac in closed_facilities]