예제 #1
0
    def get_tables(self):
        cnds    = self.navb.conditions()
        cnds.update({"(birth_date) <= '%s'" % (self.navb.finish) : ''})
        cnds.update({"(birth_date) >= '%s'" % (self.navb.start) : ''})
        
        cnds, markup, cols = self.navb.neater_tables(cnds = cnds, extras = [
                                              ('national_id',            'Mother ID'),
                                              ('user_phone',            'Reporter Phone'),
                                              ('birth_date', 'Birth date'),
                                              ('child_weight', 'Weight'),
                                              ('recent_muac', 'Muac'),
                                              ('created_at', 'Submission Date'),
                                              ('indexcol', 'ID')
                                            ])

        markup.update({'indexcol': lambda x, _, __: '<a href="/dashboards/report?tbl=bir&id=%s">View</a>' % (x), })
        
        cnds.update({queries.DELIVERY_DATA['query_str']: ''})
        INDICS = [('all', 'total', 'Total')] + [(makecol(x[0]), x[0], x[1]) for x in queries.DELIVERY_DATA['attrs']]
        INDICSDICT = {x[0]: (x[1], x[2]) for x in INDICS}

        title, sc, group, attrs, nat, tabular, locateds, INDICS_HEADERS = ('', '', '', [], [], [],[],[])

        if self.navb.kw.get('subcat') is not None:
            sc = self.navb.kw.get('subcat')
            dcols = [x[0] for x in cols]
            wcl = INDICSDICT.get(sc)#; print wcl, PREDICT
            INDICS = [(sc, wcl[0], wcl[1])] if wcl else []
            INDICS_HEADERS = dict([ ( makecol(x[0]), x[2]) for x in INDICS])
            if wcl and wcl[0] != 'total':   cnds.update({wcl[0]: ''})
            nat = Birth.fetch_log_births(cnds, dcols)            
            #print wcl, INDICSDICT, INDICS
        else:
            dcols   = [x[0] for x in cols]
            nat = Birth.fetch_log_births(cnds, dcols)

        if self.navb.kw.get('view') == 'table' or self.navb.kw.get('view') != 'log' :
            group_by = []
            group_by += ['province_pk']  if self.navb.kw.get('nation') or not group_by else []
            group_by += ['district_pk'] if self.navb.kw.get('province') else []
            group_by += ['referral_facility_pk'] if self.navb.kw.get('district') else []
            group_by += ['facility_pk'] if self.navb.kw.get('hd') else [] 
            #print "\nGROUP BY: %s \n" % group_by
            LOCS = {'nation': self.navb.kw.get('nation'),
                    'province': self.navb.kw.get('province'),
                    'district': self.navb.kw.get('district'),
                    'hospital': self.navb.kw.get('hd'),
                    'location': self.navb.kw.get('hc')
                    }
                        
            locateds = Birth.fetch_births_by_location(cnds, group_by = group_by, INDICS = INDICS)
            #print [[y.__dict__ for y in x] for x in locateds]#, INDICS, LOCS, self.navb.locs()
            tabular = give_me_table(locateds, self.navb.locs(),  INDICS = INDICS, LOCS = LOCS)
            #print locateds, "\n", tabular

        INDICS_HEADERS = dict([ ( makecol(x[0]), x[2]) for x in INDICS])
        desc  = 'Deliveries%s' % (' (%s)' % ( self.navb.find_descr( [(makecol(x[0]), x[2]) for x in INDICS], sc ) ) )
 
        #print INDICS_HEADERS, tabular, locateds
        return (title, desc, group, attrs, markup, cols, nat, tabular, locateds, INDICS_HEADERS)
예제 #2
0
    def get_tables(self):
        #print "KW: ", self.navb.kw
        cnds = self.navb.conditions()
        cnds.update({"(anc_date) <= '%s'" % (self.navb.finish): ''})
        cnds.update({
            "(lmp + INTERVAL \'%d days\') >= '%s'" % (GESTATION, self.navb.start):
            ''
        })

        precnds = self.navb.conditions()
        precnds.update({"(created_at) <= '%s'" % (self.navb.finish): ''})
        precnds.update({
            "(lmp + INTERVAL \'%d days\') >= '%s'" % (GESTATION, self.navb.start):
            ''
        })  #

        cnds, markup, cols = self.navb.neater_tables(
            cnds=cnds,
            extras=[('indexcol', 'ID'), ('national_id', 'Mother ID'),
                    ('user_phone', 'Reporter Phone'), ('lmp', 'LMP'),
                    ('mother_weight', 'Weight'), ('muac', 'Muac'),
                    ('created_at', 'Submission Date')])

        markup.update({
            'indexcol':
            lambda x, _, __:
            '<a href="/dashboards/report?tbl=anc&id=%s">View</a>' % (x),
        })

        INDICS = [(makecol(x[0]), x[0], x[1])
                  for x in queries.ANC_DATA['attrs']]
        PRE_INDICS = [('anc1', 'total', 'ANC1'),
                      ('stdanc1', '(lmp + INTERVAL \'90 days\') <= created_at',
                       'Standard ANC1')]
        ANCDICT = makedict(queries.ANC_DATA['attrs'])
        PREDICT = {x[0]: (x[1], x[2]) for x in PRE_INDICS}

        title, sc, group, attrs, nat, tabular, locateds, INDICS_HEADERS = (
            '', '', '', [], [], [], [], [])

        if self.navb.kw.get('subcat') is not None:
            sc = self.navb.kw.get('subcat')
            dcols = [x[0] for x in cols]
            if self.navb.kw.get('subcat') in [x[0] for x in PRE_INDICS]:
                wcl = PREDICT.get(sc)  #; print wcl, PREDICT
                INDICS = [(sc, wcl[0], wcl[1])] if wcl else []
                INDICS_HEADERS = dict([(makecol(x[0]), x[2])
                                       for x in PRE_INDICS])
                if wcl and wcl[0] != 'total': precnds.update({wcl[0]: ''})
                nat = Pregnancy.fetch_log_pregnancies(precnds, dcols)
            else:
                wcl = ANCDICT.get(sc)
                INDICS = [(sc, wcl[0], wcl[1])] if wcl else []
                INDICS_HEADERS = dict([(makecol(x[0]), x[2]) for x in INDICS])
                cnds.update({wcl[0]: ''})
                nat = Ancvisit.fetch_log_ancvisits(cnds, dcols)
            #;print  "INDICS: ", INDICS
            #print wcl, ANCDICT, INDICS
        else:
            dcols = [x[0] for x in cols]
            #nat     = Ancvisit.fetch_log_ancvisits(cnds, dcols)
            nat = Pregnancy.fetch_log_pregnancies(precnds, dcols)

        if self.navb.kw.get(
                'view') == 'table' or self.navb.kw.get('view') != 'log':
            group_by = []
            group_by += [
                'province_pk'
            ] if self.navb.kw.get('nation') or not group_by else []
            group_by += ['district_pk'] if self.navb.kw.get('province') else []
            group_by += ['referral_facility_pk'
                         ] if self.navb.kw.get('district') else []
            group_by += ['facility_pk'] if self.navb.kw.get('hd') else []
            #print "\nGROUP BY: %s \n" % group_by
            LOCS = {
                'nation': self.navb.kw.get('nation'),
                'province': self.navb.kw.get('province'),
                'district': self.navb.kw.get('district'),
                'hospital': self.navb.kw.get('hd'),
                'location': self.navb.kw.get('hc')
            }

            if self.navb.kw.get('subcat') in [x[0] for x in PRE_INDICS]:
                #print PRE_INDICS, LOCS, precnds, group_by
                locateds = Pregnancy.fetch_pregnancies_by_location(
                    precnds, group_by=group_by, INDICS=PRE_INDICS)
            elif self.navb.kw.get('subcat') in [x[0] for x in INDICS]:
                #print INDICS, LOCS, cnds, group_by
                locateds = Ancvisit.fetch_ancvisits_by_location(
                    cnds, group_by=group_by, INDICS=INDICS)
            else:
                INDICS = PRE_INDICS + INDICS
                locateds = Ancvisit.fetch_ancvisits_by_location(
                    cnds, group_by=group_by, INDICS=INDICS)
                locateds += Pregnancy.fetch_pregnancies_by_location(
                    precnds, group_by=group_by, INDICS=PRE_INDICS)
                #print [[y.__dict__ for y in x] for x in locateds]#, INDICS, LOCS, self.navb.locs()
            tabular = give_me_table(locateds,
                                    self.navb.locs(),
                                    INDICS=INDICS,
                                    LOCS=LOCS)
            #print locateds, "\n", tabular

        INDICS_HEADERS = dict([(makecol(x[0]), x[2]) for x in INDICS])
        desc = 'Antenatal care visits%s' % (
            ' (%s)' % (self.navb.find_descr([(makecol(x[0]), x[2])
                                             for x in INDICS], sc)))

        #print INDICS_HEADERS, tabular, locateds
        return (title, desc, group, attrs, markup, cols, nat, tabular,
                locateds, INDICS_HEADERS)
예제 #3
0
    def get_tables(self):
        cnds = self.navb.conditions()
        cnds.update({"(created_at) <= '%s'" % (self.navb.finish): ''})
        cnds.update({
            "(delivery_date + INTERVAL \'%d days\') >= '%s'" % (PNC_GESTATION, self.navb.start):
            ''
        })

        cnds, markup, cols = self.navb.neater_tables(
            cnds=cnds,
            extras=[('national_id', 'Mother ID'),
                    ('user_phone', 'Reporter Phone'),
                    ('delivery_date', 'Birth date'), ('pnc_visit', 'Visit'),
                    ('created_at', 'Submission Date'), ('indexcol', "ID")])

        markup.update({
            'indexcol':
            lambda x, _, __:
            '<a href="/dashboards/report?tbl=pnc&id=%s">View</a>' % (x),
        })

        INDICS = [
            ('all', 'total', 'Total'),
        ] + [(makecol(x[0]), x[0], x[1])
             for x in queries.PNC_DATA['PNC']['attrs']] + [
                 ('no_risk', "%s" % queries.PNC_DATA['NO_RISK']['query_str'],
                  'No Risk'),
                 ('at_risk', "%s" % queries.PNC_DATA['RISK']['query_str'],
                  'At Risk'),
             ]

        INDICSDICT = {x[0]: (x[1], x[2]) for x in INDICS}

        title, sc, group, attrs, nat, tabular, locateds, INDICS_HEADERS = (
            '', '', '', [], [], [], [], [])

        if self.navb.kw.get('group'):
            group = self.navb.kw.get('group')
            wcl = INDICSDICT[group]  #;print wcl, INDICSDICT
            title = wcl[1]
            INDICS = [(group, wcl[0], wcl[1])] if wcl else []
            INDICS_HEADERS = dict([(makecol(x[0]), x[2]) for x in INDICS])
            if wcl and wcl[0] != 'total': cnds.update({wcl[0]: ''})

            if group == 'at_risk':
                INDICS += [(makecol(x[0]), x[0], x[1])
                           for x in queries.PNC_DATA['RISK']['attrs']]
            else:
                pass
            INDICSDICT.update({x[0]: (x[1], x[2]) for x in INDICS})

            if self.navb.kw.get('subcat'):
                sc = self.navb.kw.get('subcat')
                wclsc = INDICSDICT[sc]  #;print wclsc, ATTRSDICT, ATTRS
                INDICS = [(sc, wcl[0], wclsc[1])] if wclsc else []
                INDICS_HEADERS = dict([(makecol(x[0]), x[2]) for x in INDICS])
                cnds.update({wclsc[0]: ''})

            #print cnds
            dcols = [x[0] for x in cols]
            nat = Pncvisit.fetch_log_pncvisits(cnds, dcols)

        else:
            dcols = [x[0] for x in cols]
            nat = Pncvisit.fetch_log_pncvisits(cnds, dcols)

        if self.navb.kw.get(
                'view') == 'table' or self.navb.kw.get('view') != 'log':
            group_by = []
            group_by += [
                'province_pk'
            ] if self.navb.kw.get('nation') or not group_by else []
            group_by += ['district_pk'] if self.navb.kw.get('province') else []
            group_by += ['referral_facility_pk'
                         ] if self.navb.kw.get('district') else []
            group_by += ['facility_pk'] if self.navb.kw.get('hd') else []
            #print "\nGROUP BY: %s \n" % group_by
            LOCS = {
                'nation': self.navb.kw.get('nation'),
                'province': self.navb.kw.get('province'),
                'district': self.navb.kw.get('district'),
                'hospital': self.navb.kw.get('hd'),
                'location': self.navb.kw.get('hc')
            }

            locateds = Pncvisit.fetch_pncvisits_by_location(cnds,
                                                            group_by=group_by,
                                                            INDICS=INDICS)
            #print [[y.__dict__ for y in x] for x in locateds]#, INDICS, LOCS, self.navb.locs()
            tabular = give_me_table(locateds,
                                    self.navb.locs(),
                                    INDICS=INDICS,
                                    LOCS=LOCS)
            #print locateds, "\n", tabular

        INDICS_HEADERS = dict([(makecol(x[0]), x[2]) for x in INDICS])
        desc = 'Postnatal visits%s' % (
            ' (%s)' % (self.navb.find_descr([(makecol(x[0]), x[2])
                                             for x in INDICS], sc or group)))

        #print INDICS_HEADERS, tabular, locateds
        return (title, desc, group, attrs, markup, cols, nat, tabular,
                locateds, INDICS_HEADERS)
예제 #4
0
    def get_tables(self):
        cnds    = self.navb.conditions()
        cnds.update({"(created_at) <= '%s'" % (self.navb.finish) : ''})
        cnds.update({"(created_at) >= '%s'" % (self.navb.start) : ''})

        cnds, markup, cols = self.navb.neater_tables(cnds = cnds, extras = [
                                              ('national_id',            'Mother ID'),
                                              ('user_phone',            'Reporter Phone'),
                                              ('birth_date', 'Birth date'),
                                              ('child_number', 'Child Number'),
                                              ('death_code', 'Visit'),
                                              ('created_at', 'Submission Date'),
                                              ('indexcol', "ID")
                                              
                                            ])

        markup.update({'indexcol': lambda x, _, __: '<a href="/dashboards/report?tbl=dth&id=%s">View</a>' % (x), })            

        INDICS = [('all', 'total', 'Total')]
        for x in queries.DEATH_DATA['attrs']:
            INDICS.append((makecol(x[0][0]), x[0][0], x[0][1]))
            for y in x[1]: INDICS.append((makecol(y[0]), y[0], '%s %s' % (x[0][1], y[1]) ))

        INDICSDICT = {x[0]: (x[1], x[2]) for x in INDICS}
  
        title, sc, group, attrs, nat, tabular, locateds, INDICS_HEADERS = ('', '', '', [], [], [],[],[])
        
        if self.navb.kw.get('subcat'):
            sc = self.navb.kw.get('subcat')
            wcl  = INDICSDICT[sc]#;print wcl, INDICSDICT       
            INDICS = [(sc, wcl[0], wcl[1])] if wcl else []
            INDICS_HEADERS = dict([ ( makecol(x[0]), x[2]) for x in INDICS])
            if wcl and wcl[0] != 'total':   cnds.update({wcl[0]: ''})
            
            dcols   = [x[0] for x in cols]
            nat = Death.fetch_log_deaths(cnds, dcols)
        
        else:
          dcols   = [x[0] for x in cols]
          nat = Death.fetch_log_deaths(cnds, dcols)

        if self.navb.kw.get('view') == 'table' or self.navb.kw.get('view') != 'log' :
            group_by = []
            group_by += ['province_pk']  if self.navb.kw.get('nation') or not group_by else []
            group_by += ['district_pk'] if self.navb.kw.get('province') else []
            group_by += ['referral_facility_pk'] if self.navb.kw.get('district') else []
            group_by += ['facility_pk'] if self.navb.kw.get('hd') else [] 
            #print "\nGROUP BY: %s \n" % group_by
            LOCS = {'nation': self.navb.kw.get('nation'),
                    'province': self.navb.kw.get('province'),
                    'district': self.navb.kw.get('district'),
                    'hospital': self.navb.kw.get('hd'),
                    'location': self.navb.kw.get('hc')
                    }
                        
            locateds = Death.fetch_deaths_by_location(cnds, group_by = group_by, INDICS = INDICS)
            #print [[y.__dict__ for y in x] for x in locateds]#, INDICS, LOCS, self.navb.locs()
            tabular = give_me_table(locateds, self.navb.locs(),  INDICS = INDICS, LOCS = LOCS)
            #print locateds, "\n", tabular 
        
        INDICS_HEADERS = dict([ ( makecol(x[0]), x[2]) for x in INDICS])
        desc  = 'Deaths%s' % (' (%s)' % ( self.navb.find_descr( [(makecol(x[0]), x[2]) for x in INDICS], sc or group ) ) )
 
        #print INDICS_HEADERS, tabular, locateds
        return (title, desc, group, attrs, markup, cols, nat, tabular, locateds, INDICS_HEADERS)
예제 #5
0
파일: ccms.py 프로젝트: pivotaccess2007/mch
    def get_tables(self):
        cnds    = self.navb.conditions()
        cnds.update({"(created_at) <= '%s'" % (self.navb.finish) : ''})
        cnds.update({"(created_at) >= '%s'" % (self.navb.start) : ''})

        cnds, markup, cols = self.navb.neater_tables(cnds = cnds, extras = [
                                              ('national_id',            'Mother ID'),
                                              ('user_phone',            'Reporter Phone'),
                                              ('birth_date', 'Birth date'),
                                              ('child_number', 'Child number'),
                                              ('muac', 'MUAC'),
                                              ('created_at', 'Submission Date'),
                                              ('indexcol', "ID")
                                              
                                            ])

        
        title, sc, group, attrs, nat, tabular, locateds, INDICS_HEADERS = ('', '', '', [], [], [],[],[])

        CCMINDICS = [('ccm', 'total', 'Total CCM'),] + [(makecol(x[0]), x[0], x[1]) for x in queries.CCM_DATA['attrs'] ]
        CCMINDICSDICT = {x[0]: (x[1], x[2]) for x in CCMINDICS}

        CMRINDICS = [('cmr', 'total', 'Total CMR'),] + [(makecol(x[0]), x[0], x[1]) for x in queries.CMR_DATA['attrs'] ]
        CMRINDICSDICT = {x[0]: (x[1], x[2]) for x in CMRINDICS}
        INDICS = CCMINDICS
        if self.navb.kw.get('subcat'):
          sc = self.navb.kw.get('subcat')
          dcols   = [x[0] for x in cols]
          if self.navb.kw.get('group') == 'cmr' or self.navb.kw.get('subcat') == 'cmr':
            wcl  = CMRINDICSDICT[sc]#;print wcl, CMRINDICSDICT       
            INDICS = [(sc, wcl[0], wcl[1])] if wcl else []
            INDICS_HEADERS = dict([ ( makecol(x[0]), x[2]) for x in INDICS])
            if wcl and wcl[0] != 'total':   cnds.update({wcl[0]: ''})
            markup.update({'indexcol': lambda x, _, __: '<a href="/dashboards/report?tbl=cmr&id=%s">View</a>' % (x), }) 
            nat = CCM.fetch_log_cmrs(cnds, dcols)
          else:
            wcl  = CCMINDICSDICT[sc]#;print wcl, CCMINDICSDICT       
            INDICS = [(sc, wcl[0], wcl[1])] if wcl else []
            INDICS_HEADERS = dict([ ( makecol(x[0]), x[2]) for x in INDICS])
            if wcl and wcl[0] != 'total':   cnds.update({wcl[0]: ''})
            markup.update({'indexcol': lambda x, _, __: '<a href="/dashboards/report?tbl=ccm&id=%s">View</a>' % (x), }) 
            nat = CCM.fetch_log_ccms(cnds, dcols) 
        
        else:
          dcols   = [x[0] for x in cols]
          if self.navb.kw.get('group') == 'cmr' or self.navb.kw.get('subcat') == 'cmr':
            markup.update({'indexcol': lambda x, _, __: '<a href="/dashboards/report?tbl=cmr&id=%s">View</a>' % (x), }) 
            nat = CMR.fetch_log_cmrs(cnds, dcols)
          else:
            markup.update({'indexcol': lambda x, _, __: '<a href="/dashboards/report?tbl=ccm&id=%s">View</a>' % (x), }) 
            nat = CCM.fetch_log_ccms(cnds, dcols)

        if self.navb.kw.get('view') == 'table' or self.navb.kw.get('view') != 'log' :
            group_by = []
            group_by += ['province_pk']  if self.navb.kw.get('nation') or not group_by else []
            group_by += ['district_pk'] if self.navb.kw.get('province') else []
            group_by += ['referral_facility_pk'] if self.navb.kw.get('district') else []
            group_by += ['facility_pk'] if self.navb.kw.get('hd') else [] 
            #print "\nGROUP BY: %s \n" % group_by
            LOCS = {'nation': self.navb.kw.get('nation'),
                    'province': self.navb.kw.get('province'),
                    'district': self.navb.kw.get('district'),
                    'hospital': self.navb.kw.get('hd'),
                    'location': self.navb.kw.get('hc')
                    }
                        
            if self.navb.kw.get('subcat') in [x[0] for x in CCMINDICS]:
                #print PRE_INDICS, LOCS, cnds, group_by
                locateds = CCM.fetch_ccms_by_location(cnds, group_by = group_by, INDICS = CCMINDICS)
            elif self.navb.kw.get('subcat') in [x[0] for x in CMRINDICS]:
                 #print INDICS, LOCS, cnds, group_by
                 locateds = CCM.fetch_cmrs_by_location(cnds, group_by = group_by, INDICS = CMRINDICS)
            else:
                INDICS = CCMINDICS + CMRINDICS
                locateds = CCM.fetch_ccms_by_location(cnds, group_by = group_by, INDICS = CCMINDICS)
                locateds += CCM.fetch_cmrs_by_location(cnds, group_by = group_by, INDICS = CMRINDICS)
                #print [[y.__dict__ for y in x] for x in locateds]#, INDICS, LOCS, self.navb.locs()

            tabular = give_me_table(locateds, self.navb.locs(),  INDICS = INDICS, LOCS = LOCS)
            #print locateds, "\n", tabular 
        
        INDICS_HEADERS = dict([ ( makecol(x[0]), x[2]) for x in INDICS])
        desc  = 'CCM%s' % (' (%s)' % ( self.navb.find_descr( [(makecol(x[0]), x[2]) for x in INDICS], sc or group ) ) )
 
        #print INDICS_HEADERS, tabular, locateds
        return (title, desc, group, attrs, markup, cols, nat, tabular, locateds, INDICS_HEADERS)
예제 #6
0
    def get_tables(self):
        cnds = self.navb.conditions()
        cnds.update({
            "(created_at) >= '%s'" % (self.navb.start): '',
            "(created_at) <= '%s'" % (self.navb.finish): '',
            "keyword = 'SMN'": ''
        })
        exts = {}
        cnds, markup, cols = self.navb.neater_tables(
            cnds=cnds,
            extras=[('has_gone_hc', 'Received at HC'),
                    ('has_gone_hd', 'Received at DH'),
                    ('province_pk', 'Province'), ('district_pk', 'District'),
                    ('referral_facility_pk', 'Hospital'),
                    ('facility_pk', 'Health Centre'), ('sector_pk', 'Sector'),
                    ('cell_pk', 'Cell'), ('village_pk', 'Village'),
                    ('national_id', 'National ID'),
                    ('user_phone', 'Reporter Phone'),
                    ('created_at', 'Submission Datetime'),
                    ('indexcol', 'Message')])

        markup.update({
            'action':
            lambda x, _, __:
            '<a href="/dashboards/diagnosisdash?id=%s">Fill Diagnosis Form %s</a>'
            % (x, x),
        })
        markup.update({
            'indexcol':
            lambda x, _, __:
            '<a href="/dashboards/report?tbl=smn&id=%s">View</a>' % (x),
        })
        markup.update({
            'has_gone_hc':
            lambda x, _, __: '%s' %
            ('<b style="color:red;">HC: NO</b>'
             if not x else '<b style="color:green;">HC: YES</b>')
        })
        markup.update({
            'has_gone_hd':
            lambda x, _, __: '%s' %
            ('<b style="color:red;">DH: NO</b>'
             if not x else '<b style="color:green;">DH: YES</b>')
        })
        markup.update({
            'symptoms':
            lambda x, _, __: '%s' % (Malaria.get_report_symptoms(x))
        })
        markup.update({
            'drugs':
            lambda x, _, __: '%s' % (Malaria.get_report_drugs(x))
        })
        markup.update({
            'message':
            lambda x, _, __: '%s' % (Malaria.get_report_details(x))
        })

        DESCRI = []
        MALARIADICT = makedict(queries.MALARIA_DATA['attrs'])
        INDICS = [('all', 'total', 'Total')] + [
            (makecol(x[0]), x[0], x[1]) for x in queries.MALARIA_DATA['attrs']
        ]
        attrs = []
        group = "Severe Malaria"
        title = "Severe Malaria Notifications"
        sc = self.navb.kw.get('subcat')
        title, sc, group, attrs, nat, tabular, locateds, INDICS_HEADERS = (
            '', '', '', [], [], [], [], [])
        if self.navb.kw.get('subcat') and self.navb.kw.get('subcat') in [
                makecol(x[0]) for x in queries.MALARIA_DATA['attrs']
        ]:
            sc = self.navb.kw.get('subcat')
            wcl = MALARIADICT.get(sc)
            cnds.update({wcl[0]: ''})
            INDICS = [(sc, wcl[0], wcl[1])] if wcl else []
            #print INDICS, MALARIADICT

        if self.navb.kw.get(
                'view') == 'table' or self.navb.kw.get('view') != 'log':
            group_by = []
            group_by += [
                'province_pk'
            ] if self.navb.kw.get('nation') or not group_by else []
            group_by += ['district_pk'] if self.navb.kw.get('province') else []
            group_by += ['referral_facility_pk'
                         ] if self.navb.kw.get('district') else []
            group_by += ['facility_pk'] if self.navb.kw.get('hd') else []
            #print "\nGROUP BY: %s \n" % group_by
            LOCS = {
                'nation': self.navb.kw.get('nation'),
                'province': self.navb.kw.get('province'),
                'district': self.navb.kw.get('district'),
                'hospital': self.navb.kw.get('hd'),
                'location': self.navb.kw.get('hc')
            }
            locateds = Malaria.fetch_malaria_by_location(cnds,
                                                         group_by=group_by,
                                                         INDICS=INDICS)
            #print [[y.__dict__ for y in x] for x in locateds]#, INDICS, LOCS, self.navb.locs()
            tabular = give_me_table(locateds,
                                    self.navb.locs(),
                                    INDICS=INDICS,
                                    LOCS=LOCS)
            #print tabular

        INDICS_HEADERS = dict([(makecol(x[0]), x[2]) for x in INDICS])

        dcols = [x[0] for x in cols]
        nat = Malaria.fetch_log_malaria(cnds, dcols)
        #DESCRI.append((group, title))
        desc = 'Severe malaria cases%s' % (' (%s)' % (self.navb.find_descr(
            DESCRI + [(makecol(x[0]), x[2])
                      for x in INDICS], sc or self.navb.kw.get('subcat'))))
        #print title, group, attrs, "NAT: ", nat[0].__dict__
        return (title, desc, group, attrs, markup, cols, nat, tabular,
                locateds, INDICS_HEADERS)
예제 #7
0
    def get_tables(self):
        chi_cnds = self.navb.conditions()
        five_years_ago = self.navb.start - timedelta(days=TRACKING_DAYS)
        chi_cnds.update({"(birth_date) <= '%s'" % (self.navb.finish): ''})
        chi_cnds.update({"(birth_date) >= '%s'" % (five_years_ago): ''})
        chi_attrs = queries.CHILD_NUTR.keys()

        pre_cnds = self.navb.conditions()
        pre_cnds.update({"(created_at) <= '%s'" % (self.navb.finish): ''})
        pre_cnds.update({
            "(recent_lmp + INTERVAL \'%d days\') >= '%s'" % (GESTATION, self.navb.start):
            ''
        })
        pre_attrs = queries.MOTHER_NUTR.keys()

        chi_cnds, markup, chicols = self.navb.neater_tables(
            cnds=chi_cnds,
            extras=[('national_id', 'Mother ID'),
                    ('user_phone', 'Reporter Phone'),
                    ('birth_date', 'Birth date'), ('child_weight', 'Weight'),
                    ('recent_muac', 'Muac'), ('created_at', 'Submission Date'),
                    ('indexcol', "ID")])

        CHIINDICS = [
            ('child', 'total', 'Total Children'),
        ] + [(makecol(x), queries.CHILD_NUTR[x][0], queries.CHILD_NUTR[x][1])
             for x in chi_attrs]
        CHIINDICSDICT = {x[0]: (x[1], x[2]) for x in CHIINDICS}

        pre_cnds, markup, precols = self.navb.neater_tables(
            cnds=pre_cnds,
            extras=[
                ('national_id', 'Mother ID'),
                ('user_phone', 'Reporter Phone'),
                ('recent_lmp', 'LMP'),
                #('gravidity', 'Gravidity'),
                #('parity', 'Parity'),
                ('recent_mother_weight', 'Weight'),
                ('recent_mother_height', 'Height'),
                ('recent_bmi', 'BMI'),
                ('recent_muac', 'MUAC'),
                ('created_at', 'Submission Date'),
                ('indexcol', "ID")
            ])

        PREINDICS = [
            ('mother', 'total', 'Total Mothers'),
        ] + [(makecol(x), queries.MOTHER_NUTR[x][0], queries.MOTHER_NUTR[x][1])
             for x in pre_attrs]
        PREINDICSDICT = {x[0]: (x[1], x[2]) for x in PREINDICS}

        INDICS = CHIINDICS
        title, sc, group, attrs, nat, tabular, locateds, INDICS_HEADERS = (
            '', '', '', [], [], [], [], [])

        if self.navb.kw.get('subcat'):
            sc = self.navb.kw.get('subcat')
            if self.navb.kw.get('group') == 'mother' or self.navb.kw.get(
                    'subcat') == 'mother':
                wcl = PREINDICSDICT[sc]  #;print wcl, CMRINDICSDICT
                INDICS = [(sc, wcl[0], wcl[1])] if wcl else []
                INDICS_HEADERS = dict([(makecol(x[0]), x[2]) for x in INDICS])
                if wcl and wcl[0] != 'total': pre_cnds.update({wcl[0]: ''})
                dcols, cols = [x[0] for x in precols], precols
                markup.update({
                    'indexcol':
                    lambda x, _, __:
                    '<a href="/dashboards/report?tbl=mother&id=%s">View</a>' %
                    (x),
                })
                nat = Nutrition.fetch_log_mothers(pre_cnds, dcols)
            else:
                wcl = CHIINDICSDICT[sc]  #;print wcl, CCMINDICSDICT
                INDICS = [(sc, wcl[0], wcl[1])] if wcl else []
                INDICS_HEADERS = dict([(makecol(x[0]), x[2]) for x in INDICS])
                if wcl and wcl[0] != 'total': chi_cnds.update({wcl[0]: ''})
                dcols, cols = [x[0] for x in chicols], chicols
                markup.update({
                    'indexcol':
                    lambda x, _, __:
                    '<a href="/dashboards/report?tbl=bir&id=%s">View</a>' %
                    (x),
                })
                nat = Nutrition.fetch_log_children(chi_cnds, dcols)

        else:
            if self.navb.kw.get('group') == 'mother' or self.navb.kw.get(
                    'subcat') == 'mother':
                dcols, cols = [x[0] for x in precols], precols
                markup.update({
                    'indexcol':
                    lambda x, _, __:
                    '<a href="/dashboards/report?tbl=mother&id=%s">View</a>' %
                    (x),
                })
                nat = Nutrition.fetch_log_mothers(pre_cnds, dcols)
            else:
                dcols, cols = [x[0] for x in chicols], chicols
                markup.update({
                    'indexcol':
                    lambda x, _, __:
                    '<a href="/dashboards/report?tbl=bir&id=%s">View</a>' %
                    (x),
                })
                nat = Nutrition.fetch_log_children(chi_cnds, dcols)

        if self.navb.kw.get(
                'view') == 'table' or self.navb.kw.get('view') != 'log':
            group_by = []
            group_by += [
                'province_pk'
            ] if self.navb.kw.get('nation') or not group_by else []
            group_by += ['district_pk'] if self.navb.kw.get('province') else []
            group_by += ['referral_facility_pk'
                         ] if self.navb.kw.get('district') else []
            group_by += ['facility_pk'] if self.navb.kw.get('hd') else []
            #print "\nGROUP BY: %s \n" % group_by
            LOCS = {
                'nation': self.navb.kw.get('nation'),
                'province': self.navb.kw.get('province'),
                'district': self.navb.kw.get('district'),
                'hospital': self.navb.kw.get('hd'),
                'location': self.navb.kw.get('hc')
            }

            if self.navb.kw.get('subcat') in [x[0] for x in CHIINDICS]:
                #print PRE_INDICS, LOCS, cnds, group_by
                locateds = Nutrition.fetch_children_by_location(
                    chi_cnds, group_by=group_by, INDICS=CHIINDICS)
            elif self.navb.kw.get('subcat') in [x[0] for x in PREINDICS]:
                #print INDICS, LOCS, cnds, group_by
                locateds = Nutrition.fetch_mothers_by_location(
                    pre_cnds, group_by=group_by, INDICS=PREINDICS)
            else:
                INDICS = CHIINDICS + PREINDICS
                locateds = Nutrition.fetch_children_by_location(
                    chi_cnds, group_by=group_by, INDICS=CHIINDICS)
                locateds += Nutrition.fetch_mothers_by_location(
                    pre_cnds, group_by=group_by, INDICS=PREINDICS)
                #print [[y.__dict__ for y in x] for x in locateds]#, INDICS, LOCS, self.navb.locs()

            tabular = give_me_table(locateds,
                                    self.navb.locs(),
                                    INDICS=INDICS,
                                    LOCS=LOCS)
            #print locateds, "\n", tabular

        INDICS_HEADERS = dict([(makecol(x[0]), x[2]) for x in INDICS])
        desc = 'Nutrition%s' % (
            ' (%s)' % (self.navb.find_descr([(makecol(x[0]), x[2])
                                             for x in INDICS], sc or group)))

        #print INDICS_HEADERS, tabular, locateds
        return (title, desc, group, attrs, markup, cols, nat, tabular,
                locateds, INDICS_HEADERS)
예제 #8
0
    def get_deilivery_notifications_tables(self):
        cnds = self.navb.conditions()
        cnds.update({"(created_at) <= '%s'" % (self.navb.finish): ''})
        cnds.update({
            "(lmp + INTERVAL \'%d days\') >= '%s'" % (GESTATION, self.navb.start):
            ''
        })  #;print cnds

        cnds, markup, cols = self.navb.neater_tables(
            cnds=cnds,
            extras=[('national_id', 'Mother ID'),
                    ('user_phone', 'Reporter Phone'), ('lmp', 'LMP'),
                    ('gravidity', 'Gravidity'), ('parity', 'Parity'),
                    ('mother_weight', 'Weight'), ('mother_height', 'Height'),
                    ('bmi', 'BMI'), ('created_at', 'Submission Date')])

        today = self.navb.start  #datetime.today().date()
        next_monday = today + timedelta(days=-today.weekday(), weeks=1)
        next_sunday = next_monday + timedelta(days=6)
        next_two_monday = today + timedelta(days=-today.weekday(), weeks=2)
        next_two_sunday = next_two_monday + timedelta(days=6)
        attrs = [
            ('next_week', 'Deliveries in Next Week',
             "(lmp + INTERVAL '%s days') BETWEEN '%s' AND '%s'" %
             (GESTATION, next_monday, next_sunday)),
            ('next_two_week', 'Deliveries in Next two Weeks',
             "(lmp + INTERVAL '%s days') BETWEEN '%s' AND '%s'" %
             (GESTATION, next_two_monday, next_two_sunday)),
        ]

        INDICSDICT = {x[0]: (x[2], x[1]) for x in attrs}
        INDICS = [(x[0], x[2], x[1]) for x in attrs]

        title, sc, group, attrs, nat, tabular, locateds, INDICS_HEADERS = (
            '', '', '', [], [], [], [], [])

        if self.navb.kw.get('subcat') is not None:
            sc = self.navb.kw.get('subcat')
            dcols = [x[0] for x in cols]
            wcl = INDICSDICT.get(sc)
            INDICS = [(sc, wcl[0], wcl[1])] if wcl else []
            if sc == 'next_week':
                INDICS += [
                    ('no_risk',
                     "(lmp + INTERVAL '%s days') BETWEEN '%s' AND '%s' AND %s"
                     % (GESTATION, next_monday, next_sunday,
                        queries.NO_RISK['query_str']), 'No Risk'),
                    ('at_risk',
                     "(lmp + INTERVAL '%s days') BETWEEN '%s' AND '%s' AND %s"
                     % (GESTATION, next_monday, next_sunday,
                        queries.RISK['query_str']), 'At Risk'),
                    ('high_risk',
                     "(lmp + INTERVAL '%s days') BETWEEN '%s' AND '%s' AND %s"
                     % (GESTATION, next_monday, next_sunday,
                        queries.HIGH_RISK['query_str']), 'High Risk')
                ]
            elif sc == 'next_two_week':
                INDICS += [
                    ('no_risk',
                     "(lmp + INTERVAL '%s days') BETWEEN '%s' AND '%s' AND %s"
                     % (GESTATION, next_two_monday, next_two_sunday,
                        queries.NO_RISK['query_str']), 'No Risk'),
                    ('at_risk',
                     "(lmp + INTERVAL '%s days') BETWEEN '%s' AND '%s' AND %s"
                     % (GESTATION, next_two_monday, next_two_sunday,
                        queries.RISK['query_str']), 'At Risk'),
                    ('high_risk',
                     "(lmp + INTERVAL '%s days') BETWEEN '%s' AND '%s' AND %s"
                     % (GESTATION, next_tow_monday, next_two_sunday,
                        queries.HIGH_RISK['query_str']), 'High Risk')
                ]

            INDICS_HEADERS = dict([(makecol(x[0]), x[2]) for x in INDICS])
            cnds.update({wcl[0]: ''})
            nat = Pregnancy.fetch_log_pregnancies(cnds, dcols)
            #print wcl, INDICSDICT, INDICS
        else:
            INDICS += [
                ('no_risk',
                 "(lmp + INTERVAL '%s days') BETWEEN '%s' AND '%s' AND %s" %
                 (GESTATION, next_monday, next_two_sunday,
                  queries.NO_RISK['query_str']), 'No Risk'),
                ('at_risk',
                 "(lmp + INTERVAL '%s days') BETWEEN '%s' AND '%s' AND %s" %
                 (GESTATION, next_monday, next_two_sunday,
                  queries.RISK['query_str']), 'At Risk'),
                ('high_risk',
                 "(lmp + INTERVAL '%s days') BETWEEN '%s' AND '%s' AND %s" %
                 (GESTATION, next_monday, next_two_sunday,
                  queries.HIGH_RISK['query_str']), 'High Risk')
            ]
            dcols = [x[0] for x in cols]
            nat = Pregnancy.fetch_log_pregnancies(cnds, dcols)

        if self.navb.kw.get(
                'view') == 'table' or self.navb.kw.get('view') != 'log':
            group_by = []
            group_by += [
                'province_pk'
            ] if self.navb.kw.get('nation') or not group_by else []
            group_by += ['district_pk'] if self.navb.kw.get('province') else []
            group_by += ['referral_facility_pk'
                         ] if self.navb.kw.get('district') else []
            group_by += ['facility_pk'] if self.navb.kw.get('hd') else []
            #print "\nGROUP BY: %s \n" % group_by
            LOCS = {
                'nation': self.navb.kw.get('nation'),
                'province': self.navb.kw.get('province'),
                'district': self.navb.kw.get('district'),
                'hospital': self.navb.kw.get('hd'),
                'location': self.navb.kw.get('hc')
            }

            locateds = Pregnancy.fetch_pregnancies_by_location(
                cnds, group_by=group_by, INDICS=INDICS)
            tabular = give_me_table(locateds,
                                    self.navb.locs(),
                                    INDICS=INDICS,
                                    LOCS=LOCS)
            #print locateds, "\n", tabular

        INDICS_HEADERS = dict([(makecol(x[0]), x[2]) for x in INDICS])
        desc = 'Deliveries Notifications%s' % (
            ' (%s)' % (self.navb.find_descr([(makecol(x[0]), x[2])
                                             for x in INDICS], sc)))
        """
        nat = Pregnancy.fetch_pregnancies(cnds, cols, exts)
        details = {}
        for attr in attrs:
            attr_cnds = self.navb.conditions()
            attr_cnds.update({"(created_at) <= '%s'" % (self.navb.finish) : ''})
            attr_cnds.update({attr[2]: ''})
            cols = ['COUNT(*) AS total']
            exts = {'no_risk': ('COUNT(*)', queries.NO_RISK['query_str']), 
	             'at_risk': ('COUNT(*)', queries.RISK['query_str']),
                 'high_risk': ('COUNT(*)', queries.HIGH_RISK['query_str']),
	            }
            details.update({ attr[0] : Pregnancy.fetch_pregnancies(attr_cnds, cols, exts) })
        #print title, group, attrs, "NAT: ", nat[0].__dict__, details
        """

        return (title, desc, group, attrs, markup, cols, nat, tabular,
                locateds, INDICS_HEADERS)
예제 #9
0
    def get_tables(self):
        cnds = self.navb.conditions()
        cnds.update({"(created_at) <= '%s'" % (self.navb.finish): ''})
        cnds.update({
            "(lmp + INTERVAL \'%d days\') >= '%s'" % (GESTATION, self.navb.start):
            ''
        })  #;print cnds
        exts = {}
        cnds, markup, cols = self.navb.neater_tables(
            cnds=cnds,
            extras=[('national_id', 'Mother ID'),
                    ('user_phone', 'Reporter Phone'), ('lmp', 'LMP'),
                    ('gravidity', 'Gravidity'), ('parity', 'Parity'),
                    ('mother_weight', 'Weight'), ('mother_height', 'Height'),
                    ('bmi', 'BMI'), ('created_at', 'Submission Date'),
                    ('indexcol', 'ID')])

        markup.update({
            'indexcol':
            lambda x, _, __:
            '<a href="/dashboards/report?tbl=pre&id=%s">View</a>' % (x),
        })

        #print cnds, markup, cols
        DESCRI = []
        HIGHRISKDICT = makedict(queries.HIGH_RISK['attrs'])
        RISKDICT = makedict(queries.RISK['attrs'])
        INDICS = []
        title, sc, group, attrs, nat, tabular, locateds, INDICS_HEADERS = (
            '', '', '', [], [], [], [], [])  #; print self.navb.kw
        if self.navb.kw.get('subcat') is not None:
            sc = self.navb.kw.get('subcat')  #;print RISKDICT
            wcl = HIGHRISKDICT.get(sc) or RISKDICT.get(sc)  #;print wcl
            INDICS = [(sc, wcl[0], wcl[1])] if wcl else []
            cnds.update({wcl[0]: ''})
            dcols = [x[0] for x in cols]  #;print  "INDICS: ", INDICS
            #nat = Pregnancy.fetch_log_pregnancies(cnds, dcols)
        elif self.navb.kw.get('group') == 'no_risk':
            title = 'No Risk'
            group = 'no_risk'
            INDICS = [(group, queries.NO_RISK['query_str'], title)]
            cnds.update({queries.NO_RISK['query_str']: ''})
            dcols = [x[0] for x in cols]  #;print INDICS
            #nat = Pregnancy.fetch_log_pregnancies(cnds, dcols)
        elif self.navb.kw.get(
                'group') == 'at_risk' and self.navb.kw.get('subcat') is None:
            title = 'At Risk'
            group = 'at_risk'
            INDICS = [(group, queries.RISK['query_str'], title)]
            for k in RISKDICT.keys():
                INDICS.append((k, RISKDICT[k][0], RISKDICT[k][1]))
            cnds.update({queries.RISK['query_str']: ''})
            dcols = [x[0] for x in cols]
            #nat = Pregnancy.fetch_log_pregnancies(cnds, dcols)
        elif self.navb.kw.get(
                'group') == 'high_risk' and self.navb.kw.get('subcat') is None:
            title = 'High Risk'
            group = 'high_risk'
            INDICS = [(group, queries.HIGH_RISK['query_str'], title)]
            for k in HIGHRISKDICT.keys():
                INDICS.append((k, HIGHRISKDICT[k][0], HIGHRISKDICT[k][1]))
            cnds.update({queries.HIGH_RISK['query_str']: ''})
            dcols = [x[0] for x in cols]
            #nat = Pregnancy.fetch_log_pregnancies(cnds, dcols)
        elif self.navb.kw.get('group') == 'toilet':
            title = 'With Toilet'
            group = 'toilet'
            INDICS = [(group, "LOWER(toilet) = 'to'", title)]
            cnds.update({"LOWER(toilet) = 'to'": ''})
            dcols = [x[0] for x in cols]
            #nat = Pregnancy.fetch_log_pregnancies(cnds, dcols)
        elif self.navb.kw.get('group') == 'handwash':
            title = 'With Handwashing'
            group = 'handwash'
            INDICS = [(group, "LOWER(handwash) = 'hw'", title)]
            cnds.update({"LOWER(handwash) = 'hw'": ''})
            dcols = [x[0] for x in cols]
            #nat = Pregnancy.fetch_log_pregnancies(cnds, dcols)
        elif self.navb.kw.get('group') == 'notoilet':
            title = 'No Toilet'
            group = 'notoilet'
            INDICS = [(group, "LOWER(toilet) = 'nt'", title)]
            cnds.update({"LOWER(toilet) = 'nt'": ''})
            dcols = [x[0] for x in cols]
            #nat = Pregnancy.fetch_log_pregnancies(cnds, dcols)
        elif self.navb.kw.get('group') == 'nohandwash':
            title = 'No Handwashing'
            group = 'nohandwash'
            INDICS = [(group, "LOWER(handwash) = 'nh'", title)]
            cnds.update({"LOWER(handwash) = 'nh'": ''})
            dcols = [x[0] for x in cols]
            #nat = Pregnancy.fetch_log_pregnancies(cnds, dcols)
        else:
            dcols = [x[0] for x in cols]
            INDICS = [
                ("no_risk", queries.NO_RISK['query_str'], 'No Risk'),
                ("at_risk", queries.RISK['query_str'], 'At Risk'),
                ("high_risk", queries.HIGH_RISK['query_str'], 'High Risk'),
                ("toilet", "LOWER(toilet) = 'to'", 'With toilet'),
                ("handwash", "LOWER(handwash) = 'hw'", 'With handwashing'),
                ("notoilet", "LOWER(toilet) = 'nt'", 'With no toilet'),
                ("nohandwash", "LOWER(handwash) = 'nh'", 'With no Handwashing')
            ]
            #nat = Pregnancy.fetch_log_pregnancies(cnds, dcols)

        if self.navb.kw.get(
                'view') == 'table' or self.navb.kw.get('view') != 'log':
            group_by = []
            group_by += [
                'province_pk'
            ] if self.navb.kw.get('nation') or not group_by else []
            group_by += ['district_pk'] if self.navb.kw.get('province') else []
            group_by += ['referral_facility_pk'
                         ] if self.navb.kw.get('district') else []
            group_by += ['facility_pk'] if self.navb.kw.get('hd') else []
            #print "\nGROUP BY: %s \n" % group_by
            LOCS = {
                'nation': self.navb.kw.get('nation'),
                'province': self.navb.kw.get('province'),
                'district': self.navb.kw.get('district'),
                'hospital': self.navb.kw.get('hd'),
                'location': self.navb.kw.get('hc')
            }
            locateds = Pregnancy.fetch_pregnancies_by_location(
                cnds, group_by=group_by, INDICS=INDICS)
            #print [[y.__dict__ for y in x] for x in locateds]#, INDICS, LOCS, self.navb.locs()
            tabular = give_me_table(locateds,
                                    self.navb.locs(),
                                    INDICS=INDICS,
                                    LOCS=LOCS)
            #print tabular
        INDICS_HEADERS = dict([(makecol(x[0]), x[2]) for x in INDICS])
        nat = Pregnancy.fetch_log_pregnancies(cnds, dcols)
        DESCRI.append((group, title))
        desc = 'Pregnancies%s' % (' (%s)' % (self.navb.find_descr(
            DESCRI + [(makecol(x[0]), x[2])
                      for x in INDICS], sc or self.navb.kw.get('group'))))
        #print title, group, attrs, "NAT: ", nat[0].__dict__
        return (title, desc, group, attrs, markup, cols, nat, tabular,
                locateds, INDICS_HEADERS)
예제 #10
0
    def get_tables(self):
        cnds = self.navb.conditions()
        cnds.update({"(created_at) <= '%s'" % (self.navb.finish): ''})
        cnds.update({"(created_at) >= '%s'" % (self.navb.start): ''})
        exts = {}
        cnds, markup, cols = self.navb.neater_tables(
            cnds=cnds,
            extras=[('province_pk', 'Province'), ('district_pk', 'District'),
                    ('referral_facility_pk', 'Hospital'),
                    ('facility_pk', 'Health Centre'), ('sector_pk', 'Sector'),
                    ('cell_pk', 'Cell'), ('village_pk', 'Village'),
                    ('user_phone', 'Reporter Phone'), ('message', 'Message'),
                    ('created_at', 'Submission Date')])

        INDICS = [
            ("pre", 'total', "Pregnancy", Pregnancy.fetch_log_pregnancies,
             Pregnancy.fetch_pregnancies_by_location),
            ("anc", 'total', "Antenatal Consultation",
             Ancvisit.fetch_log_ancvisits,
             Ancvisit.fetch_ancvisits_by_location),
            ("ref", 'total', "Refusal", Refusal.fetch_log_refusals,
             Refusal.fetch_refusals_by_location),
            ("red", 'total', "Red Alert", Redalert.fetch_log_redalerts,
             Redalert.fetch_redalerts_by_location),
            ("rar", 'total', "Red Alert Result", Redalert.fetch_log_redresults,
             Redalert.fetch_redresults_by_location),
            ("risk", 'total', "Risk", Risk.fetch_log_risks,
             Risk.fetch_risks_by_location),
            ("res", 'total', "Risk Result", Riskresult.fetch_log_riskresults,
             Riskresult.fetch_riskresults_by_location),
            ("dep", 'total', "Departure", Departure.fetch_log_departures,
             Departure.fetch_departures_by_location),
            ("bir", 'total', "Birth", Birth.fetch_log_births,
             Birth.fetch_births_by_location),
            ("pnc", 'total', "Postnatal Care", Pncvisit.fetch_log_pncvisits,
             Pncvisit.fetch_pncvisits_by_location),
            ("nbc", 'total', "Newborn Care", Nbcvisit.fetch_log_nbcvisits,
             Nbcvisit.fetch_nbcvisits_by_location),
            ("chi", 'total', "Child Health",
             Childhealth.fetch_log_childhealths,
             Childhealth.fetch_childhealths_by_location),
            ("cbn", 'total', "Community Based Nutrition",
             Nutrition.fetch_log_nutritions,
             Nutrition.fetch_nutritions_by_location),
            ("ccm", 'total', "Community Case Management", CCM.fetch_log_ccms,
             CCM.fetch_ccms_by_location),
            ("cmr", 'total', "Case Management Response", CCM.fetch_log_cmrs,
             CCM.fetch_cmrs_by_location),
            ("dth", 'total', "Death", Death.fetch_log_deaths,
             Death.fetch_deaths_by_location),
            ("smn", 'total', "Severe Malaria", Malaria.fetch_log_malaria,
             Malaria.fetch_malaria_by_location),
            ("smr", 'total', "Severe Malaria Result",
             Malaria.fetch_log_malaria, Malaria.fetch_malaria_by_location),
            ("rso", 'total', "Risk Of Stock Out", Stock.fetch_log_stock,
             Stock.fetch_stock_by_location),
            ("so", 'total', "Stock out", Stock.fetch_log_stock,
             Stock.fetch_stock_by_location),
            ("ss", 'total', "Stock Supplied", Stock.fetch_log_stock,
             Stock.fetch_stock_by_location),
        ]

        INDICSDICT = {x[0]: (x[1], x[2], x[3], x[4]) for x in INDICS}

        title, sc, group, attrs, nat, tabular, locateds, INDICS_HEADERS = (
            '', '', '', [], [], [], [], [])

        if self.navb.kw.get('subcat'):
            sc = self.navb.kw.get('subcat')
            wcl = INDICSDICT[sc]  #;print wcl, INDICSDICT
            INDICS = [(sc, wcl[0], wcl[1])] if wcl else []
            INDICS_HEADERS = dict([(makecol(x[0]), x[2]) for x in INDICS])
            if sc in ['smn', 'smr', 'rso', 'so', 'ss']:
                cnds = self.extra_cnds(
                    cnds, extra={"keyword = '%s'" % sc.upper(): ''})
            dcols = [x[0] for x in cols]
            nat = wcl[2](cnds, dcols)
        else:
            nat = Redalert.fetch_log_redalerts(cnds, dcols)

        if self.navb.kw.get(
                'view') == 'table' or self.navb.kw.get('view') != 'log':
            group_by = []
            group_by += [
                'province_pk'
            ] if self.navb.kw.get('nation') or not group_by else []
            group_by += ['district_pk'] if self.navb.kw.get('province') else []
            group_by += ['referral_facility_pk'
                         ] if self.navb.kw.get('district') else []
            group_by += ['facility_pk'] if self.navb.kw.get('hd') else []
            #print "\nGROUP BY: %s \n" % group_by
            LOCS = {
                'nation': self.navb.kw.get('nation'),
                'province': self.navb.kw.get('province'),
                'district': self.navb.kw.get('district'),
                'hospital': self.navb.kw.get('hd'),
                'location': self.navb.kw.get('hc')
            }

            if self.navb.kw.get('subcat'):
                #print INDICS, LOCS, cnds, group_by
                wcl = INDICSDICT[sc]
                INDICS = [(sc, wcl[0], wcl[1])] if wcl else []
                INDICS_HEADERS = dict([(makecol(x[0]), x[2]) for x in INDICS])
                locateds = wcl[3](cnds, group_by=group_by, INDICS=INDICS)
            else:
                INDICS = REDINDICS + RARINDICS
                locateds = Redalert.fetch_redalerts_by_location(
                    cnds, group_by=group_by, INDICS=REDINDICS)
                locateds += Redalert.fetch_redresults_by_location(
                    cnds, group_by=group_by, INDICS=RARINDICS)
                #print [[y.__dict__ for y in x] for x in locateds]#, INDICS, LOCS, self.navb.locs()

            tabular = give_me_table(locateds,
                                    self.navb.locs(),
                                    INDICS=INDICS,
                                    LOCS=LOCS)
            #print locateds, "\n", tabular

        INDICS_HEADERS = dict([(makecol(x[0]), x[2]) for x in INDICS])
        desc = 'Reports%s' % (
            ' (%s)' % (self.navb.find_descr([(makecol(x[0]), x[2])
                                             for x in INDICS], sc or group)))

        #print INDICS_HEADERS, tabular, locateds
        return (title, desc, group, attrs, markup, cols, nat, tabular,
                locateds, INDICS_HEADERS)
