<s> """<STR_LIT>""" <EOL> __all__ = ( "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ) <EOL> import datetime <EOL> try : <EOL> import json <EOL> except ImportError : <EOL> try : <EOL> import simplejson as json <EOL> except : <EOL> import gluon . contrib . simplejson as json <EOL> from gluon import * <EOL> from gluon . storage import Storage <EOL> from . . s3 import * <EOL> from s3layouts import S3PopupLink <EOL> MONITORING_UPGRADE = { "<STR_LIT>" : ( "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ) , <EOL> "<STR_LIT>" : ( "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ) , <EOL> "<STR_LIT>" : ( "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ) , <EOL> } <EOL> class DiseaseDataModel ( S3Model ) : <EOL> names = ( "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ) <EOL> def model ( self ) : <EOL> T = current . T <EOL> db = current . db <EOL> crud_strings = current . response . s3 . crud_strings <EOL> define_table = self . define_table <EOL> tablename = "<STR_LIT>" <EOL> define_table ( tablename , <EOL> self . super_link ( "<STR_LIT>" , "<STR_LIT>" ) , <EOL> Field ( "<STR_LIT:name>" , <EOL> requires = IS_NOT_EMPTY ( ) <EOL> ) , <EOL> Field ( "<STR_LIT>" ) , <EOL> Field ( "<STR_LIT>" ) , <EOL> Field ( "<STR_LIT:code>" , <EOL> label = T ( "<STR_LIT>" ) , <EOL> ) , <EOL> Field ( "<STR_LIT:description>" , "<STR_LIT:text>" ) , <EOL> Field ( "<STR_LIT>" , "<STR_LIT>" , <EOL> label = T ( "<STR_LIT>" ) , <EOL> ) , <EOL> Field ( "<STR_LIT>" , "<STR_LIT>" , <EOL> label = T ( "<STR_LIT>" ) , <EOL> ) , <EOL> s3_comments ( ) , <EOL> * s3_meta_fields ( ) ) <EOL> represent = S3Represent ( lookup = tablename ) <EOL> disease_id = S3ReusableField ( "<STR_LIT>" , "<STR_LIT>" % tablename , <EOL> label = T ( "<STR_LIT>" ) , <EOL> represent = represent , <EOL> requires = IS_ONE_OF ( db , "<STR_LIT>" , <EOL> represent , <EOL> ) , <EOL> sortby = "<STR_LIT:name>" , <EOL> comment = S3PopupLink ( f = "<STR_LIT>" , <EOL> tooltip = T ( "<STR_LIT>" ) , <EOL> ) , <EOL> ) <EOL> self . add_components ( tablename , <EOL> disease_symptom = "<STR_LIT>" , <EOL> ) <EOL> self . configure ( tablename , <EOL> deduplicate = self . disease_duplicate , <EOL> super_entity = "<STR_LIT>" , <EOL> ) <EOL> crud_strings [ tablename ] = Storage ( <EOL> label_create = T ( "<STR_LIT>" ) , <EOL> title_display = T ( "<STR_LIT>" ) , <EOL> title_list = T ( "<STR_LIT>" ) , <EOL> title_update = T ( "<STR_LIT>" ) , <EOL> title_upload = T ( "<STR_LIT>" ) , <EOL> label_list_button = T ( "<STR_LIT>" ) , <EOL> label_delete_button = T ( "<STR_LIT>" ) , <EOL> msg_record_created = T ( "<STR_LIT>" ) , <EOL> msg_record_modified = T ( "<STR_LIT>" ) , <EOL> msg_record_deleted = T ( "<STR_LIT>" ) , <EOL> msg_list_empty = T ( "<STR_LIT>" ) ) <EOL> tablename = "<STR_LIT>" <EOL> define_table ( tablename , <EOL> disease_id ( ) , <EOL> Field ( "<STR_LIT:name>" ) , <EOL> Field ( "<STR_LIT:description>" , <EOL> label = T ( "<STR_LIT>" ) , <EOL> ) , <EOL> Field ( "<STR_LIT>" , <EOL> label = T ( "<STR_LIT>" ) , <EOL> ) , <EOL> * s3_meta_fields ( ) ) <EOL> represent = S3Represent ( lookup = tablename ) <EOL> symptom_id = S3ReusableField ( "<STR_LIT>" , "<STR_LIT>" % tablename , <EOL> label = T ( "<STR_LIT>" ) , <EOL> represent = represent , <EOL> requires = IS_ONE_OF ( db , "<STR_LIT>" , <EOL> represent , <EOL> ) , <EOL> sortby = "<STR_LIT:name>" , <EOL> ) <EOL> crud_strings [ tablename ] = Storage ( <EOL> label_create = T ( "<STR_LIT>" ) , <EOL> title_display = T ( "<STR_LIT>" ) , <EOL> title_list = T ( "<STR_LIT>" ) , <EOL> title_update = T ( "<STR_LIT>" ) , <EOL> title_upload = T ( "<STR_LIT>" ) , <EOL> label_list_button = T ( "<STR_LIT>" ) , <EOL> label_delete_button = T ( "<STR_LIT>" ) , <EOL> msg_record_created = T ( "<STR_LIT>" ) , <EOL> msg_record_modified = T ( "<STR_LIT>" ) , <EOL> msg_record_deleted = T ( "<STR_LIT>" ) , <EOL> msg_list_empty = T ( "<STR_LIT>" ) ) <EOL> return dict ( disease_disease_id = disease_id , <EOL> disease_symptom_id = symptom_id , <EOL> ) <EOL> @ staticmethod <EOL> def defaults ( ) : <EOL> """<STR_LIT>""" <EOL> dummy = S3ReusableField ( "<STR_LIT>" , "<STR_LIT>" , <EOL> readable = False , <EOL> writable = False ) <EOL> return dict ( disease_disease_id = lambda ** attr : dummy ( "<STR_LIT>" ) , <EOL> disease_symptom_id = lambda ** attr : dummy ( "<STR_LIT>" ) , <EOL> ) <EOL> @ staticmethod <EOL> def disease_duplicate ( item ) : <EOL> """<STR_LIT>""" <EOL> data = item . data <EOL> code = data . get ( "<STR_LIT:code>" ) <EOL> name = data . get ( "<STR_LIT:name>" ) <EOL> table = item . table <EOL> queries = [ ] <EOL> if code : <EOL> queries . append ( ( table . code == code ) ) <EOL> if name : <EOL> queries . append ( ( table . name == name ) ) <EOL> if queries : <EOL> query = reduce ( lambda x , y : x | y , queries ) <EOL> else : <EOL> return <EOL> rows = current . db ( query ) . select ( table . id , <EOL> table . code , <EOL> table . name ) <EOL> duplicate = None <EOL> for row in rows : <EOL> if code and row . code == code : <EOL> duplicate = row . id <EOL> break <EOL> if name and row . name == name : <EOL> duplicate = row . id <EOL> if duplicate : <EOL> item . id = duplicate <EOL> item . method = item . METHOD . UPDATE <EOL> return <EOL> class CaseTrackingModel ( S3Model ) : <EOL> names = ( "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ) <EOL> def model ( self ) : <EOL> T = current . T <EOL> db = current . db <EOL> crud_strings = current . response . s3 . crud_strings <EOL> define_table = self . define_table <EOL> configure = self . configure <EOL> add_components = self . add_components <EOL> person_id = self . pr_person_id <EOL> diagnosis_status = { "<STR_LIT>" : T ( "<STR_LIT>" ) , <EOL> "<STR_LIT>" : T ( "<STR_LIT>" ) , <EOL> "<STR_LIT>" : T ( "<STR_LIT>" ) , <EOL> "<STR_LIT>" : T ( "<STR_LIT>" ) , <EOL> "<STR_LIT>" : T ( "<STR_LIT>" ) , <EOL> } <EOL> diagnosis_status_represent = S3Represent ( options = diagnosis_status ) <EOL> monitoring_levels = { "<STR_LIT>" : T ( "<STR_LIT>" ) , <EOL> "<STR_LIT>" : T ( "<STR_LIT>" ) , <EOL> "<STR_LIT>" : T ( "<STR_LIT>" ) , <EOL> "<STR_LIT>" : T ( "<STR_LIT>" ) , <EOL> "<STR_LIT>" : T ( "<STR_LIT>" ) , <EOL> } <EOL> monitoring_level_represent = S3Represent ( options = monitoring_levels ) <EOL> illness_status = { "<STR_LIT>" : T ( "<STR_LIT>" ) , <EOL> "<STR_LIT>" : T ( "<STR_LIT>" ) , <EOL> "<STR_LIT>" : T ( "<STR_LIT>" ) , <EOL> "<STR_LIT>" : T ( "<STR_LIT>" ) , <EOL> "<STR_LIT>" : T ( "<STR_LIT>" ) , <EOL> "<STR_LIT>" : T ( "<STR_LIT>" ) , <EOL> } <EOL> illness_status_represent = S3Represent ( options = illness_status ) <EOL> tablename = "<STR_LIT>" <EOL> define_table ( tablename , <EOL> Field ( "<STR_LIT>" , length = <NUM_LIT:64> , <EOL> requires = IS_EMPTY_OR ( <EOL> IS_NOT_IN_DB ( db , "<STR_LIT>" ) ) , <EOL> ) , <EOL> person_id ( empty = False , <EOL> ondelete = "<STR_LIT>" , <EOL> ) , <EOL> self . disease_disease_id ( ) , <EOL> self . gis_location_id ( ) , <EOL> Field ( "<STR_LIT>" , <EOL> label = T ( "<STR_LIT>" ) , <EOL> represent = illness_status_represent , <EOL> requires = IS_IN_SET ( illness_status ) , <EOL> default = "<STR_LIT>" , <EOL> ) , <EOL> s3_date ( "<STR_LIT>" , <EOL> label = T ( "<STR_LIT>" ) , <EOL> ) , <EOL> Field ( "<STR_LIT>" , <EOL> label = T ( "<STR_LIT>" ) , <EOL> represent = diagnosis_status_represent , <EOL> requires = IS_IN_SET ( diagnosis_status ) , <EOL> default = "<STR_LIT>" , <EOL> ) , <EOL> s3_date ( "<STR_LIT>" , <EOL> default = "<STR_LIT>" , <EOL> label = T ( "<STR_LIT>" ) , <EOL> ) , <EOL> Field ( "<STR_LIT>" , <EOL> label = T ( "<STR_LIT>" ) , <EOL> represent = monitoring_level_represent , <EOL> requires = IS_IN_SET ( monitoring_levels ) , <EOL> default = "<STR_LIT>" , <EOL> ) , <EOL> s3_date ( "<STR_LIT>" , <EOL> label = T ( "<STR_LIT>" ) , <EOL> ) , <EOL> * s3_meta_fields ( ) ) <EOL> represent = disease_CaseRepresent ( ) <EOL> case_id = S3ReusableField ( "<STR_LIT>" , "<STR_LIT>" % tablename , <EOL> label = T ( "<STR_LIT>" ) , <EOL> represent = represent , <EOL> requires = IS_ONE_OF ( db , "<STR_LIT>" , <EOL> represent , <EOL> ) , <EOL> comment = S3PopupLink ( f = "<STR_LIT>" , <EOL> tooltip = T ( "<STR_LIT>" ) , <EOL> ) , <EOL> ) <EOL> add_components ( tablename , <EOL> disease_case_monitoring = "<STR_LIT>" , <EOL> disease_case_diagnostics = "<STR_LIT>" , <EOL> disease_tracing = "<STR_LIT>" , <EOL> disease_exposure = ( { "<STR_LIT:name>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> } , <EOL> { "<STR_LIT:name>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> } , <EOL> ) , <EOL> ) <EOL> report_fields = [ "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ] <EOL> report_options = { "<STR_LIT>" : report_fields , <EOL> "<STR_LIT>" : report_fields , <EOL> "<STR_LIT>" : [ ( T ( "<STR_LIT>" ) , "<STR_LIT>" ) , <EOL> ] , <EOL> "<STR_LIT>" : { "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : True , <EOL> } , <EOL> } <EOL> filter_widgets = [ S3TextFilter ( [ "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ] , <EOL> label = T ( "<STR_LIT>" ) , <EOL> comment = T ( "<STR_LIT>" ) , <EOL> ) , <EOL> S3OptionsFilter ( "<STR_LIT>" , <EOL> options = monitoring_levels , <EOL> ) , <EOL> S3OptionsFilter ( "<STR_LIT>" , <EOL> options = diagnosis_status , <EOL> ) , <EOL> S3LocationFilter ( "<STR_LIT>" , <EOL> ) , <EOL> ] <EOL> configure ( tablename , <EOL> create_onvalidation = self . case_create_onvalidation , <EOL> deduplicate = self . case_duplicate , <EOL> delete_next = URL ( f = "<STR_LIT>" , args = [ "<STR_LIT>" ] ) , <EOL> filter_widgets = filter_widgets , <EOL> onaccept = self . case_onaccept , <EOL> report_options = report_options , <EOL> ) <EOL> crud_strings [ tablename ] = Storage ( <EOL> label_create = T ( "<STR_LIT>" ) , <EOL> title_display = T ( "<STR_LIT>" ) , <EOL> title_list = T ( "<STR_LIT>" ) , <EOL> title_update = T ( "<STR_LIT>" ) , <EOL> title_upload = T ( "<STR_LIT>" ) , <EOL> label_list_button = T ( "<STR_LIT>" ) , <EOL> label_delete_button = T ( "<STR_LIT>" ) , <EOL> msg_record_created = T ( "<STR_LIT>" ) , <EOL> msg_record_modified = T ( "<STR_LIT>" ) , <EOL> msg_record_deleted = T ( "<STR_LIT>" ) , <EOL> msg_list_empty = T ( "<STR_LIT>" ) ) <EOL> tablename = "<STR_LIT>" <EOL> define_table ( tablename , <EOL> case_id ( ) , <EOL> s3_datetime ( default = "<STR_LIT>" ) , <EOL> Field ( "<STR_LIT>" , <EOL> represent = illness_status_represent , <EOL> requires = IS_IN_SET ( illness_status ) , <EOL> ) , <EOL> s3_comments ( ) , <EOL> * s3_meta_fields ( ) ) <EOL> represent = S3Represent ( lookup = tablename , fields = [ "<STR_LIT>" ] ) <EOL> status_id = S3ReusableField ( "<STR_LIT>" , "<STR_LIT>" % tablename , <EOL> label = T ( "<STR_LIT>" ) , <EOL> represent = represent , <EOL> requires = IS_ONE_OF ( db , "<STR_LIT>" , <EOL> represent , <EOL> ) , <EOL> comment = S3PopupLink ( f = "<STR_LIT>" , <EOL> tooltip = T ( "<STR_LIT>" ) , <EOL> ) , <EOL> ) <EOL> add_components ( tablename , <EOL> disease_symptom = { "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT:key>" : "<STR_LIT>" , <EOL> } <EOL> ) <EOL> crud_fields = [ "<STR_LIT>" , <EOL> "<STR_LIT:date>" , <EOL> "<STR_LIT>" , <EOL> S3SQLInlineLink ( "<STR_LIT>" , <EOL> field = "<STR_LIT>" , <EOL> label = T ( "<STR_LIT>" ) , <EOL> multiple = True , <EOL> ) , <EOL> "<STR_LIT>" , <EOL> ] <EOL> configure ( tablename , <EOL> crud_form = S3SQLCustomForm ( * crud_fields ) , <EOL> list_fields = [ "<STR_LIT:date>" , <EOL> "<STR_LIT>" , <EOL> ( T ( "<STR_LIT>" ) , "<STR_LIT>" ) , <EOL> "<STR_LIT>" , <EOL> ] , <EOL> onaccept = self . monitoring_onaccept , <EOL> ) <EOL> crud_strings [ tablename ] = Storage ( <EOL> label_create = T ( "<STR_LIT>" ) , <EOL> title_display = T ( "<STR_LIT>" ) , <EOL> title_list = T ( "<STR_LIT>" ) , <EOL> title_update = T ( "<STR_LIT>" ) , <EOL> title_upload = T ( "<STR_LIT>" ) , <EOL> label_list_button = T ( "<STR_LIT>" ) , <EOL> label_delete_button = T ( "<STR_LIT>" ) , <EOL> msg_record_created = T ( "<STR_LIT>" ) , <EOL> msg_record_modified = T ( "<STR_LIT>" ) , <EOL> msg_record_deleted = T ( "<STR_LIT>" ) , <EOL> msg_list_empty = T ( "<STR_LIT>" ) ) <EOL> tablename = "<STR_LIT>" <EOL> define_table ( tablename , <EOL> Field ( "<STR_LIT>" , "<STR_LIT>" , <EOL> requires = IS_ONE_OF ( db , "<STR_LIT>" ) , <EOL> ) , <EOL> self . disease_symptom_id ( ) , <EOL> * s3_meta_fields ( ) ) <EOL> probe_status = { "<STR_LIT>" : T ( "<STR_LIT>" ) , <EOL> "<STR_LIT>" : T ( "<STR_LIT>" ) , <EOL> "<STR_LIT>" : T ( "<STR_LIT>" ) , <EOL> "<STR_LIT>" : T ( "<STR_LIT>" ) , <EOL> "<STR_LIT>" : T ( "<STR_LIT>" ) , <EOL> } <EOL> tablename = "<STR_LIT>" <EOL> define_table ( tablename , <EOL> case_id ( ) , <EOL> Field ( "<STR_LIT>" ) , <EOL> Field ( "<STR_LIT>" , length = <NUM_LIT:64> , unique = True , <EOL> ) , <EOL> s3_date ( "<STR_LIT>" , <EOL> default = "<STR_LIT>" , <EOL> label = T ( "<STR_LIT>" ) , <EOL> ) , <EOL> Field ( "<STR_LIT>" , <EOL> represent = S3Represent ( options = probe_status ) , <EOL> requires = IS_IN_SET ( probe_status ) , <EOL> default = "<STR_LIT>" , <EOL> ) , <EOL> Field ( "<STR_LIT>" ) , <EOL> Field ( "<STR_LIT:result>" ) , <EOL> s3_date ( "<STR_LIT>" , <EOL> label = T ( "<STR_LIT>" ) , <EOL> ) , <EOL> Field ( "<STR_LIT>" , <EOL> represent = diagnosis_status_represent , <EOL> requires = IS_EMPTY_OR ( <EOL> IS_IN_SET ( diagnosis_status ) ) , <EOL> ) , <EOL> s3_comments ( ) , <EOL> * s3_meta_fields ( ) ) <EOL> crud_strings [ tablename ] = Storage ( <EOL> label_create = T ( "<STR_LIT>" ) , <EOL> title_display = T ( "<STR_LIT>" ) , <EOL> title_list = T ( "<STR_LIT>" ) , <EOL> title_update = T ( "<STR_LIT>" ) , <EOL> title_upload = T ( "<STR_LIT>" ) , <EOL> label_list_button = T ( "<STR_LIT>" ) , <EOL> label_delete_button = T ( "<STR_LIT>" ) , <EOL> msg_record_created = T ( "<STR_LIT>" ) , <EOL> msg_record_modified = T ( "<STR_LIT>" ) , <EOL> msg_record_deleted = T ( "<STR_LIT>" ) , <EOL> msg_list_empty = T ( "<STR_LIT>" ) ) <EOL> return dict ( disease_case_id = case_id ) <EOL> @ staticmethod <EOL> def defaults ( ) : <EOL> """<STR_LIT>""" <EOL> dummy = S3ReusableField ( "<STR_LIT>" , "<STR_LIT>" , <EOL> readable = False , <EOL> writable = False ) <EOL> return dict ( disease_case_id = lambda ** attr : dummy ( "<STR_LIT>" ) , <EOL> ) <EOL> @ staticmethod <EOL> def get_case ( person_id , disease_id ) : <EOL> """<STR_LIT>""" <EOL> ctable = current . s3db . disease_case <EOL> query = ( ctable . person_id == person_id ) & ( ctable . disease_id == disease_id ) & ( ctable . deleted != True ) <EOL> record = current . db ( query ) . select ( ctable . id , <EOL> ctable . case_number , <EOL> limitby = ( <NUM_LIT:0> , <NUM_LIT:1> ) ) . first ( ) <EOL> return record <EOL> @ classmethod <EOL> def case_create_onvalidation ( cls , form ) : <EOL> """<STR_LIT>""" <EOL> formvars = form . vars <EOL> try : <EOL> case_id = formvars . id <EOL> person_id = formvars . person_id <EOL> except AttributeError , e : <EOL> return <EOL> if "<STR_LIT>" not in formvars : <EOL> disease_id = current . s3db . disease_case . disease_id . default <EOL> else : <EOL> disease_id = formvars . disease_id <EOL> record = cls . get_case ( person_id , disease_id ) <EOL> if record and record . id != case_id : <EOL> error = current . T ( "<STR_LIT>" ) <EOL> link = A ( record . case_number , <EOL> _href = URL ( f = "<STR_LIT>" , args = [ record . id ] ) ) <EOL> form . errors . person_id = XML ( "<STR_LIT>" % ( error , link ) ) <EOL> return <EOL> @ staticmethod <EOL> def case_duplicate ( item ) : <EOL> """<STR_LIT>""" <EOL> data = item . data <EOL> case_number = data . get ( "<STR_LIT>" ) <EOL> person_id = data . get ( "<STR_LIT>" ) <EOL> table = item . table <EOL> if case_number : <EOL> query = ( table . case_number == case_number ) & ( table . deleted != True ) <EOL> else : <EOL> disease_id = data . get ( "<STR_LIT>" ) <EOL> if person_id and disease_id : <EOL> query = ( table . disease_id == disease_id ) & ( table . person_id == person_id ) & ( table . deleted != True ) <EOL> else : <EOL> return <EOL> duplicate = current . db ( query ) . select ( table . id , <EOL> table . person_id , <EOL> limitby = ( <NUM_LIT:0> , <NUM_LIT:1> ) ) . first ( ) <EOL> if duplicate : <EOL> item . data . person_id = duplicate . person_id <EOL> item . id = duplicate . id <EOL> item . method = item . METHOD . UPDATE <EOL> return <EOL> @ staticmethod <EOL> def case_onaccept ( form ) : <EOL> """<STR_LIT>""" <EOL> formvars = form . vars <EOL> try : <EOL> record_id = formvars . id <EOL> except AttributeError : <EOL> return <EOL> disease_propagate_case_status ( record_id ) <EOL> return <EOL> @ staticmethod <EOL> def monitoring_onaccept ( form ) : <EOL> """<STR_LIT>""" <EOL> formvars = form . vars <EOL> try : <EOL> record_id = formvars . id <EOL> except AttributeError : <EOL> return <EOL> db = current . db <EOL> s3db = current . s3db <EOL> ctable = s3db . disease_case <EOL> mtable = s3db . disease_case_monitoring <EOL> case_id = None <EOL> if "<STR_LIT>" not in formvars : <EOL> query = ( mtable . id == record_id ) <EOL> row = db ( query ) . select ( mtable . case_id , limitby = ( <NUM_LIT:0> , <NUM_LIT:1> ) ) . first ( ) <EOL> if row : <EOL> case_id = row . case_id <EOL> else : <EOL> case_id = formvars . case_id <EOL> if not case_id : <EOL> return <EOL> query = ( mtable . case_id == case_id ) & ( mtable . illness_status != None ) <EOL> row = db ( query ) . select ( mtable . illness_status , <EOL> orderby = "<STR_LIT>" , <EOL> limitby = ( <NUM_LIT:0> , <NUM_LIT:1> ) ) . first ( ) <EOL> if row : <EOL> db ( ctable . id == case_id ) . update ( illness_status = row . illness_status ) <EOL> disease_propagate_case_status ( case_id ) <EOL> return <EOL> class disease_CaseRepresent ( S3Represent ) : <EOL> def __init__ ( self ) : <EOL> """<STR_LIT>""" <EOL> super ( disease_CaseRepresent , self ) . __init__ ( lookup = "<STR_LIT>" ) <EOL> def lookup_rows ( self , key , values , fields = [ ] ) : <EOL> """<STR_LIT>""" <EOL> s3db = current . s3db <EOL> table = self . table <EOL> ptable = s3db . pr_person <EOL> dtable = s3db . disease_disease <EOL> left = [ ptable . on ( ptable . id == table . person_id ) , <EOL> dtable . on ( dtable . id == table . disease_id ) ] <EOL> if len ( values ) == <NUM_LIT:1> : <EOL> query = ( key == values [ <NUM_LIT:0> ] ) <EOL> else : <EOL> query = key . belongs ( values ) <EOL> rows = current . db ( query ) . select ( table . id , <EOL> table . case_number , <EOL> dtable . name , <EOL> dtable . short_name , <EOL> dtable . acronym , <EOL> ptable . first_name , <EOL> ptable . last_name , <EOL> left = left ) <EOL> self . queries += <NUM_LIT:1> <EOL> return rows <EOL> def represent_row ( self , row ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> case_number = row [ self . tablename ] . case_number <EOL> except AttributeError : <EOL> return row . case_number <EOL> disease_name = None <EOL> try : <EOL> disease = row [ "<STR_LIT>" ] <EOL> except AttributeError : <EOL> pass <EOL> else : <EOL> for field in ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT:name>" ) : <EOL> if field in disease : <EOL> disease_name = disease [ field ] <EOL> if disease_name : <EOL> break <EOL> if disease_name and case_number : <EOL> case = "<STR_LIT>" % ( case_number , disease_name ) <EOL> elif disease_name : <EOL> case = "<STR_LIT>" % disease_name <EOL> else : <EOL> case = case_number <EOL> try : <EOL> person = row [ "<STR_LIT>" ] <EOL> except AttributeError : <EOL> return case <EOL> full_name = s3_fullname ( person ) <EOL> if case : <EOL> return "<STR_LIT:U+0020>" . join ( ( case , full_name ) ) <EOL> else : <EOL> return full_name <EOL> class ContactTracingModel ( S3Model ) : <EOL> names = ( "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ) <EOL> def model ( self ) : <EOL> T = current . T <EOL> db = current . db <EOL> crud_strings = current . response . s3 . crud_strings <EOL> define_table = self . define_table <EOL> case_id = self . disease_case_id <EOL> contact_tracing_status = { <EOL> "<STR_LIT>" : T ( "<STR_LIT>" ) , <EOL> "<STR_LIT>" : T ( "<STR_LIT>" ) , <EOL> } <EOL> tablename = "<STR_LIT>" <EOL> define_table ( tablename , <EOL> case_id ( ) , <EOL> s3_datetime ( "<STR_LIT>" , <EOL> label = T ( "<STR_LIT>" ) , <EOL> set_min = "<STR_LIT>" , <EOL> ) , <EOL> s3_datetime ( "<STR_LIT>" , <EOL> label = T ( "<STR_LIT>" ) , <EOL> set_max = "<STR_LIT>" , <EOL> ) , <EOL> self . gis_location_id ( ) , <EOL> Field ( "<STR_LIT>" , "<STR_LIT:text>" , <EOL> ) , <EOL> Field ( "<STR_LIT:status>" , <EOL> default = "<STR_LIT>" , <EOL> label = T ( "<STR_LIT>" ) , <EOL> requires = IS_IN_SET ( contact_tracing_status , zero = None ) , <EOL> represent = S3Represent ( options = contact_tracing_status ) , <EOL> ) , <EOL> s3_comments ( ) , <EOL> * s3_meta_fields ( ) ) <EOL> represent = S3Represent ( lookup = tablename , fields = [ "<STR_LIT>" ] ) <EOL> tracing_id = S3ReusableField ( "<STR_LIT>" , "<STR_LIT>" % tablename , <EOL> label = T ( "<STR_LIT>" ) , <EOL> represent = represent , <EOL> requires = IS_EMPTY_OR ( <EOL> IS_ONE_OF ( db , "<STR_LIT>" , <EOL> represent , <EOL> ) ) , <EOL> sortby = "<STR_LIT:date>" , <EOL> comment = S3PopupLink ( f = "<STR_LIT>" , <EOL> tooltip = T ( "<STR_LIT>" ) , <EOL> ) , <EOL> ) <EOL> self . add_components ( tablename , <EOL> disease_exposure = "<STR_LIT>" , <EOL> ) <EOL> crud_strings [ tablename ] = Storage ( <EOL> label_create = T ( "<STR_LIT>" ) , <EOL> title_display = T ( "<STR_LIT>" ) , <EOL> title_list = T ( "<STR_LIT>" ) , <EOL> title_update = T ( "<STR_LIT>" ) , <EOL> title_upload = T ( "<STR_LIT>" ) , <EOL> label_list_button = T ( "<STR_LIT>" ) , <EOL> label_delete_button = T ( "<STR_LIT>" ) , <EOL> msg_record_created = T ( "<STR_LIT>" ) , <EOL> msg_record_modified = T ( "<STR_LIT>" ) , <EOL> msg_record_deleted = T ( "<STR_LIT>" ) , <EOL> msg_list_empty = T ( "<STR_LIT>" ) ) <EOL> protection_level = { "<STR_LIT>" : T ( "<STR_LIT>" ) , <EOL> "<STR_LIT>" : T ( "<STR_LIT>" ) , <EOL> "<STR_LIT>" : T ( "<STR_LIT>" ) , <EOL> } <EOL> protection_level_represent = S3Represent ( options = protection_level ) <EOL> exposure_type = { "<STR_LIT>" : T ( "<STR_LIT>" ) , <EOL> "<STR_LIT>" : T ( "<STR_LIT>" ) , <EOL> "<STR_LIT>" : T ( "<STR_LIT>" ) , <EOL> } <EOL> exposure_type_represent = S3Represent ( options = exposure_type ) <EOL> exposure_risk = { "<STR_LIT>" : T ( "<STR_LIT>" ) , <EOL> "<STR_LIT>" : T ( "<STR_LIT>" ) , <EOL> "<STR_LIT>" : T ( "<STR_LIT>" ) , <EOL> "<STR_LIT>" : T ( "<STR_LIT>" ) , <EOL> } <EOL> exposure_risk_represent = S3Represent ( options = exposure_risk ) <EOL> tablename = "<STR_LIT>" <EOL> define_table ( tablename , <EOL> case_id ( ) , <EOL> tracing_id ( ) , <EOL> self . pr_person_id ( requires = IS_ADD_PERSON_WIDGET2 ( ) , <EOL> widget = S3AddPersonWidget2 ( controller = "<STR_LIT>" ) , <EOL> ) , <EOL> s3_datetime ( ) , <EOL> Field ( "<STR_LIT>" , <EOL> default = "<STR_LIT>" , <EOL> represent = exposure_type_represent , <EOL> requires = IS_IN_SET ( exposure_type , zero = None ) , <EOL> ) , <EOL> Field ( "<STR_LIT>" , <EOL> default = "<STR_LIT>" , <EOL> represent = protection_level_represent , <EOL> requires = IS_IN_SET ( protection_level , zero = None ) , <EOL> ) , <EOL> Field ( "<STR_LIT>" , <EOL> default = "<STR_LIT>" , <EOL> represent = exposure_risk_represent , <EOL> requires = IS_IN_SET ( exposure_risk , zero = None ) , <EOL> ) , <EOL> Field ( "<STR_LIT>" , "<STR_LIT:text>" ) , <EOL> * s3_meta_fields ( ) ) <EOL> self . configure ( tablename , <EOL> onaccept = self . exposure_onaccept , <EOL> ) <EOL> crud_strings [ tablename ] = Storage ( <EOL> label_create = T ( "<STR_LIT>" ) , <EOL> title_display = T ( "<STR_LIT>" ) , <EOL> title_list = T ( "<STR_LIT>" ) , <EOL> title_update = T ( "<STR_LIT>" ) , <EOL> title_upload = T ( "<STR_LIT>" ) , <EOL> label_list_button = T ( "<STR_LIT>" ) , <EOL> label_delete_button = T ( "<STR_LIT>" ) , <EOL> msg_record_created = T ( "<STR_LIT>" ) , <EOL> msg_record_modified = T ( "<STR_LIT>" ) , <EOL> msg_record_deleted = T ( "<STR_LIT>" ) , <EOL> msg_list_empty = T ( "<STR_LIT>" ) ) <EOL> return { } <EOL> @ staticmethod <EOL> def defaults ( ) : <EOL> return { } <EOL> @ staticmethod <EOL> def exposure_onaccept ( form ) : <EOL> """<STR_LIT>""" <EOL> formvars = form . vars <EOL> try : <EOL> record_id = formvars . id <EOL> except AttributeError : <EOL> return <EOL> db = current . db <EOL> s3db = current . s3db <EOL> if "<STR_LIT>" not in formvars : <EOL> etable = s3db . disease_exposure <EOL> row = db ( etable . id == record_id ) . select ( etable . case_id , <EOL> limitby = ( <NUM_LIT:0> , <NUM_LIT:1> ) ) . first ( ) <EOL> if not row : <EOL> return <EOL> case_id = row . case_id <EOL> else : <EOL> case_id = formvars . case_id <EOL> disease_propagate_case_status ( case_id ) <EOL> return <EOL> def disease_propagate_case_status ( case_id ) : <EOL> """<STR_LIT>""" <EOL> db = current . db <EOL> s3db = current . s3db <EOL> risk_status = ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) <EOL> ctable = s3db . disease_case <EOL> query = ( ctable . id == case_id ) & ( ctable . deleted != True ) <EOL> case = db ( query ) . select ( ctable . id , <EOL> ctable . created_on , <EOL> ctable . disease_id , <EOL> ctable . illness_status , <EOL> ctable . symptom_debut , <EOL> ctable . diagnosis_status , <EOL> ctable . diagnosis_date , <EOL> limitby = ( <NUM_LIT:0> , <NUM_LIT:1> ) ) . first ( ) <EOL> if case is None : <EOL> return <EOL> disease_id = case . disease_id <EOL> symptom_debut = case . symptom_debut <EOL> if not symptom_debut : <EOL> mtable = s3db . disease_case_monitoring <EOL> query = ( mtable . case_id == case_id ) & ( mtable . illness_status . belongs ( risk_status ) ) & ( mtable . deleted != True ) <EOL> monitoring = db ( query ) . select ( mtable . date , <EOL> orderby = "<STR_LIT>" , <EOL> limitby = ( <NUM_LIT:0> , <NUM_LIT:1> ) ) . first ( ) <EOL> if monitoring : <EOL> symptom_debut = monitoring . date <EOL> if not symptom_debut and case . illness_status in risk_status : <EOL> symptom_debut = case . created_on <EOL> if not symptom_debut : <EOL> return <EOL> if case . diagnosis_status == "<STR_LIT>" and case . diagnosis_date > symptom_debut : <EOL> return <EOL> dtable = s3db . disease_disease <EOL> query = ( dtable . id == disease_id ) & ( dtable . deleted != True ) <EOL> disease = db ( query ) . select ( dtable . trace_period , <EOL> dtable . watch_period , <EOL> limitby = ( <NUM_LIT:0> , <NUM_LIT:1> ) ) . first ( ) <EOL> if not disease : <EOL> return <EOL> trace_period = disease . trace_period <EOL> if trace_period : <EOL> risk_period_start = symptom_debut - datetime . timedelta ( days = disease . trace_period ) <EOL> else : <EOL> risk_period_start = symptom_debut <EOL> etable = s3db . disease_exposure <EOL> query = ( etable . case_id == case_id ) & ( etable . date >= risk_period_start ) & ( etable . exposure_risk == "<STR_LIT>" ) & ( etable . deleted != True ) <EOL> exposures = db ( query ) . select ( etable . person_id ) <EOL> for exposure in exposures : <EOL> disease_create_case ( disease_id , <EOL> exposure . person_id , <EOL> monitoring_level = "<STR_LIT>" , <EOL> ) <EOL> return <EOL> def disease_create_case ( disease_id , person_id , monitoring_level = None ) : <EOL> """<STR_LIT>""" <EOL> ctable = current . s3db . disease_case <EOL> query = ( ctable . person_id == person_id ) & ( ctable . disease_id == disease_id ) & ( ctable . deleted != True ) <EOL> case = current . db ( query ) . select ( ctable . id , <EOL> ctable . monitoring_level , <EOL> limitby = ( <NUM_LIT:0> , <NUM_LIT:1> ) ) . first ( ) <EOL> if case : <EOL> case_id = case . id <EOL> if monitoring_level is not None : <EOL> disease_upgrade_monitoring ( case_id , <EOL> monitoring_level , <EOL> case = case , <EOL> ) <EOL> else : <EOL> case_id = ctable . insert ( disease_id = disease_id , <EOL> person_id = person_id , <EOL> monitoring_level = monitoring_level , <EOL> ) <EOL> return case_id <EOL> def disease_upgrade_monitoring ( case_id , level , case = None ) : <EOL> """<STR_LIT>""" <EOL> if level not in MONITORING_UPGRADE : <EOL> return False <EOL> else : <EOL> previous_levels = MONITORING_UPGRADE [ level ] <EOL> if case is None or "<STR_LIT>" not in case : <EOL> ctable = current . s3db . disease_case <EOL> query = ( ctable . id == case_id ) & ( ctable . monitoring_level . belongs ( previous_levels ) ) & ( ctable . deleted != True ) <EOL> case = current . db ( query ) . select ( ctable . id , <EOL> limitby = ( <NUM_LIT:0> , <NUM_LIT:1> ) ) . first ( ) <EOL> elif case . monitoring_level not in previous_levels : <EOL> return <EOL> if case : <EOL> case . update_record ( monitoring_level = level ) <EOL> return True <EOL> class DiseaseStatsModel ( S3Model ) : <EOL> """<STR_LIT>""" <EOL> names = ( "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ) <EOL> def model ( self ) : <EOL> T = current . T <EOL> db = current . db <EOL> configure = self . configure <EOL> crud_strings = current . response . s3 . crud_strings <EOL> define_table = self . define_table <EOL> super_link = self . super_link <EOL> location_id = self . gis_location_id <EOL> stats_parameter_represent = S3Represent ( lookup = "<STR_LIT>" , <EOL> translate = True ) <EOL> tablename = "<STR_LIT>" <EOL> define_table ( tablename , <EOL> super_link ( "<STR_LIT>" , "<STR_LIT>" ) , <EOL> Field ( "<STR_LIT:name>" , <EOL> label = T ( "<STR_LIT:Name>" ) , <EOL> requires = IS_NOT_EMPTY ( ) , <EOL> represent = lambda v : T ( v ) if v is not None else NONE , <EOL> ) , <EOL> s3_comments ( "<STR_LIT:description>" , <EOL> label = T ( "<STR_LIT>" ) , <EOL> ) , <EOL> * s3_meta_fields ( ) <EOL> ) <EOL> ADD_STATISTIC = T ( "<STR_LIT>" ) <EOL> crud_strings [ tablename ] = Storage ( <EOL> label_create = ADD_STATISTIC , <EOL> title_display = T ( "<STR_LIT>" ) , <EOL> title_list = T ( "<STR_LIT>" ) , <EOL> title_update = T ( "<STR_LIT>" ) , <EOL> label_list_button = T ( "<STR_LIT>" ) , <EOL> msg_record_created = T ( "<STR_LIT>" ) , <EOL> msg_record_modified = T ( "<STR_LIT>" ) , <EOL> msg_record_deleted = T ( "<STR_LIT>" ) , <EOL> msg_list_empty = T ( "<STR_LIT>" ) ) <EOL> configure ( tablename , <EOL> deduplicate = S3Duplicate ( ) , <EOL> super_entity = "<STR_LIT>" , <EOL> ) <EOL> tablename = "<STR_LIT>" <EOL> define_table ( tablename , <EOL> super_link ( "<STR_LIT>" , "<STR_LIT>" ) , <EOL> super_link ( "<STR_LIT>" , "<STR_LIT>" , <EOL> instance_types = ( "<STR_LIT>" , ) , <EOL> label = T ( "<STR_LIT>" ) , <EOL> represent = stats_parameter_represent , <EOL> readable = True , <EOL> writable = True , <EOL> empty = False , <EOL> comment = S3PopupLink ( c = "<STR_LIT>" , <EOL> f = "<STR_LIT>" , <EOL> vars = { "<STR_LIT>" : "<STR_LIT>" } , <EOL> title = ADD_STATISTIC , <EOL> ) , <EOL> ) , <EOL> location_id ( <EOL> requires = IS_LOCATION ( ) , <EOL> widget = S3LocationAutocompleteWidget ( ) , <EOL> ) , <EOL> Field ( "<STR_LIT:value>" , "<STR_LIT>" , <EOL> label = T ( "<STR_LIT>" ) , <EOL> represent = lambda v : IS_FLOAT_AMOUNT . represent ( v , precision = <NUM_LIT:2> ) , <EOL> requires = IS_NOT_EMPTY ( ) , <EOL> ) , <EOL> s3_date ( empty = False ) , <EOL> self . stats_source_id ( ) , <EOL> s3_comments ( ) , <EOL> * s3_meta_fields ( ) <EOL> ) <EOL> crud_strings [ tablename ] = Storage ( <EOL> label_create = T ( "<STR_LIT>" ) , <EOL> title_display = T ( "<STR_LIT>" ) , <EOL> title_list = T ( "<STR_LIT>" ) , <EOL> title_update = T ( "<STR_LIT>" ) , <EOL> title_upload = T ( "<STR_LIT>" ) , <EOL> label_list_button = T ( "<STR_LIT>" ) , <EOL> msg_record_created = T ( "<STR_LIT>" ) , <EOL> msg_record_modified = T ( "<STR_LIT>" ) , <EOL> msg_record_deleted = T ( "<STR_LIT>" ) , <EOL> msg_list_empty = T ( "<STR_LIT>" ) ) <EOL> levels = current . gis . get_relevant_hierarchy_levels ( ) <EOL> location_fields = [ "<STR_LIT>" % level for level in levels ] <EOL> list_fields = [ "<STR_LIT>" ] <EOL> list_fields . extend ( location_fields ) <EOL> list_fields . extend ( ( ( "<STR_LIT:value>" , <EOL> "<STR_LIT:date>" , <EOL> "<STR_LIT>" , <EOL> ) ) ) <EOL> filter_widgets = [ S3OptionsFilter ( "<STR_LIT>" , <EOL> label = T ( "<STR_LIT>" ) , <EOL> multiple = False , <EOL> ) , <EOL> S3OptionsFilter ( "<STR_LIT>" , <EOL> label = T ( "<STR_LIT>" ) , <EOL> multiple = False , <EOL> ) , <EOL> S3LocationFilter ( "<STR_LIT>" , <EOL> levels = levels , <EOL> ) , <EOL> ] <EOL> report_options = Storage ( rows = location_fields , <EOL> cols = [ "<STR_LIT>" ] , <EOL> fact = [ ( T ( "<STR_LIT>" ) , "<STR_LIT>" ) , <EOL> ] , <EOL> defaults = Storage ( rows = location_fields [ <NUM_LIT:0> ] , <EOL> cols = "<STR_LIT>" , <EOL> fact = "<STR_LIT>" , <EOL> totals = True , <EOL> chart = "<STR_LIT>" , <EOL> table = "<STR_LIT>" , <EOL> ) <EOL> ) <EOL> configure ( tablename , <EOL> deduplicate = S3Duplicate ( primary = ( "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT:date>" , <EOL> ) , <EOL> ) , <EOL> filter_widgets = filter_widgets , <EOL> list_fields = list_fields , <EOL> report_options = report_options , <EOL> super_entity = "<STR_LIT>" , <EOL> timeplot_options = { "<STR_LIT>" : { "<STR_LIT>" : "<STR_LIT:date>" , <EOL> "<STR_LIT>" : "<STR_LIT:date>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> } , <EOL> } , <EOL> ) <EOL> aggregate_types = { <NUM_LIT:1> : T ( "<STR_LIT>" ) , <EOL> <NUM_LIT:2> : T ( "<STR_LIT>" ) , <EOL> } <EOL> tablename = "<STR_LIT>" <EOL> define_table ( tablename , <EOL> super_link ( "<STR_LIT>" , "<STR_LIT>" , <EOL> empty = False , <EOL> instance_types = ( "<STR_LIT>" , ) , <EOL> label = T ( "<STR_LIT>" ) , <EOL> represent = S3Represent ( lookup = "<STR_LIT>" ) , <EOL> readable = True , <EOL> writable = True , <EOL> ) , <EOL> location_id ( <EOL> requires = IS_LOCATION ( ) , <EOL> widget = S3LocationAutocompleteWidget ( ) , <EOL> ) , <EOL> Field ( "<STR_LIT>" , "<STR_LIT>" , <EOL> default = <NUM_LIT:1> , <EOL> label = T ( "<STR_LIT>" ) , <EOL> represent = lambda opt : aggregate_types . get ( opt , <EOL> current . messages . UNKNOWN_OPT ) , <EOL> requires = IS_IN_SET ( aggregate_types ) , <EOL> ) , <EOL> s3_date ( "<STR_LIT:date>" , <EOL> label = T ( "<STR_LIT>" ) , <EOL> ) , <EOL> Field ( "<STR_LIT>" , "<STR_LIT>" , <EOL> label = T ( "<STR_LIT>" ) , <EOL> represent = lambda v : IS_FLOAT_AMOUNT . represent ( v , precision = <NUM_LIT:2> ) , <EOL> ) , <EOL> * s3_meta_fields ( ) <EOL> ) <EOL> return dict ( <EOL> disease_stats_rebuild_all_aggregates = self . disease_stats_rebuild_all_aggregates , <EOL> disease_stats_update_aggregates = self . disease_stats_update_aggregates , <EOL> disease_stats_update_location_aggregates = self . disease_stats_update_location_aggregates , <EOL> ) <EOL> @ staticmethod <EOL> def disease_stats_rebuild_all_aggregates ( ) : <EOL> """<STR_LIT>""" <EOL> db = current . db <EOL> ttable = db . scheduler_task <EOL> rtable = db . scheduler_run <EOL> wtable = db . scheduler_worker <EOL> query = ( ttable . task_name == "<STR_LIT>" ) & ( rtable . task_id == ttable . id ) & ( rtable . status == "<STR_LIT>" ) <EOL> rows = db ( query ) . select ( rtable . id , <EOL> rtable . task_id , <EOL> rtable . worker_name ) <EOL> now = current . request . utcnow <EOL> for row in rows : <EOL> db ( wtable . worker_name == row . worker_name ) . update ( status = "<STR_LIT>" ) <EOL> db ( rtable . id == row . id ) . update ( stop_time = now , <EOL> status = "<STR_LIT>" ) <EOL> db ( ttable . id == row . task_id ) . update ( stop_time = now , <EOL> status = "<STR_LIT>" ) <EOL> current . s3db . disease_stats_aggregate . truncate ( ) <EOL> dtable = db . disease_stats_data <EOL> query = ( dtable . deleted != True ) <EOL> records = db ( query ) . select ( dtable . parameter_id , <EOL> dtable . date , <EOL> dtable . value , <EOL> dtable . location_id , <EOL> ) <EOL> current . s3task . async ( "<STR_LIT>" , <EOL> vars = dict ( records = records . json ( ) , all = True ) , <EOL> timeout = <NUM_LIT> <EOL> ) <EOL> @ staticmethod <EOL> def disease_stats_update_aggregates ( records = None , all = False ) : <EOL> """<STR_LIT>""" <EOL> if not records : <EOL> return <EOL> if isinstance ( records , basestring ) : <EOL> from_json = True <EOL> from dateutil . parser import parse <EOL> records = json . loads ( records ) <EOL> elif isinstance ( records [ <NUM_LIT:0> ] [ "<STR_LIT:date>" ] , <EOL> ( datetime . date , datetime . datetime ) ) : <EOL> from_json = False <EOL> else : <EOL> from_json = True <EOL> from dateutil . parser import parse <EOL> db = current . db <EOL> s3db = current . s3db <EOL> atable = db . disease_stats_aggregate <EOL> if not all : <EOL> return <EOL> dtable = s3db . disease_stats_data <EOL> now = current . request . now <EOL> earliest_period = now . date ( ) <EOL> locations = { } <EOL> parameters = [ ] <EOL> pappend = parameters . append <EOL> for record in records : <EOL> location_id = record [ "<STR_LIT>" ] <EOL> if location_id not in locations : <EOL> locations [ location_id ] = { } <EOL> parameter_id = record [ "<STR_LIT>" ] <EOL> if parameter_id not in parameters : <EOL> pappend ( parameter_id ) <EOL> if parameter_id not in locations [ location_id ] : <EOL> locations [ location_id ] [ parameter_id ] = { } <EOL> if from_json : <EOL> date = parse ( record [ "<STR_LIT:date>" ] ) <EOL> date = date . date ( ) <EOL> else : <EOL> date = record [ "<STR_LIT:date>" ] <EOL> if date < earliest_period : <EOL> earliest_period = date <EOL> locations [ location_id ] [ parameter_id ] [ date ] = record [ "<STR_LIT:value>" ] <EOL> from dateutil . rrule import rrule , DAILY <EOL> dates = rrule ( DAILY , dtstart = earliest_period , until = now ) <EOL> dates = [ d . date ( ) for d in dates ] <EOL> insert = atable . insert <EOL> lfield = atable . location_id <EOL> pfield = atable . parameter_id <EOL> dfield = atable . date <EOL> ifield = atable . id <EOL> _q = ( atable . agg_type == <NUM_LIT:1> ) <EOL> for location_id in locations : <EOL> location = locations [ location_id ] <EOL> query = _q & ( lfield == location_id ) <EOL> for parameter_id in location : <EOL> parameter = location [ parameter_id ] <EOL> q = query & ( pfield == parameter_id ) <EOL> for d in dates : <EOL> values = [ ] <EOL> vappend = values . append <EOL> for date in parameter : <EOL> if date <= d : <EOL> vappend ( parameter [ date ] ) <EOL> values_sum = sum ( values ) <EOL> exists = db ( q & ( dfield == d ) ) . select ( ifield , <EOL> limitby = ( <NUM_LIT:0> , <NUM_LIT:1> ) ) <EOL> if exists : <EOL> db ( ifield == exists . first ( ) . id ) . update ( sum = values_sum ) <EOL> else : <EOL> insert ( agg_type = <NUM_LIT:1> , <EOL> location_id = location_id , <EOL> parameter_id = parameter_id , <EOL> date = d , <EOL> sum = values_sum , <EOL> ) <EOL> gtable = db . gis_location <EOL> ifield = gtable . id <EOL> location_ids = locations . keys ( ) <EOL> paths = db ( ifield . belongs ( location_ids ) ) . select ( gtable . path ) <EOL> paths = [ p . path . split ( "<STR_LIT:/>" ) for p in paths ] <EOL> import itertools <EOL> ancestors = tuple ( itertools . chain . from_iterable ( paths ) ) <EOL> ancestors = [ a for a in ancestors if a not in location_ids ] <EOL> pfield = gtable . parent <EOL> query = ( gtable . deleted == False ) & ( pfield . belongs ( ancestors ) ) <EOL> all_children = db ( query ) . select ( ifield , <EOL> pfield ) <EOL> rows = db ( ifield . belongs ( ancestors ) ) . select ( ifield , <EOL> gtable . level ) <EOL> L0 = [ ] <EOL> L0_append = L0 . append <EOL> L1 = [ ] <EOL> L1_append = L1 . append <EOL> L2 = [ ] <EOL> L2_append = L2 . append <EOL> L3 = [ ] <EOL> L3_append = L3 . append <EOL> L4 = [ ] <EOL> L4_append = L4 . append <EOL> for row in rows : <EOL> if row . level == "<STR_LIT>" : <EOL> L0_append ( row . id ) <EOL> elif row . level == "<STR_LIT>" : <EOL> L1_append ( row . id ) <EOL> elif row . level == "<STR_LIT>" : <EOL> L2_append ( row . id ) <EOL> elif row . level == "<STR_LIT>" : <EOL> L3_append ( row . id ) <EOL> elif row . level == "<STR_LIT>" : <EOL> L4_append ( row . id ) <EOL> async = current . s3task . async <EOL> from gluon . serializers import json as jsons <EOL> dates = jsons ( dates ) <EOL> for level in ( L4 , L3 , L2 , L1 ) : <EOL> for location_id in level : <EOL> children = [ c . id for c in all_children if c . parent == location_id ] <EOL> children = json . dumps ( children ) <EOL> for parameter_id in parameters : <EOL> async ( "<STR_LIT>" , <EOL> args = [ location_id , children , parameter_id , dates ] , <EOL> timeout = <NUM_LIT> <EOL> ) <EOL> @ staticmethod <EOL> def disease_stats_update_location_aggregates ( location_id , <EOL> children , <EOL> parameter_id , <EOL> dates , <EOL> ) : <EOL> """<STR_LIT>""" <EOL> db = current . db <EOL> atable = current . s3db . disease_stats_aggregate <EOL> ifield = atable . id <EOL> lfield = atable . location_id <EOL> pfield = atable . parameter_id <EOL> dfield = atable . date <EOL> children = json . loads ( children ) <EOL> query = ( pfield == parameter_id ) & ( atable . deleted != True ) & ( lfield . belongs ( children ) ) <EOL> rows = db ( query ) . select ( atable . sum , <EOL> dfield , <EOL> lfield , <EOL> orderby = ( lfield , ~ dfield ) , <EOL> ) <EOL> if not rows : <EOL> return <EOL> query = ( lfield == location_id ) & ( pfield == parameter_id ) <EOL> existing = db ( query ) . select ( ifield , <EOL> dfield , <EOL> ) <EOL> exists = { } <EOL> for e in existing : <EOL> exists [ e . date ] = e . id <EOL> from dateutil . parser import parse <EOL> dates = json . loads ( dates ) <EOL> insert = atable . insert <EOL> for date in dates : <EOL> date = parse ( date ) <EOL> date = date . date ( ) <EOL> last_location = None <EOL> values = [ ] <EOL> vappend = values . append <EOL> for row in rows : <EOL> if date < row . date : <EOL> continue <EOL> new_location_id = row . location_id <EOL> if new_location_id != last_location : <EOL> last_location = new_location_id <EOL> vappend ( row . sum ) <EOL> if values : <EOL> values_sum = sum ( values ) <EOL> else : <EOL> values_sum = <NUM_LIT:0> <EOL> attr = dict ( agg_type = <NUM_LIT:2> , <EOL> sum = values_sum , <EOL> ) <EOL> if date in exists : <EOL> db ( ifield == exists [ date ] ) . update ( ** attr ) <EOL> else : <EOL> insert ( parameter_id = parameter_id , <EOL> location_id = location_id , <EOL> date = date , <EOL> ** attr <EOL> ) <EOL> def disease_rheader ( r , tabs = None ) : <EOL> """<STR_LIT>""" <EOL> T = current . T <EOL> if r . representation != "<STR_LIT:html>" : <EOL> return None <EOL> resourcename = r . name <EOL> if resourcename == "<STR_LIT>" : <EOL> tabs = ( ( T ( "<STR_LIT>" ) , None ) , <EOL> ( T ( "<STR_LIT>" ) , "<STR_LIT>" ) , <EOL> ( T ( "<STR_LIT>" ) , "<STR_LIT>" ) , <EOL> ) <EOL> rheader_fields = ( [ "<STR_LIT:name>" ] , <EOL> [ "<STR_LIT:code>" ] , <EOL> ) <EOL> rheader = S3ResourceHeader ( rheader_fields , tabs ) ( r ) <EOL> elif resourcename == "<STR_LIT>" : <EOL> tabs = ( ( T ( "<STR_LIT>" ) , None ) , <EOL> ( T ( "<STR_LIT>" ) , "<STR_LIT>" ) , <EOL> ( T ( "<STR_LIT>" ) , "<STR_LIT>" ) , <EOL> ( T ( "<STR_LIT>" ) , "<STR_LIT>" ) , <EOL> ( T ( "<STR_LIT>" ) , "<STR_LIT>" ) , <EOL> ( T ( "<STR_LIT>" ) , "<STR_LIT>" ) , <EOL> ) <EOL> rheader_fields = ( [ "<STR_LIT>" ] , <EOL> ) <EOL> rheader = S3ResourceHeader ( rheader_fields , tabs ) ( r ) <EOL> elif resourcename == "<STR_LIT>" : <EOL> tabs = ( ( T ( "<STR_LIT>" ) , None ) , <EOL> ( T ( "<STR_LIT>" ) , "<STR_LIT>" ) , <EOL> ) <EOL> rheader_fields = ( [ "<STR_LIT>" ] , <EOL> ) <EOL> rheader = S3ResourceHeader ( rheader_fields , tabs ) ( r ) <EOL> else : <EOL> rheader = "<STR_LIT>" <EOL> return rheader </s>
