Beispiel #1
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)
Beispiel #2
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'), ('indexcol', "ID")])

        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]
                markup.update({
                    'indexcol':
                    lambda x, _, __:
                    '<a href="/dashboards/report?tbl=rar&id=%s">View</a>' %
                    (x),
                })
                nat = Redalert.fetch_log_redresults(cnds, dcols)
            else:
                dcols = [x[0] for x in cols]
                markup.update({
                    'indexcol':
                    lambda x, _, __:
                    '<a href="/dashboards/report?tbl=red&id=%s">View</a>' %
                    (x),
                })
                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)