예제 #11
0
    def get_tables(self):
        cnds = self.navb.conditions()
        cnds.update({"(created_at) <= '%s'" % (self.navb.finish): ''})
        cnds.update({"(created_at) >= '%s'" % (self.navb.start): ''})

        cnds, markup, cols = self.navb.neater_tables(
            cnds=cnds,
            extras=[('national_id', 'Mother ID'),
                    ('user_phone', 'Reporter Phone'),
                    ('birth_date', 'Birth date'),
                    ('child_number', 'Child Number'),
                    ('child_weight', 'Child Weight'),
                    ('mother_weight', 'Mother Weight'),
                    ('created_at', 'Submission Date')])

        REDINDICS = [
            ('red', 'total', 'Total RED'),
        ] + [(makecol(x[0]), x[0], x[1]) for x in queries.RED_DATA['attrs']]
        REDINDICSDICT = {x[0]: (x[1], x[2]) for x in REDINDICS}

        RARINDICS = [
            ('rar', 'total', 'Total RAR'),
        ] + [
            (makecol(x[0]), x[0], x[1]) for x in queries.RAR_DATA['attrs']
        ] + [(makecol(x[0]), x[0], x[1]) for x in queries.RAR_DATA['outs']]
        RARINDICSDICT = {x[0]: (x[1], x[2]) for x in RARINDICS}

        INDICS = REDINDICS
        title, sc, group, attrs, nat, tabular, locateds, INDICS_HEADERS = (
            '', '', '', [], [], [], [], [])

        if self.navb.kw.get('subcat'):
            sc = self.navb.kw.get('subcat')
            if self.navb.kw.get('group') == 'rar' or self.navb.kw.get(
                    'subcat') == 'rar':
                wcl = RARINDICSDICT[sc]  #;print wcl, RARINDICSDICT
                INDICS = [(sc, wcl[0], wcl[1])] if wcl else []
                INDICS_HEADERS = dict([(makecol(x[0]), x[2]) for x in INDICS])
                if wcl and wcl[0] != 'total': cnds.update({wcl[0]: ''})
                dcols = [x[0] for x in cols]
                nat = Redalert.fetch_log_redresults(cnds, dcols)
            else:
                wcl = REDINDICSDICT[sc]  #;print wcl, CCMINDICSDICT
                INDICS = [(sc, wcl[0], wcl[1])] if wcl else []
                INDICS_HEADERS = dict([(makecol(x[0]), x[2]) for x in INDICS])
                if wcl and wcl[0] != 'total': cnds.update({wcl[0]: ''})
                dcols = [x[0] for x in cols]
                nat = Redalert.fetch_log_redalerts(cnds, dcols)

        else:
            if self.navb.kw.get('group') == 'rar' or self.navb.kw.get(
                    'subcat') == 'rar':
                dcols = [x[0] for x in cols]
                nat = Redalert.fetch_log_redresults(cnds, dcols)
            else:
                dcols = [x[0] for x in cols]
                nat = Redalert.fetch_log_redalerts(cnds, dcols)

        if self.navb.kw.get(
                'view') == 'table' or self.navb.kw.get('view') != 'log':
            group_by = []
            group_by += [
                'province_pk'
            ] if self.navb.kw.get('nation') or not group_by else []
            group_by += ['district_pk'] if self.navb.kw.get('province') else []
            group_by += ['referral_facility_pk'
                         ] if self.navb.kw.get('district') else []
            group_by += ['facility_pk'] if self.navb.kw.get('hd') else []
            #print "\nGROUP BY: %s \n" % group_by
            LOCS = {
                'nation': self.navb.kw.get('nation'),
                'province': self.navb.kw.get('province'),
                'district': self.navb.kw.get('district'),
                'hospital': self.navb.kw.get('hd'),
                'location': self.navb.kw.get('hc')
            }

            if self.navb.kw.get('subcat') in [x[0] for x in REDINDICS]:
                #print REDINDICS, LOCS, cnds, group_by
                locateds = Redalert.fetch_redalerts_by_location(
                    cnds, group_by=group_by, INDICS=REDINDICS)
            elif self.navb.kw.get('subcat') in [x[0] for x in RARINDICS]:
                #print RARINDICS, LOCS, cnds, group_by
                locateds = Redalert.fetch_redresults_by_location(
                    cnds, group_by=group_by, INDICS=RARINDICS)
            else:
                INDICS = REDINDICS + RARINDICS
                locateds = Redalert.fetch_redalerts_by_location(
                    cnds, group_by=group_by, INDICS=REDINDICS)
                locateds += Redalert.fetch_redresults_by_location(
                    cnds, group_by=group_by, INDICS=RARINDICS)
                #print [[y.__dict__ for y in x] for x in locateds]#, INDICS, LOCS, self.navb.locs()

            tabular = give_me_table(locateds,
                                    self.navb.locs(),
                                    INDICS=INDICS,
                                    LOCS=LOCS)
            #print locateds, "\n", tabular

        INDICS_HEADERS = dict([(makecol(x[0]), x[2]) for x in INDICS])
        desc = 'Red alerts%s' % (
            ' (%s)' % (self.navb.find_descr([(makecol(x[0]), x[2])
                                             for x in INDICS], sc or group)))

        #print INDICS_HEADERS, tabular, locateds
        return (title, desc, group, attrs, markup, cols, nat, tabular,
                locateds, INDICS_HEADERS)