-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisease.py
1783 lines (1563 loc) · 70.5 KB
/
disease.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
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
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
""" Sahana Eden Disease Tracking Models
@copyright: 2014-2017 (c) Sahana Software Foundation
@license: MIT
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
"""
__all__ = ("DiseaseDataModel",
"CaseTrackingModel",
"ContactTracingModel",
"DiseaseStatsModel",
"disease_rheader",
)
import datetime
import json
from gluon import *
from gluon.storage import Storage
from ..s3 import *
from s3layouts import S3PopupLink
# Monitoring upgrades {new_level:previous_levels}
MONITORING_UPGRADE = {"OBSERVATION": ("NONE",
"FOLLOW-UP",
),
"DIAGNOSTICS": ("NONE",
"OBSERVATION",
"FOLLOW-UP",
),
"QUARANTINE": ("NONE",
"OBSERVATION",
"DIAGNOSTICS",
"FOLLOW-UP",
),
}
# =============================================================================
class DiseaseDataModel(S3Model):
names = ("disease_disease",
"disease_disease_id",
"disease_symptom",
"disease_symptom_id",
)
def model(self):
T = current.T
db = current.db
crud_strings = current.response.s3.crud_strings
define_table = self.define_table
# =====================================================================
# Basic Disease Information
#
tablename = "disease_disease"
define_table(tablename,
self.super_link("doc_id", "doc_entity"),
# @ToDo: Labels for i18n
Field("name",
requires = IS_NOT_EMPTY()
),
Field("short_name"),
Field("acronym"),
Field("code",
label = T("ICD-10-CM Code"),
),
Field("description", "text"),
Field("trace_period", "integer",
label = T("Trace Period before Symptom Debut (days)"),
),
Field("watch_period", "integer",
label = T("Watch Period after Exposure (days)"),
),
s3_comments(),
*s3_meta_fields())
represent = S3Represent(lookup=tablename)
disease_id = S3ReusableField("disease_id", "reference %s" % tablename,
label = T("Disease"),
represent = represent,
requires = IS_ONE_OF(db, "disease_disease.id",
represent,
),
sortby = "name",
comment = S3PopupLink(f = "disease",
tooltip = T("Add a new disease to the catalog"),
),
)
self.add_components(tablename,
disease_symptom = "disease_id",
)
self.configure(tablename,
deduplicate = self.disease_duplicate,
super_entity = "doc_entity",
)
# CRUD strings
crud_strings[tablename] = Storage(
label_create = T("Create Disease"),
title_display = T("Disease Information"),
title_list = T("Diseases"),
title_update = T("Edit Disease Information"),
title_upload = T("Import Disease Information"),
label_list_button = T("List Diseases"),
label_delete_button = T("Delete Disease Information"),
msg_record_created = T("Disease Information added"),
msg_record_modified = T("Disease Information updated"),
msg_record_deleted = T("Disease Information deleted"),
msg_list_empty = T("No Diseases currently registered"))
# =====================================================================
# Symptom Information
#
tablename = "disease_symptom"
define_table(tablename,
disease_id(),
Field("name"),
Field("description",
label = T("Short Description"),
),
Field("assessment",
label = T("Assessment method"),
),
*s3_meta_fields())
# @todo: refine to include disease name?
represent = S3Represent(lookup=tablename)
symptom_id = S3ReusableField("symptom_id", "reference %s" % tablename,
label = T("Symptom"),
represent = represent,
requires = IS_ONE_OF(db, "disease_symptom.id",
represent,
),
sortby = "name",
)
# CRUD strings
crud_strings[tablename] = Storage(
label_create = T("Add Symptom"),
title_display = T("Symptom Information"),
title_list = T("Symptoms"),
title_update = T("Edit Symptom Information"),
title_upload = T("Import Symptom Information"),
label_list_button = T("List Symptoms"),
label_delete_button = T("Delete Symptom Information"),
msg_record_created = T("Symptom Information added"),
msg_record_modified = T("Symptom Information updated"),
msg_record_deleted = T("Symptom Information deleted"),
msg_list_empty = T("No Symptom Information currently available"))
# Pass names back to global scope (s3.*)
return dict(disease_disease_id = disease_id,
disease_symptom_id = symptom_id,
)
# -------------------------------------------------------------------------
@staticmethod
def defaults():
""" Safe defaults for names in case the module is disabled """
dummy = S3ReusableField("dummy_id", "integer",
readable = False,
writable = False)
return dict(disease_disease_id = lambda **attr: dummy("disease_id"),
disease_symptom_id = lambda **attr: dummy("symptom_id"),
)
# -------------------------------------------------------------------------
@staticmethod
def disease_duplicate(item):
"""
Disease import update detection
@param item: the import item
"""
data = item.data
code = data.get("code")
name = data.get("name")
table = item.table
queries = []
if code:
queries.append((table.code == code))
if name:
queries.append((table.name == name))
if queries:
query = reduce(lambda x, y: x | y, queries)
else:
return
rows = current.db(query).select(table.id,
table.code,
table.name)
duplicate = None
for row in rows:
if code and row.code == code:
duplicate = row.id
break
if name and row.name == name:
duplicate = row.id
if duplicate:
item.id = duplicate
item.method = item.METHOD.UPDATE
return
# =============================================================================
class CaseTrackingModel(S3Model):
names = ("disease_case",
"disease_case_id",
"disease_case_monitoring",
"disease_case_monitoring_symptom",
"disease_case_diagnostics",
)
def model(self):
# @todo: add treatment component?
T = current.T
db = current.db
crud_strings = current.response.s3.crud_strings
define_table = self.define_table
configure = self.configure
add_components = self.add_components
person_id = self.pr_person_id
# =====================================================================
# Diagnosis Status
#
diagnosis_status = {"UNKNOWN": T("Unknown"),
"RISK": T("At Risk"),
"PROBABLE": T("Probable"),
"CONFIRMED-POS": T("Confirmed Positive"),
"CONFIRMED-NEG": T("Confirmed Negative"),
}
diagnosis_status_represent = S3Represent(options = diagnosis_status)
# =====================================================================
# Monitoring Levels
#
monitoring_levels = {"NONE": T("No Monitoring"),
# Clinical observation required:
"OBSERVATION": T("Observation"),
# Targeted diagnostics required:
"DIAGNOSTICS": T("Diagnostics"),
# Quarantine required:
"QUARANTINE": T("Quarantine"),
# Follow-up after recovery:
"FOLLOW-UP": T("Post-Recovery Follow-Up"),
}
monitoring_level_represent = S3Represent(options = monitoring_levels)
# =====================================================================
# Illness status
#
illness_status = {"UNKNOWN": T("Unknown, Not Checked"),
"ASYMPTOMATIC": T("Asymptomatic, Clinical Signs Negative"),
"SYMPTOMATIC": T("Symptomatic, Clinical Signs Positive"),
"SEVERE": T("Severely Ill, Clinical Signs Positive"),
"DECEASED": T("Deceased, Clinical Signs Positive"),
"RECOVERED": T("Recovered"),
}
illness_status_represent = S3Represent(options = illness_status)
# =====================================================================
# Case
#
tablename = "disease_case"
define_table(tablename,
Field("case_number", length=64,
requires = IS_EMPTY_OR([
IS_LENGTH(64),
IS_NOT_IN_DB(db, "disease_case.case_number"),
]),
),
person_id(empty = False,
ondelete = "CASCADE",
),
self.disease_disease_id(),
#s3_date(), # date registered == created_on?
self.gis_location_id(),
# @todo: add site ID for registering site?
# Current illness status and symptom debut
Field("illness_status",
label = T("Current Illness Status"),
represent = illness_status_represent,
requires = IS_IN_SET(illness_status),
default = "UNKNOWN",
),
s3_date("symptom_debut",
label = T("Symptom Debut"),
),
# Current diagnosis status and date of last status update
Field("diagnosis_status",
label = T("Diagnosis Status"),
represent = diagnosis_status_represent,
requires = IS_IN_SET(diagnosis_status),
default = "UNKNOWN",
),
s3_date("diagnosis_date",
default = "now",
label = T("Diagnosis Date"),
),
# Current monitoring level and end date
Field("monitoring_level",
label = T("Current Monitoring Level"),
represent = monitoring_level_represent,
requires = IS_IN_SET(monitoring_levels),
default = "NONE",
),
s3_date("monitoring_until",
label = T("Monitoring required until"),
),
*s3_meta_fields())
# Reusable Field
represent = disease_CaseRepresent()
case_id = S3ReusableField("case_id", "reference %s" % tablename,
label = T("Case"),
represent = represent,
requires = IS_ONE_OF(db, "disease_case.id",
represent,
),
comment = S3PopupLink(f = "case",
tooltip = T("Add a new case"),
),
)
# Components
add_components(tablename,
disease_case_monitoring = "case_id",
disease_case_diagnostics = "case_id",
disease_tracing = "case_id",
disease_exposure = ({"name": "exposure",
"joinby": "person_id",
"pkey": "person_id",
},
{"name": "contact",
"joinby": "case_id",
},
),
)
report_fields = ["disease_id",
"location_id",
"illness_status",
"monitoring_level",
"diagnosis_status",
]
report_options = {"rows": report_fields,
"cols": report_fields,
"fact": [(T("Number of Cases"), "count(id)"),
],
"defaults": {"rows": "location_id",
"cols": "diagnosis_status",
"fact": "count(id)",
"totals": True,
},
}
filter_widgets = [S3TextFilter(["case_number",
"person_id$first_name",
"person_id$middle_name",
"person_id$last_name",
],
label = T("Search"),
comment = T("Enter Case Number or Name"),
),
S3OptionsFilter("monitoring_level",
options = monitoring_levels,
),
S3OptionsFilter("diagnosis_status",
options = diagnosis_status,
),
S3LocationFilter("location_id",
),
]
configure(tablename,
create_onvalidation = self.case_create_onvalidation,
deduplicate = self.case_duplicate,
delete_next = URL(f="case", args=["summary"]),
filter_widgets = filter_widgets,
onaccept = self.case_onaccept,
report_options = report_options,
)
# CRUD strings
crud_strings[tablename] = Storage(
label_create = T("Create Case"),
title_display = T("Case Details"),
title_list = T("Cases"),
title_update = T("Edit Cases"),
title_upload = T("Import Cases"),
label_list_button = T("List Cases"),
label_delete_button = T("Delete Case"),
msg_record_created = T("Case added"),
msg_record_modified = T("Case updated"),
msg_record_deleted = T("Case deleted"),
msg_list_empty = T("No Cases currently registered"))
# =====================================================================
# Monitoring
#
tablename = "disease_case_monitoring"
define_table(tablename,
case_id(),
s3_datetime(default="now"),
Field("illness_status",
represent = illness_status_represent,
requires = IS_IN_SET(illness_status),
),
s3_comments(),
*s3_meta_fields())
# Reusable Field
represent = S3Represent(lookup=tablename, fields=["case_id"])
status_id = S3ReusableField("status_id", "reference %s" % tablename,
label = T("Case"),
represent = represent,
requires = IS_ONE_OF(db, "disease_case.id",
represent,
),
comment = S3PopupLink(f = "case",
tooltip = T("Add a new case"),
),
)
# Components
add_components(tablename,
disease_symptom = {"link": "disease_case_monitoring_symptom",
"joinby": "status_id",
"key": "symptom_id",
}
)
# Custom CRUD form
crud_fields = ["case_id",
"date",
"illness_status",
S3SQLInlineLink("symptom",
field = "symptom_id",
label = T("Symptoms"),
multiple = True,
),
"comments",
]
configure(tablename,
crud_form = S3SQLCustomForm(*crud_fields),
list_fields = ["date",
"illness_status",
(T("Symptoms"), "symptom.name"),
"comments",
],
onaccept = self.monitoring_onaccept,
)
# CRUD strings
crud_strings[tablename] = Storage(
label_create = T("Add Monitoring Update"),
title_display = T("Monitoring Update"),
title_list = T("Monitoring Updates"),
title_update = T("Edit Monitoring Update"),
title_upload = T("Import Monitoring Updates"),
label_list_button = T("List Monitoring Updates"),
label_delete_button = T("Delete Monitoring Update"),
msg_record_created = T("Monitoring Update added"),
msg_record_modified = T("Monitoring Update updated"),
msg_record_deleted = T("Monitoring Update deleted"),
msg_list_empty = T("No Monitoring Information currently available"))
# =====================================================================
# Monitoring <=> Symptom
#
tablename = "disease_case_monitoring_symptom"
define_table(tablename,
Field("status_id", "reference disease_case_monitoring",
requires = IS_ONE_OF(db, "disease_case_monitoring.id"),
),
self.disease_symptom_id(),
*s3_meta_fields())
# =====================================================================
# Diagnostics
#
probe_status = {"PENDING": T("Pending"),
"PROCESSED": T("Processed"),
"VALIDATED": T("Validated"),
"INVALID": T("Invalid"),
"LOST": T("Lost"),
}
tablename = "disease_case_diagnostics"
define_table(tablename,
case_id(),
# @todo: make a lookup table in DiseaseDataModel:
Field("probe_type"),
Field("probe_number", length=64, unique=True,
requires = IS_LENGTH(64),
),
s3_date("probe_date",
default = "now",
label = T("Probe Date"),
),
Field("probe_status",
represent = S3Represent(options = probe_status),
requires = IS_IN_SET(probe_status),
default = "PENDING",
),
# @todo: make a lookup table in DiseaseDataModel:
Field("test_type"),
Field("result"),
s3_date("result_date",
label = T("Result Date"),
),
Field("conclusion",
represent = diagnosis_status_represent,
requires = IS_EMPTY_OR(
IS_IN_SET(diagnosis_status)),
),
s3_comments(),
*s3_meta_fields())
# CRUD strings
crud_strings[tablename] = Storage(
label_create = T("Add Diagnostic Test"),
title_display = T("Diagnostic Test Details"),
title_list = T("Diagnostic Tests"),
title_update = T("Edit Diagnostic Test Details"),
title_upload = T("Import Diagnostic Test Data"),
label_list_button = T("List Diagnostic Tests"),
label_delete_button = T("Delete Diagnostic Test"),
msg_record_created = T("Diagnostic Test added"),
msg_record_modified = T("Diagnostic Test updated"),
msg_record_deleted = T("Diagnostic Test deleted"),
msg_list_empty = T("No Diagnostic Tests currently registered"))
# =====================================================================
# Pass names back to global scope (s3.*)
return dict(disease_case_id = case_id)
# -------------------------------------------------------------------------
@staticmethod
def defaults():
""" Safe defaults for names in case the module is disabled """
dummy = S3ReusableField("dummy_id", "integer",
readable = False,
writable = False)
return dict(disease_case_id = lambda **attr: dummy("case_id"),
)
# -------------------------------------------------------------------------
@staticmethod
def get_case(person_id, disease_id):
"""
Find the case record for a person for a disease
@param person_id: the person record ID
@param disease_id: the disease record ID
"""
ctable = current.s3db.disease_case
query = (ctable.person_id == person_id) & \
(ctable.disease_id == disease_id) & \
(ctable.deleted != True)
record = current.db(query).select(ctable.id,
ctable.case_number,
limitby = (0, 1)).first()
return record
# -------------------------------------------------------------------------
@classmethod
def case_create_onvalidation(cls, form):
"""
Make sure that there's only one case per person and disease
"""
formvars = form.vars
try:
case_id = formvars.id
person_id = formvars.person_id
except AttributeError, e:
return
if "disease_id" not in formvars:
disease_id = current.s3db.disease_case.disease_id.default
else:
disease_id = formvars.disease_id
record = cls.get_case(person_id, disease_id)
if record and record.id != case_id:
error = current.T("This case is already registered")
link = A(record.case_number,
_href=URL(f="case", args=[record.id]))
form.errors.person_id = XML("%s: %s" % (error, link))
return
# -------------------------------------------------------------------------
@staticmethod
def case_duplicate(item):
"""
Case import update detection
@param item: the import item
"""
data = item.data
case_number = data.get("case_number")
person_id = data.get("person_id")
table = item.table
if case_number:
query = (table.case_number == case_number) & \
(table.deleted != True)
else:
disease_id = data.get("disease_id")
if person_id and disease_id:
query = (table.disease_id == disease_id) & \
(table.person_id == person_id) & \
(table.deleted != True)
else:
return
duplicate = current.db(query).select(table.id,
table.person_id,
limitby=(0, 1)).first()
if duplicate:
item.data.person_id = duplicate.person_id
item.id = duplicate.id
item.method = item.METHOD.UPDATE
return
# -------------------------------------------------------------------------
@staticmethod
def case_onaccept(form):
"""
Propagate status updates of the case to high-risk contacts
"""
formvars = form.vars
try:
record_id = formvars.id
except AttributeError:
return
disease_propagate_case_status(record_id)
return
# -------------------------------------------------------------------------
@staticmethod
def monitoring_onaccept(form):
"""
Update the illness status of the case from last monitoring entry
"""
formvars = form.vars
try:
record_id = formvars.id
except AttributeError:
return
db = current.db
s3db = current.s3db
ctable = s3db.disease_case
mtable = s3db.disease_case_monitoring
# Get the case ID
case_id = None
if "case_id" not in formvars:
query = (mtable.id == record_id)
row = db(query).select(mtable.case_id, limitby=(0, 1)).first()
if row:
case_id = row.case_id
else:
case_id = formvars.case_id
if not case_id:
return
query = (mtable.case_id == case_id) & \
(mtable.illness_status != None)
row = db(query).select(mtable.illness_status,
orderby = "disease_case_monitoring.date desc",
limitby = (0, 1)).first()
if row:
db(ctable.id == case_id).update(illness_status = row.illness_status)
# Propagate case status to contacts
disease_propagate_case_status(case_id)
return
# =============================================================================
class disease_CaseRepresent(S3Represent):
def __init__(self):
""" Constructor """
super(disease_CaseRepresent, self).__init__(lookup = "disease_case")
# -------------------------------------------------------------------------
def lookup_rows(self, key, values, fields=[]):
"""
Custom rows lookup
@param key: the key Field
@param values: the values
@param fields: unused (retained for API compatibility)
"""
s3db = current.s3db
table = self.table
ptable = s3db.pr_person
dtable = s3db.disease_disease
left = [ptable.on(ptable.id == table.person_id),
dtable.on(dtable.id == table.disease_id)]
if len(values) == 1:
query = (key == values[0])
else:
query = key.belongs(values)
rows = current.db(query).select(table.id,
table.case_number,
dtable.name,
dtable.short_name,
dtable.acronym,
ptable.first_name,
ptable.last_name,
left = left)
self.queries += 1
return rows
# -------------------------------------------------------------------------
def represent_row(self, row):
"""
Represent a row
@param row: the Row
"""
try:
case_number = row[self.tablename].case_number
except AttributeError:
return row.case_number
disease_name = None
try:
disease = row["disease_disease"]
except AttributeError:
pass
else:
for field in ("acronym", "short_name", "name"):
if field in disease:
disease_name = disease[field]
if disease_name:
break
if disease_name and case_number:
case = "%s [%s]" % (case_number, disease_name)
elif disease_name:
case = "[%s]" % disease_name
else:
case = case_number
try:
person = row["pr_person"]
except AttributeError:
return case
full_name = s3_fullname(person)
if case:
return " ".join((case, full_name))
else:
return full_name
# =============================================================================
class ContactTracingModel(S3Model):
names = ("disease_tracing",
"disease_exposure",
)
def model(self):
T = current.T
db = current.db
crud_strings = current.response.s3.crud_strings
define_table = self.define_table
case_id = self.disease_case_id
# =====================================================================
# Tracing Information: when/where did a case pose risk for exposure?
#
# Processing Status
contact_tracing_status = {
"OPEN": T("Open"), # not all contacts identified yet
"COMPLETE": T("Complete"), # all contacts identified, closed
}
tablename = "disease_tracing"
define_table(tablename,
case_id(),
s3_datetime("start_date",
label = T("From"),
set_min="#disease_tracing_end_date",
),
s3_datetime("end_date",
label = T("To"),
set_max="#disease_tracing_start_date",
),
# @todo: add site_id?
self.gis_location_id(),
Field("circumstances", "text",
),
Field("status",
default = "OPEN",
label = T("Tracing Status"),
requires = IS_IN_SET(contact_tracing_status, zero=None),
represent = S3Represent(options=contact_tracing_status),
),
s3_comments(),
*s3_meta_fields())
# @todo: implement specific S3Represent class
represent = S3Represent(lookup=tablename, fields=["case_id"])
tracing_id = S3ReusableField("tracing_id", "reference %s" % tablename,
label = T("Tracing Record"),
represent = represent,
requires = IS_EMPTY_OR(
IS_ONE_OF(db, "disease_tracing.id",
represent,
)),
sortby = "date",
comment = S3PopupLink(f = "tracing",
tooltip = T("Add a new contact tracing information"),
),
)
self.add_components(tablename,
disease_exposure = "tracing_id",
)
# CRUD strings
crud_strings[tablename] = Storage(
label_create = T("Add Tracing Record"),
title_display = T("Tracing Details"),
title_list = T("Contact Tracings"),
title_update = T("Edit Tracing Information"),
title_upload = T("Import Tracing Information"),
label_list_button = T("List Tracing Record"),
label_delete_button = T("Delete Tracing Record"),
msg_record_created = T("Tracing Record added"),
msg_record_modified = T("Tracing Record updated"),
msg_record_deleted = T("Tracing Record deleted"),
msg_list_empty = T("No Contact Tracings currently registered"))
# =====================================================================
# Protection
#
protection_level = {"NONE": T("Unknown"),
"PARTIAL": T("Partial"),
"FULL": T("Full"),
}
protection_level_represent = S3Represent(options = protection_level)
# =====================================================================
# Exposure Type
#
exposure_type = {"UNKNOWN": T("Unknown"),
"DIRECT": T("Direct"),
"INDIRECT": T("Indirect"),
}
exposure_type_represent = S3Represent(options = exposure_type)
# =====================================================================
# Exposure Risk Level
#
exposure_risk = {"UNKNOWN": T("Unknown"),
"NONE": T("No known exposure"),
"LOW": T("Low risk exposure"),
"HIGH": T("High risk exposure"),
}
exposure_risk_represent = S3Represent(options = exposure_risk)
# =====================================================================
# Exposure: when and how was a person exposed to the disease?
#
tablename = "disease_exposure"
define_table(tablename,
case_id(),
tracing_id(),
self.pr_person_id(requires = IS_ADD_PERSON_WIDGET2(),
widget = S3AddPersonWidget2(controller="pr"),
),
s3_datetime(),
#self.gis_location_id(),
Field("exposure_type",
default = "UNKNOWN",
represent = exposure_type_represent,
requires = IS_IN_SET(exposure_type, zero=None),
),
Field("protection_level",
default = "NONE",
represent = protection_level_represent,
requires = IS_IN_SET(protection_level, zero=None),
),
Field("exposure_risk",
default = "LOW",
represent = exposure_risk_represent,
requires = IS_IN_SET(exposure_risk, zero=None),
),
Field("circumstances", "text"),
*s3_meta_fields())
self.configure(tablename,
onaccept = self.exposure_onaccept,
)
crud_strings[tablename] = Storage(
label_create = T("Add Exposure Information"),
title_display = T("Exposure Details"),
title_list = T("Exposure Information"),
title_update = T("Edit Exposure Information"),
title_upload = T("Import Exposure Information"),
label_list_button = T("List Exposures"),
label_delete_button = T("Delete Exposure Information"),
msg_record_created = T("Exposure Information added"),
msg_record_modified = T("Exposure Information updated"),
msg_record_deleted = T("Exposure Information deleted"),
msg_list_empty = T("No Exposure Information currently registered"))
# Pass names back to global scope (s3.*)
return {}
# -------------------------------------------------------------------------
@staticmethod
def defaults():
return {}
# -------------------------------------------------------------------------
@staticmethod
def exposure_onaccept(form):
"""
@todo: docstring
"""
formvars = form.vars
try:
record_id = formvars.id
except AttributeError:
return
db = current.db
s3db = current.s3db
# We need case_id, person_id and exposure_risk from the current record
if "case_id" not in formvars: