forked from revence27/rapidsms-nova
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
765 lines (630 loc) · 28.9 KB
/
utils.py
File metadata and controls
765 lines (630 loc) · 28.9 KB
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
#!/usr/bin/env python
# vim: ai ts=4 sts=4 et sw=4
import unicodecsv as csv
import xlwt
from datetime import date, timedelta
from django.contrib.auth.decorators import permission_required
from django.http import HttpResponse
from django.shortcuts import get_object_or_404
from rapidsmsrw1000.apps.chws.models import *
from rapidsmsrw1000.apps.ubuzima.models import *
from rapidsmsrw1000.apps.enum import *
import calendar
import json
from django.utils.safestring import SafeString
from dateutil import rrule
from random import randint
#wbk = xlwt.Workbook()
#sheet.write(0,0,"ReportID")
#sheet.write(0,1,"Date")
#sheet.write(0,2,"Location")
#sheet.write(0,3,"District")
#sheet.write(0,4,"Type")
#sheet.write(0,5,"Reporter")
#sheet.write(0,6,"Patient")
#sheet.write(0,7,"Message")
#row = 1
#for r in reports:
# sheet.write(row,0,r.pk)
# sheet.write(row,1,"%d.%d.%d" % (r.created.day,r.created.month,r.created.year))
# sheet.write(row,2,r.location.name)
# sheet.write(row,3,r.location.parent.parent.name)
# sheet.write(row,4,r.type.name)
# sheet.write(row,5,r.reporter.alias)
# sheet.write(row,6,r.patient.national_id)
# sheet.write(row,7,r.summary())
# row = row+1
#wbk.save('xlwt.xls')####This allows the workbook to be saved on the disk
def random_with_N_digits(n):
range_start = 10**(n-1)
range_end = (10**n)-1
return randint(range_start, range_end)
def months_between(start,end):
months = []
cursor = start
while cursor <= end:
m="%d-%d"%(cursor.month,cursor.year)
if m not in months:
months.append(m)
cursor += timedelta(weeks=1)
return months
def days_between(start,end):
days = []
cursor = start
for dt in rrule.rrule(rrule.DAILY, dtstart = start, until = end):
days.append(dt.date())
return days
def months_enum():
months=Enum('Months',JAN = 1, FEB = 2, MAR = 3, APR = 4, MAY = 5, JUN = 6, JUL = 7, AUG = 8, SEP = 9, OCT = 10, NOV = 11, DEC = 12)
return months
def create_workbook():
return xlwt.Workbook()
def create_worksheet( workbook, name = "Export"):
return workbook.add_sheet(name)
def create_heads(sheet, heads):
"""sheet = xlwt sheet object ... eg sheet = xlwt.Workbook().add_sheet("Reports"),
heads = a list of headers ... eg : heads = ['ReportID','Date','Location','District','Type','Reporter','Patient','Message'] """
hl = len(heads)
row, col = 0, 0
for h in heads:
if col == hl:
break
else:
sheet.write(row, col, heads[col])
col = col + 1
return sheet
def create_content(sheet, row, content_list):
"""sheet = xlwt sheet object ... eg sheet = xlwt.Workbook().add_sheet("Reports"),
heads = a list of headers ... eg : content_list = [r.id, "%d.%d.%d" % (r.created.day,r.created.month,r.created.year),r.location.name,r.location.parent.parent.name,r.type.name,r.reporter.alias,r.patient.national_id, r.summary()] """
cl = len(content_list)
col = 0
for item in content_list:
if col == cl:
break
else:
sheet.write(row, col, content_list[col])
col = col + 1
return sheet
def heading_report(report):
heads = ['ReportID','Date','Facility', 'District', 'Province','Type','Reporter','Patient', 'LMP', 'DOB', 'VisitDate',' ANCVisit','NBCVisit','PNCVisit','MotherWeight','MotherHeight','ChildWeight','ChildHeight','MUAC', 'ChilNumber','Gender', 'Gravidity','Parity', 'VaccinationReceived' , 'VaccinationCompletion','Breastfeeding', 'Intevention', 'Status','Toilet','Handwash' , 'Located','Symptoms']
dob = lmp = visit = anc = nbc = pnc = mother_w = child_w = mother_h = child_h = muac = chino = gender = gr = pr = vr = vc = bf = interv = st = toi = hw = loc = sym = ""
try:
mother_wf = report.fields.filter(type__key = 'mother_weight')
for s in mother_wf:
mother_w = mother_w.join("%d" % s.value)
except: pass
try:
mother_hf = report.fields.filter(type__key = 'mother_height')
for s in mother_hf:
mother_h = mother_h.join("%d" % s.value)
except: pass
try:
child_wf = report.fields.filter(type__key = 'child_weight')
for s in child_wf:
child_w = child_w.join("%d" % s.value)
except: pass
try:
child_hf = report.fields.filter(type__key = 'child_height')
for s in child_hf:
child_h = child_h.join("%d" % s.value)
except: pass
try:
ancf = report.fields.filter(type__key__in = ['anc2','anc3','anc4'])
for s in ancf:
anc = anc.join("%s" % s.type.description)
except: pass
try:
pncf = report.fields.filter(type__key__in = ['pnc1','pnc2','pnc3'])
for s in pncf:
pnc = pnc.join("%s" % s.type.description)
except: pass
try:
nbcf = report.fields.filter(type__key__in = ['nbc1','nbc2','nbc3','nbc4','nbc5'])
for s in nbcf:
nbc = nbc.join("%s" % s.type.description)
except: pass
try:
muacf = report.fields.filter(type__key = 'muac')
for s in muacf:
muac = muac.join("%d" % s.value)
except: pass
try:
chinof = report.fields.filter(type__key = 'child_number')
for s in chinof:
chino = chino.join("%d" % s.value)
except: pass
try:
genderf = report.fields.filter(type__key__in = ['gi','bo'])
for s in genderf:
gender = gender.join("%s" % s.type.description)
except: pass
try:
grf = report.fields.filter(type__key = 'gravity')
for s in grf:
gr = gr.join("%d" % s.value)
except: pass
try:
prf = report.fields.filter(type__key = 'parity')
for s in prf:
pr = pr.join("%d" % s.value)
except: pass
try:
vrf = report.fields.filter(type__key__in = ['v1','v2','v3','v4','v5','v6'])
for s in vrf:
vr = vr.join("%s" % s.type.description)
except: pass
try:
vcf = report.fields.filter(type__key__in = ['vc', 'vi', 'nv'])
for s in vcf:
vc = vc.join("%s" % s.type.description)
except: pass
try:
bff = report.fields.filter(type__key__in = ['ebf','cbf','nb'])
for s in bff:
bf = bf.join("%s" % s.type.description)
except: pass
try:
intervf = report.fields.filter(type__category__name = 'Intervention Codes')
for s in intervf:
interv = interv.join("%s" % s.type.description)
except: pass
try:
locf = report.fields.filter(type__category__name = 'Location Codes')
for s in locf:
loc = loc.join("%s" % s.type.description)
except: pass
try:
stf = report.fields.filter(type__category__name = 'Results Codes')
for s in stf:
st = st+s.type.description+", "
except: pass
try:
hwf = report.fields.filter(type__key__in = ['hw','nh'])
for s in hwf:
hw = hw.join("%s" % s.type.description)
except: pass
try:
toif = report.fields.filter(type__key__in = ['to', 'nt'])
for s in toif:
toi = toi.join("%s" % s.type.description)
except: pass
try:
symf = report.fields.filter(type__category__name__in = ['Risk Codes' , 'Red Alert Codes'])
for s in symf:
sym = sym+s.type.description+", "
except: pass
print 'id:%s'%report.id, 'date:%s'%read_date(report.created), 'fac:%s'%report.location.name, 'dist:%s'%report.district.name, 'prv:%s'%report.province.name, 'rty:%s'%report.type.name , 'rnid:%s'%report.reporter.telephone_moh, 'pnid:%s'%report.patient.national_id , 'lmp:%s'%lmp, 'dob:%s'%dob, 'visit:%s'%visit, 'anc:%s'%anc, 'nbc:%s'%nbc, 'pnc:%s'%pnc, 'm_w:%s'%mother_w, 'm_h:%s'%mother_h, 'c_w:%s'%child_w, 'c_w:%s'%child_h, 'muac:%s'%muac, 'chino:%s'%chino, 'gender:%s'%gender, 'gr:%s'%gr, 'pr:%s'%pr, 'vr:%s'%vr, 'vc:%s'%vc, 'bf:%s'%bf, 'interv:%s'%interv, 'st:%s'%st, 'toi:%s'%toi, 'hw:%s'%hw, 'loc:%s'%loc, 'sym:%s'%sym
if report.type.name == 'Birth': dob = read_date(report.date)
elif report.type.name == 'Pregnancy': lmp = read_date(report.date)
elif report.type.name == 'ANC': visit = read_date(report.date)
else: dob = read_date(report.date)
content = [report.id, read_date(report.created), report.location.name, report.district.name, report.province.name, report.type.name , report.reporter.national_id, report.patient.national_id , lmp, dob, visit, anc,nbc, pnc, mother_w, mother_h, child_w, child_h, muac, chino, gender, gr, pr, vr, vc, bf, interv, st, toi, hw, loc, sym ]
return {'heads' : heads, 'content' : content}
def reports_to_excel(reports):
maquis = heading_report(reports[1])
workbook = create_workbook()
sheet = create_worksheet(workbook, "reports")
sheet = create_heads(sheet, maquis['heads'])
row = 1
for report in reports:
sheet = create_content(sheet, row, heading_report(report)['content'])
row = row + 1
response = HttpResponse(mimetype = "application/ms-excel")
response['Content-Disposition'] = 'attachment; filename = reports.xls'
workbook.save(response)
return response
def read_date(date):
try: return "%d/%d/%d" % (date.day, date.month, date.year)
except: return ""
def matching_reports(req, diced, alllocs = False):
rez = {}
pst = {}
level = get_level(req)
try:
rez['created__gte'] = diced['period']['start']
rez['created__lte'] = diced['period']['end']+timedelta(1)
except KeyError:
pass
try:
loc = int(req.REQUEST['location'])
rez['location__id'] = loc
except KeyError:
try:
dst=int(req.REQUEST['district'])
rez['district__id'] = dst
except KeyError:
try:
dst=int(req.REQUEST['province'])
rez['province__id'] = dst
except KeyError: pass
if level['level'] == 'Nation': pst['nation__id'] = level['uloc'].nation.id
elif level['level'] == 'Province': pst['province__id'] = level['uloc'].province.id
elif level['level'] == 'District': pst['district__id'] = level['uloc'].district.id
elif level['level'] == 'HealthCentre': pst['location__id'] = level['uloc'].health_centre.id
if rez:
ans = Report.objects.filter(**rez).order_by("-created")
else:
ans = Report.objects.all().order_by("-created")
if pst:
ans = ans.filter(**pst).order_by("-created")
return ans
def location_name(req):
ans = []
try:
uloc = get_user_location(req)
if uloc.health_centre:
prv = uloc.health_centre.province
ans.append(prv.name + ' Province')
dst = uloc.health_centre.district
ans.append(dst.name + ' District')
loc = uloc.health_centre
ans.append(loc.name)
elif uloc.district:
prv = uloc.district.province
ans.append(prv.name + ' Province')
dst = uloc.district
ans.append(dst.name + ' District')
loc = HealthCentre.objects.get(id = int(req.REQUEST['loc']))
ans.append(loc.name)
elif uloc.province:
prv = uloc.province
ans.append(prv.name + ' Province')
dst = District.objects.get(id = int(req.REQUEST['district']))
ans.append(dst.name + ' District')
loc = HealthCentre.objects.get(id = int(req.REQUEST['loc']))
ans.append(loc.name)
else:
prv = Province.objects.get(id = int(req.REQUEST['province']))
ans.append(prv.name + ' Province')
dst = District.objects.get(id = int(req.REQUEST['district']))
ans.append(dst.name + ' District')
loc = HealthCentre.objects.get(id = int(req.REQUEST['loc']))
ans.append(loc.name)
except KeyError, DoesNotExist:
pass
ans.reverse()
return ', '.join(ans)
def cut_date(str):
stt = [int(x) for x in str.split('.')]
stt.reverse()
return date(* stt)
def default_period(req):
if req.REQUEST.has_key('start_date') and req.REQUEST.has_key('end_date'):
return {'start':cut_date(req.REQUEST['start_date']),
'end':cut_date(req.REQUEST['end_date'])}
#return {'start':date.today()-timedelta(days = datetime.datetime.today().day - 1), 'end':date.today() + timedelta(hours = 23,seconds = 59)}#In production
return {'start':date.today()-timedelta(days = 1), 'end':date.today() + timedelta(hours = 23,seconds = 59)}#In production if fast need
#return {'start':date.today() - timedelta(date.today().day), 'end':date.today()}#locally
def get_level(req):
try:
uloc = req.user.hcuser.all()[0]
level = ""
if uloc.nation:
level = "Nation"
elif uloc.province:
level = "Province"
elif uloc.district:
level = "District"
elif uloc.health_centre:
level = "HealthCentre"
return {'level': level, 'uloc': uloc}
except:
return None
def reporter_fresher(req):
pst = {}
try:
level = get_level(req)
if level['level'] == 'Nation':
pst['nation__id'] = level['uloc'].nation.id
elif level['level'] == 'Province':
pst['province__id'] = level['uloc'].province.id
elif level['level'] == 'District':
pst['district__id'] = level['uloc'].district.id
elif level['level'] == 'HealthCentre':
pst['health_centre__id'] = level['uloc'].health_centre.id
except:
pass
return pst
def default_location(req):
try:
uloc = get_user_location(req)
hcs = None
if uloc.nation:
sel = int(req.REQUEST['location']) if req.REQUEST.has_key('location') else 1
hcs = HealthCentre.objects.filter(nation = uloc.nation).extra(select = {'selected':'id = %d' % (sel,)}).order_by('name')
elif uloc.province:
sel = int(req.REQUEST['location']) if req.REQUEST.has_key('location') else 1
hcs = HealthCentre.objects.filter(province = uloc.province).extra(select = {'selected':'id = %d' % (sel,)}).order_by('name')
elif uloc.district:
sel = int(req.REQUEST['location']) if req.REQUEST.has_key('location') else 1
hcs = HealthCentre.objects.filter(district = uloc.district).extra(select = {'selected':'id = %d' % (sel,)}).order_by('name')
elif uloc.health_centre:
sel = int(req.REQUEST['location']) if req.REQUEST.has_key('location') else uloc.health_centre.id
hcs = HealthCentre.objects.filter(pk = uloc.health_centre.id).extra(select = {'selected':'id = %d' % (sel,)}).order_by('name')
if req.REQUEST.has_key('province'): hcs = hcs.filter(province__id = int(req.REQUEST['province']))
if req.REQUEST.has_key('district'): hcs = hcs.filter(district__id = int(req.REQUEST['district']))
if req.REQUEST.has_key('location'): hcs = hcs.filter(id = int(req.REQUEST['location']))
return hcs
except UserLocation.DoesNotExist, e:
return []
def default_province(req):
uloc = get_user_location(req)
prvs = None
try:
if uloc:
if uloc.nation:
sel = int(req.REQUEST['province']) if req.REQUEST.has_key('province') else 0
prvs = Province.objects.all().extra(select = {'selected':'id = %d' % (sel,)}).order_by('name')
elif uloc.province:
sel = int(req.REQUEST['province']) if req.REQUEST.has_key('province') else uloc.province.id
prvs = Province.objects.filter( pk = uloc.province.id ).extra(select = {'selected':'id = %d' % (sel,)}).order_by('name')
elif uloc.district:
sel = int(req.REQUEST['province']) if req.REQUEST.has_key('province') else uloc.district.province.id
prvs = Province.objects.filter( pk = uloc.district.province.id ).extra(select = {'selected':'id = %d' % (sel,)}).order_by('name')
elif uloc.health_centre:
sel = int(req.REQUEST['province']) if req.REQUEST.has_key('province') else uloc.health_centre.province.id
prvs = Province.objects.filter( pk = uloc.health_centre.province.id ).extra(select = {'selected':'id = %d' % (sel,)}).order_by('name')
if req.REQUEST.has_key('province'): prvs = prvs.filter(id = int(req.REQUEST['province']))
return prvs.exclude(name = 'TEST')
else:
return []
except Exception, e:
return []
def default_district(req):
uloc = get_user_location(req)
dsts = None
try:
if uloc:
if uloc.nation:
sel = int(req.REQUEST['district']) if req.REQUEST.has_key('district') else 1
dsts = District.objects.all().extra(select = {'selected':'id = %d' % (sel,)}).order_by('name')
elif uloc.province:
sel = int(req.REQUEST['district']) if req.REQUEST.has_key('district') else 1
dsts = District.objects.filter( province = uloc.province ).extra(select = {'selected':'id = %d' % (sel,)}).order_by('name')
elif uloc.district:
sel = int(req.REQUEST['district']) if req.REQUEST.has_key('district') else uloc.district.id
dsts = District.objects.filter( pk = uloc.district.id ).extra(select = {'selected':'id = %d' % (sel,)}).order_by('name')
elif uloc.health_centre:
sel = int(req.REQUEST['district']) if req.REQUEST.has_key('district') else uloc.health_centre.district.id
dsts = District.objects.filter( pk = uloc.health_centre.district.id ).extra(select = {'selected':'id = %d' % (sel,)}).order_by('name')
if req.REQUEST.has_key('province'): dsts = dsts.filter(province__id = int(req.REQUEST['province']))
#if req.REQUEST.has_key('district'): dsts = dsts.filter(id = int(req.REQUEST['district']))
return dsts
else:
return []
except Exception, e:
return []
def paginated(req, data):
req.base_template = "webapp/layout.html"
paginator = Paginator(data, 20)
try: page = int(req.GET.get("page", '1'))
except ValueError: page = 1
try:
data = paginator.page(page)
except (InvalidPage, EmptyPage):
data = paginator.page(paginator.num_pages)
return data
#Commented because of RHEA group rights
#@permission_required('ubuzima.can_view')
def get_user_location(req):
req.base_template = "webapp/layout.html"
uloc = get_object_or_404(UserLocation, user=req.user)
return uloc
def csv_chws(chws,group):
response = HttpResponse(mimetype='text/csv')
response['Content-Disposition'] = 'attachment; filename=%s-export-%s.csv' % (group.name,datetime.datetime.now().strftime('%Y-%m-%d-%H-%M-%S'))
writer = csv.writer(response)
writer.writerow(['IZINA RY\'UMURYANGO','ANDI MAZINA','ICYO AKORA(Binome, ASM)','IGITSINA(F:Gore,M:Gabo)',\
'AMASHULI YIZE(P:Primaire, S:Secondaire, U:University, N:Ntabwo yize)','ITARIKI Y\'AMAVUKO(DD/MM/AAAA)',\
'ITARIKI Y\'UBUJYANAMA(AAAA:Umwaka)','NUMERO Y\'INDANGAMUNTU', 'NUMERO YA TELEPHONE YAHAWE NA MNISANTE', 'UMUDUGUDU',\
'AKAGARI', 'SECTEUR', 'CENTRE DE SANTE(Ivuriro)', 'HOPITAL(Ibitaro)', 'DISTRICT(Akarere)'])
for r in chws:
surname = given_name = role = sex = education_level = date_of_birth = join_date = national_id = telephone_moh = \
village = cell = sector = district = health_centre = referral_hospital = ""
try: surname = r.surname
except: pass
try: given_name = r.given_name
except: pass
try: role = r.role.name
except: pass
try: sex = r.sex
except: pass
try: education_level = r.education_level
except: pass
try: date_of_birth = "%d/%d/%d" % (r.date_of_birth.day, r.date_of_birth.month, r.date_of_birth.year )
except: pass
try: join_date = "%d/%d/%d" % (r.join_date.day, r.join_date.month, r.join_date.year)
except: pass
try: national_id = r.national_id
except: pass
try: telephone_moh = r.telephone_moh
except: pass
try: village = r.village.name
except: pass
try: cell = r.cell.name
except: pass
try: sector = r.sector.name
except: pass
try: district = r.district.name
except: pass
try: health_centre = r.health_centre.name
except: pass
try: referral_hospital = r.referral_hospital.name
except: pass
writer.writerow([surname, given_name, role, sex, \
education_level, date_of_birth , join_date, national_id,\
telephone_moh, village, cell, sector,\
health_centre, referral_hospital, district])
return response
def excel_chws(chws):
workbook = create_workbook()
sheet = create_worksheet(workbook, "reporters")
sheet = create_heads(sheet, ['IZINA RY\'UMURYANGO','ANDI MAZINA','ICYO AKORA(Binome, ASM)','IGITSINA(F:Gore,M:Gabo)',\
'AMASHULI YIZE(P:Primaire, S:Secondaire, U:University, N:Ntabwo yize)','ITARIKI Y\'AMAVUKO(DD/MM/AAAA)',\
'ITARIKI Y\'UBUJYANAMA(AAAA:Umwaka)','NUMERO Y\'INDANGAMUNTU', 'NUMERO YA TELEPHONE YAHAWE NA MNISANTE', 'UMUDUGUDU',\
'AKAGARI', 'SECTEUR', 'CENTRE DE SANTE(Ivuriro)', 'HOPITAL(Ibitaro)', 'DISTRICT(Akarere)'])
row = 1
for r in chws:
surname = given_name = role = sex = education_level = date_of_birth = join_date = national_id = telephone_moh = \
village = cell = sector = district = health_centre = referral_hospital = ""
try: surname = r.surname
except: pass
try: given_name = r.given_name
except: pass
try: role = r.role.name
except: pass
try: sex = r.sex
except: pass
try: education_level = r.education_level
except: pass
try: date_of_birth = "%d/%d/%d" % (r.date_of_birth.day, r.date_of_birth.month, r.date_of_birth.year )
except: pass
try: join_date = "%d/%d/%d" % (r.join_date.day, r.join_date.month, r.join_date.year)
except: pass
try: national_id = r.national_id
except: pass
try: telephone_moh = r.telephone_moh
except: pass
try: village = r.village.name
except: pass
try: cell = r.cell.name
except: pass
try: sector = r.sector.name
except: pass
try: district = r.district.name
except: pass
try: health_centre = r.health_centre.name
except: pass
try: referral_hospital = r.referral_hospital.name
except: pass
sheet = create_content(sheet, row, [surname, given_name, role, sex, \
education_level, date_of_birth , join_date, national_id,\
telephone_moh, village, cell, sector,\
health_centre, referral_hospital, district])
row = row + 1
response = HttpResponse(mimetype = "application/ms-excel")
response['Content-Disposition'] = 'attachment; filename = reporters.xls'
workbook.save(response)
return response
def excel_regs_confirms(regs):
workbook = create_workbook()
sheet = create_worksheet(workbook, "reporters")
sheet = create_heads(sheet, ['IZINA RY\'UMURYANGO','ANDI MAZINA','ICYO AKORA(Binome, ASM)','IGITSINA(F:Gore,M:Gabo)',\
'AMASHULI YIZE(P:Primaire, S:Secondaire, U:University, N:Ntabwo yize)','ITARIKI Y\'AMAVUKO(DD/MM/AAAA)',\
'ITARIKI Y\'UBUJYANAMA(AAAA:Umwaka)','NUMERO Y\'INDANGAMUNTU', 'NUMERO YA TELEPHONE YAHAWE NA MNISANTE', 'UMUDUGUDU',\
'AKAGARI', 'SECTEUR', 'CENTRE DE SANTE(Ivuriro)', 'HOPITAL(Ibitaro)', 'DISTRICT(Akarere)'])
row = 1
for r in regs:
surname = given_name = role = sex = education_level = date_of_birth = join_date = national_id = telephone_moh = \
village = cell = sector = district = health_centre = referral_hospital = ""
try: surname = r.reporter.surname
except: pass
try: given_name = r.reporter.given_name
except: pass
try: role = r.reporter.role.name
except: pass
try: sex = r.reporter.sex
except: pass
try: education_level = r.reporter.education_level
except: pass
try: date_of_birth = "%d/%d/%d" % (r.reporter.date_of_birth.day, r.reporter.date_of_birth.month, r.reporter.date_of_birth.year )
except: pass
try: join_date = "%d/%d/%d" % (r.reporter.join_date.day, r.reporter.join_date.month, r.reporter.join_date.year)
except: pass
try: national_id = r.reporter.national_id
except: pass
try: telephone_moh = r.reporter.telephone_moh
except: pass
try: village = r.reporter.village.name
except: pass
try: cell = r.reporter.cell.name
except: pass
try: sector = r.reporter.sector.name
except: pass
try: district = r.reporter.district.name
except: pass
try: health_centre = r.reporter.health_centre.name
except: pass
try: referral_hospital = r.reporter.referral_hospital.name
except: pass
sheet = create_content(sheet, row, [surname, given_name, role, sex, \
education_level, date_of_birth , join_date, national_id,\
telephone_moh, village, cell, sector,\
health_centre, referral_hospital, district])
row = row + 1
response = HttpResponse(mimetype = "application/ms-excel")
response['Content-Disposition'] = 'attachment; filename = reporters.xls'
workbook.save(response)
return response
def excel_supervisors(chws):
workbook = create_workbook()
sheet = create_worksheet(workbook, "reporters")
sheet = create_heads(sheet, ['AMAZINA','ITARIKI Y\'AMAVUKO(DD/MM/AAAA)','EMAIL','NUMERO Y\'INDANGAMUNTU', 'NUMERO YA TELEPHONE YAHAWE NA MNISANTE', 'UMUDUGUDU',\
'AKAGARI', 'SECTEUR', 'CENTRE DE SANTE(Ivuriro)', 'HOPITAL(Ibitaro)', 'DISTRICT(Akarere)'])
row = 1
for r in chws:
surname = date_of_birth = email = national_id = telephone_moh = \
village = cell = sector = district = health_centre = referral_hospital = ""
try: surname = r.names
except: pass
try: date_of_birth = "%d/%d/%d" % (r.dob.day, r.dob.month, r.dob.year )
except: pass
try: email = r.email
except: pass
try: national_id = r.national_id
except: pass
try: telephone_moh = r.telephone_moh
except: pass
try: village = r.village.name
except: pass
try: cell = r.cell.name
except: pass
try: sector = r.sector.name
except: pass
try: district = r.district.name
except: pass
try: health_centre = r.health_centre.name
except: pass
try: referral_hospital = r.referral_hospital.name
except: pass
sheet = create_content(sheet, row, [surname, date_of_birth , email, national_id,\
telephone_moh, village, cell, sector,\
health_centre, referral_hospital, district])
row = row + 1
response = HttpResponse(mimetype = "application/ms-excel")
response['Content-Disposition'] = 'attachment; filename = reporters.xls'
workbook.save(response)
return response
def write(data, filename):
with open(filename, 'w') as output:
json.dump(data, output)
return True
def load(filename):
with open(filename, 'r') as input:
data = json.load(input)
return data
def ValuesQuerySetToDict(vqs):
return [item for item in vqs]
def valFromListOneDIMDict(ld):
return [item.values()[0] for item in ld]
def my_area(req):
rez = {}
pst = {}
level = get_level(req)
try:
loc = int(req.REQUEST['location'])
rez['location'] = loc
except KeyError:
try:
dst=int(req.REQUEST['district'])
rez['district'] = dst
except KeyError:
try:
prv=int(req.REQUEST['province'])
rez['province'] = prv
except KeyError: pass
if level['level'] == 'Nation': pst['nation'] = level['uloc'].nation.id
elif level['level'] == 'Province': pst['province'] = level['uloc'].province.id
elif level['level'] == 'District': pst['district'] = level['uloc'].district.id
elif level['level'] == 'HealthCentre': pst['location'] = level['uloc'].health_centre.id
return [rez,pst]