Exemplo n.º 1
0
    def cases(self):
        if 'debug_case' in self.request.GET:
            case = CommCareCase.get(self.request.GET['debug_case'])
            if case.domain != DOMAIN:
                raise Http404()
            return [case]

        query = case_es.CaseES().domain(self.domain)\
                .fields([])\
                .opened_range(lte=self.datespan.enddate_utc)\
                .case_type(self.default_case_type)
        query.index = 'report_cases'

        if self.case_status == 'open':
            query = query.filter(es_filters.OR(
                case_es.is_closed(False),
                case_es.closed_range(gte=self.datespan.enddate_utc)
            ))
        elif self.case_status == 'closed':
            query = query.filter(case_es.closed_range(lte=self.datespan.enddate_utc))

        query = query.owner([user['doc_id'] for user in self.users_matching_filter])

        result = query.run()

        return [
            CommCareCase.wrap(doc)
            for doc in iter_docs(CommCareCase.get_db(), result.ids)
        ]
Exemplo n.º 2
0
class TauxDeRuptureFluff(fluff.IndicatorDocument):
    document_class = XFormInstance
    document_filter = ANDFilter([
        FormPropertyFilter(xmlns=RAPTURE_XMLNSES[0]),
        IsExistFormPropertyFilter(xmlns=RAPTURE_XMLNSES[0],
                                  property_path="form",
                                  property_value='district')
    ])
    domains = INTRAHEALTH_DOMAINS
    deleted_types = IH_DELETED_TYPES
    save_direct_to_sql = True
    group_by = (fluff.AttributeGetter('product_name',
                                      lambda f: get_rupture_products(f)),
                fluff.AttributeGetter('product_code',
                                      lambda f: get_rupture_products_code(f)))

    region_id = flat_field(
        lambda f: get_location_id_by_type(form=f, type=u'r\xe9gion'))
    district_id = flat_field(
        lambda f: get_location_id_by_type(form=f, type='district'))
    district_name = flat_field(lambda f: f.form['district'])
    PPS_name = flat_field(
        lambda f: CommCareCase.get(f.form['case']['@case_id']).name)

    total_stock = report_calcs.RupturesDeStocks('pps_stocked_out')
Exemplo n.º 3
0
    def get_rows(self, datespan):
        def get_awc_filter(awcs):
            return get_nested_terms_filter("awc_name.#value", awcs)

        def get_gp_filter(gp):
            owner_ids = [user._id for user in self.users
                         if getattr(user, 'user_data', {}).get('gp') in self.gp]
            return es_filters.term("owner_id", owner_ids)

        def get_block_filter(block):
            return es_filters.term("block_name.#value", block.lower())

        query = case_es.CaseES().domain(self.domain)\
                .fields([])\
                .opened_range(lte=self.datespan.enddate_utc)\
                .term("type.exact", self.default_case_type)
        query.index = 'report_cases'

        if self.display_open_cases_only:
            query = query.filter(es_filters.OR(
                case_es.is_closed(False),
                case_es.closed_range(gte=self.datespan.enddate_utc)
            ))
        elif self.display_closed_cases_only:
            query = query.filter(case_es.closed_range(lte=self.datespan.enddate_utc))

        if self.awcs:
            query = query.filter(get_awc_filter(self.awcs))
        elif self.gp:
            query = query.filter(get_gp_filter(self.gp))
        elif self.block:
            query = query.filter(get_block_filter(self.block))
        result = query.run()
        return map(CommCareCase, iter_docs(CommCareCase.get_db(), result.ids))
Exemplo n.º 4
0
def referenced_case_attribute(case, field_name):
    if not case.indices[0]['referenced_id']:
        return ""
    referenced_case = CommCareCase.get(case.indices[0]['referenced_id'])
    if hasattr(referenced_case, field_name):
        return getattr(referenced_case, field_name)
    else:
        return ""
Exemplo n.º 5
0
def get_case_property(form, property_name):
    case_dict = form.form.get('case')
    if not case_dict:
        return None
    case_id = case_dict.get('@case_id')
    if not case_id:
        return None
    case = CommCareCase.get(case_id)
    return case.get_case_property(property_name)
Exemplo n.º 6
0
class LivraisonFluff(fluff.IndicatorDocument):
    document_class = XFormInstance
    document_filter = FormPropertyFilter(xmlns=LIVRAISON_XMLNSES[0])

    domains = INTRAHEALTH_DOMAINS
    group_by = ('domain', )
    save_direct_to_sql = True
    deleted_types = IH_DELETED_TYPES

    month = flat_field(lambda f: get_month(f, 'mois_visite'))
    duree_moyenne_livraison = report_calcs.DureeMoyenneLivraison()

    region_id = flat_field(
        lambda f: get_location_id_by_type(form=f, type=u'r\xe9gion'))
    district_id = flat_field(
        lambda f: CommCareCase.get(f.form['case']['@case_id']).location_id)
    district_name = flat_field(
        lambda f: CommCareCase.get(f.form['case']['@case_id']).name)
Exemplo n.º 7
0
    def get_rows(self, datespan):
        cases = []
        self.form_sql_data = OpmFormSqlData(domain=DOMAIN, case_id=None, datespan=self.datespan)
        for case_id in self.form_sql_data.data.keys():
            try:
                cases.append(CommCareCase.get(case_id))
            except ResourceNotFound:
                pass

        return [case for case in cases if self.passes_filter(case)]
Exemplo n.º 8
0
    def get_rows(self, datespan):
        cases = []
        self.form_sql_data = OpmFormSqlData(domain=DOMAIN, case_id=None, datespan=self.datespan)
        for case_id in self.form_sql_data.data.keys():
            try:
                cases.append(CommCareCase.get(case_id))
            except ResourceNotFound:
                pass

        return [case for case in cases if self.passes_filter(case)]
Exemplo n.º 9
0
    def handle(self, *args, **options):

        self.stdout.write("Pulling stuff\n")
        beneficiaries = CommCareCase.get_all_cases('opm', include_docs=True)
        users = CommCareUser.by_domain('opm')
        fixtures = FixtureDataItem.get_item_list('opm', 'condition_amounts')
        # you won't be able to get the fixtures without the FixtureDataType
        fixtures.append(FixtureDataType.by_domain_tag('opm',
            'condition_amounts').one())
        forms = []

        for b in beneficiaries:
            forms += b.get_forms()

        test_data = []

        month, year = test_month_year
        for report_class in [IncentivePaymentReport, BeneficiaryPaymentReport]:
            self.stdout.write("Running %s\n" % report_class.__name__)
            report = get_report(report_class, month, year)
            snapshot = OpmReportSnapshot(
                domain=DOMAIN,
                month=month,
                year=year,
                report_class=report.report_class.__name__,
                headers=report.headers,
                slugs=report.slugs,
                rows=report.rows,
            )
            test_data.append(snapshot.to_json())

        self.stdout.write("Saving raw data\n")
        test_data += [form.to_json() for form in forms]
        test_data += [u.to_json() for u in users]
        test_data += [b.to_json() for b in beneficiaries]
        test_data += [f.to_json() for f in fixtures]

        doc_ids = set()
        docs = []
        for doc in test_data:
            if doc.get('_id') not in doc_ids:
                for attrib in ['_rev', '_attachments']:
                    try:
                        del doc[attrib]
                    except KeyError:
                        pass                
                doc_ids.add(doc.get('_id', 'null'))
                docs.append(doc)
            else:
                self.stdout.write("Ignoring duplicates\n")

        with open(test_data_location, 'w') as f:
            f.write(json.dumps(docs, indent=2))
            
        self.stdout.write("Pulled stuff, let's hope it worked\n")
Exemplo n.º 10
0
    def get_rows(self):
        query = case_es.CaseES().domain(self.domain)\
                .fields([])\
                .opened_range(lte=self.datespan.enddate_utc)\
                .case_type(self.default_case_type)
        query.index = 'report_cases'

        if self.case_status == 'open':
            query = query.filter(es_filters.OR(
                case_es.is_closed(False),
                case_es.closed_range(gte=self.datespan.enddate_utc)
            ))
        elif self.case_status == 'closed':
            query = query.filter(case_es.closed_range(lte=self.datespan.enddate_utc))

        query = query.owner([user['doc_id'] for user in self.users_matching_filter])

        result = query.run()

        return [
            CommCareCase.wrap(doc)
            for doc in iter_docs(CommCareCase.get_db(), result.ids)
        ]
Exemplo n.º 11
0
    def get_rows(self, datespan):
        def get_awc_filter(awcs):
            return get_nested_terms_filter("awc_name.#value", awcs)

        def get_gp_filter(gp):
            owner_ids = [
                user._id for user in self.users
                if getattr(user, 'user_data', {}).get('gp') in self.gp
            ]
            return es_filters.term("owner_id", owner_ids)

        def get_block_filter(block):
            return es_filters.term("block_name.#value", block.lower())

        query = case_es.CaseES().domain(self.domain)\
                .fields([])\
                .opened_range(lte=self.datespan.enddate_utc)\
                .term("type.exact", self.default_case_type)
        query.index = 'report_cases'

        if self.display_open_cases_only:
            query = query.filter(
                es_filters.OR(
                    case_es.is_closed(False),
                    case_es.closed_range(gte=self.datespan.enddate_utc)))
        elif self.display_closed_cases_only:
            query = query.filter(
                case_es.closed_range(lte=self.datespan.enddate_utc))

        if self.awcs:
            query = query.filter(get_awc_filter(self.awcs))
        elif self.gp:
            query = query.filter(get_gp_filter(self.gp))
        elif self.block:
            query = query.filter(get_block_filter(self.block))
        result = query.run()
        return map(CommCareCase, iter_docs(CommCareCase.get_db(), result.ids))
Exemplo n.º 12
0
 def get_rows(self, datespan):
     return CommCareCase.get_all_cases(DOMAIN, include_docs=True)
Exemplo n.º 13
0
    def __init__(self, case, report):
        if report.snapshot is not None:
            report.filter(
                lambda key: case['_source'][key],
                # case.awc_name, case.block_name
                [('awc_name', 'awcs'), ('block_name', 'block'), ('owner_id', 'gp'), ('closed', 'is_open')],
            )
        if not report.is_rendered_as_email:
            img_elem = '<div style="width:100px !important;"><img src="/static/opm/img/%s"></div>'
        else:
            img_elem = '<div><img src="/static/opm/img/%s"></div>'
        def condition_image(image_y, image_n, condition):
            if condition is None:
                return ''
            elif condition is True:
                return img_elem % image_y
            elif condition is False:
                return img_elem % image_n

        met = {
            'window_1_1': None,
            'window_1_2': None,
            'window_2_1': None,
            'window_2_2': None,
            'attendance_vhnd_3': None,
            'attendance_vhnd_6': None,
            'child1_vhndattend_calc': None,
            'prev_child1_vhndattend_calc': None,
            'child1_attendance_vhnd': None,
            'weight_tri_1': None,
            'prev_weight_tri_1': None,
            'weight_tri_2': None,
            'prev_weight_tri_2': None,
            'child1_growthmon_calc': None,
            'prev_child1_growthmon_calc': None,
            'child1_excl_breastfeed_calc': None,
            'prev_child1_excl_breastfeed_calc': None,
            'child1_ors_calc': None,
            'prev_child1_ors_calc': None,
            'child1_weight_calc': None,
            'child1_register_calc': None,
            'child1_measles_calc': None,
            'prev_child1_weight_calc': None,
            'prev_child1_register_calc': None,
            'prev_child1_measles_calc': None,
            'child1_suffer_diarrhea': None,
            'interpret_grade_1': None
        }

        def get_property(obj, name, default=None):
            if name in obj:
                if type(obj[name]) is dict:
                    return obj[name]
                return obj[name]
            else:
                return default if default is not None else EMPTY_FIELD

        def get_property_from_forms(forms, met_properties):
            for form in forms:
                for k, v in met_properties.iteritems():
                    if k in ['child1_suffer_diarrhea', 'child1_growthmon_calc', 'prev_child1_growthmon_calc']:
                        if 'child_1' in form.form and k in form.form['child_1']:
                            met_properties[k] = form.form['child_1'][k]
                    else:
                        if k in form.form:
                            met_properties[k] = form.form[k]
            return met_properties

        try:
            case_obj = CommCareCase.get(case['_source']['_id'])
        except ResourceNotFound:
            raise InvalidRow
        case_property = lambda _property, default: get_property(case_obj, _property, default=default)

        self.case_id = case_property('_id', '')
        self.block_name = case_property('block_name', '')
        self.owner_id = case_property('owner_id', '')
        self.closed = case_property('closed', False)

        self.name = case_property('name', EMPTY_FIELD)
        self.awc_name = case_property('awc_name', EMPTY_FIELD)
        self.husband_name = case_property('husband_name', EMPTY_FIELD)

        reporting_month = report.month
        reporting_year = report.year
        reporting_date = datetime.date(reporting_year, reporting_month + 1, 1) - datetime.timedelta(1)
        
        dod_date = case_property('dod', EMPTY_FIELD)
        edd_date = case_property('edd', EMPTY_FIELD)
        status = "unknown"
        preg_month = -1
        child_age = -1
        window = -1
        if dod_date == EMPTY_FIELD and edd_date == EMPTY_FIELD:
            raise InvalidRow
        if dod_date and dod_date != EMPTY_FIELD:
            if dod_date >= reporting_date:
                status = 'pregnant'
                preg_month = 9 - (dod_date - reporting_date).days / 30 # edge case
            elif dod_date < reporting_date:
                status = 'mother'
                child_age = 1 + (reporting_date - dod_date).days / 30
        elif edd_date and edd_date != EMPTY_FIELD:
            if edd_date >= reporting_date:
                status = 'pregnant'
                preg_month = 9 - (edd_date - reporting_date).days / 30
            elif edd_date < reporting_date: # edge case
                raise InvalidRow

        if status == 'pregnant' and (preg_month > 3 and preg_month < 10):
            window = (preg_month - 1) / 3
        elif status == 'mother' and (child_age > 0 and child_age < 37):
            window = (child_age - 1) / 3 + 1
        else:
            raise InvalidRow

        if status == "unknown" or window == -1 or (child_age == -1 and preg_month == -1):
            raise InvalidRow

        self.status = status
        self.child_age = child_age
        self.preg_month = preg_month
        self.window = window

        # confirm birth-spacing condition, if it's not needed forms need not be fetched for pregnant cases
        birth_spacing_prompt = []
        child_birth_weight_taken = None # None - condition n/a, True - condition met, False - condition not met
        child_excusive_breastfed = None
        if self.status == 'mother':
            forms = case_obj.get_forms()
            for form in forms:
                if 'birth_spacing_prompt' in form.form:
                    birth_spacing_prompt.append(form.form['birth_spacing_prompt'])  

            form_start_date, form_end_date = None, None
            filtered_forms = [form for form in forms if report.datespan.startdate <= form.received_on <= report.datespan.enddate]   
            if self.child_age == 3:
                prev_forms = [form for form in forms if report.datespan.startdate - datetime.timedelta(90) <= form.received_on <= report.datespan.enddate]
                weight_key = "child1_child_weight"
                child_forms = [form.form["child_1"] for form in prev_forms if "child_1" in form.form]
                birth_weight = [child[weight_key] for child in child_forms if weight_key in child]
                child_birth_weight_taken = '1' in birth_weight
            if self.child_age == 6:
                prev_forms = [form for form in forms if report.datespan.startdate - datetime.timedelta(180) <= form.received_on <= report.datespan.enddate]
                excl_key = "child1_child_excbreastfed"
                child_forms = [form.form["child_1"] for form in prev_forms if "child_1" in form.form]
                exclusive_breastfed = [child[excl_key] for child in child_forms if excl_key in child]
                child_excusive_breastfed = exclusive_breastfed == ['1', '1', '1', '1', '1', '1']
            get_property_from_forms(filtered_forms, met)

        vhnd_attendance = {
            4: case_property('attendance_vhnd_1', 0),
            5: case_property('attendance_vhnd_2', 0),
            6: case_property('attendance_vhnd_3', 0),
            7: case_property('month_7_attended', 0),
            8: case_property('month_8_attended', 0)
        }
        if self.status == 'pregnant':
            met_one, met_two, met_three, met_four, met_five = None, None, None, None, None
            self.child_age = EMPTY_FIELD
            if self.preg_month != 9:
                met_one = vhnd_attendance[self.preg_month] == '1'
            if self.preg_month == 6:
                met_two = 'received' in [case_property('weight_tri_1', 0), case_property('prev_weight_tri_1', 0)]
                if report.block.lower() == "atri":
                    met_three = case_property('ifa_tri_1', 0) == 'received'
            if self.preg_month == 9:
                met_two = 'received' in [case_property('weight_tri_1', 0), case_property('prev_weight_tri_1', 0)]         
            
            self.one = condition_image(M_ATTENDANCE_Y, M_ATTENDANCE_N, met_one)
            self.two = condition_image(M_WEIGHT_Y, M_WEIGHT_N, met_two)
            self.three = condition_image(IFA_Y, IFA_N, met_three)
            self.four = ''

        if self.status == 'mother':
            self.preg_month = EMPTY_FIELD
            met_one, met_two, met_three, met_four, met_five = None, None, None, None, None
            self.one, self.two, self.three, self.four, self.five = '','','','',''
            if self.child_age != 1:
                met_one = 'received' in [met['child1_vhndattend_calc'], met['prev_child1_vhndattend_calc'], met['child1_attendance_vhnd']]
                self.one = condition_image(C_ATTENDANCE_Y, C_ATTENDANCE_N, met_one)
            if self.child_age % 3 == 0:
                met_two = 'received' in [met['child1_growthmon_calc'], met['prev_child1_growthmon_calc']]
                if met['child1_suffer_diarrhea'] == '1':
                    met_three = 'received' in [met['child1_ors_calc'], met['prev_child1_ors_calc']]
                    self.three = condition_image(ORSZNTREAT_Y, ORSZNTREAT_N, met_three)
                self.two = condition_image(C_WEIGHT_Y, C_WEIGHT_N, met_two)
            if self.child_age == 3 and report.block.lower() == 'atri':
                met_four = child_birth_weight_taken
                self.four = condition_image(CHILD_WEIGHT_Y, CHILD_WEIGHT_N, met_four)
            if self.child_age == 6 and report.block.lower() == 'atri':
                met_four = 'received' in [met['child1_register_calc'] or met['prev_child1_register_calc']]
                met_five = child_excusive_breastfed
                self.four = condition_image(C_REGISTER_Y, C_REGISTER_N, met_four)
                self.five = condition_image(EXCBREASTFED_Y, EXCBREASTFED_N, met_five)
            if self.child_age == 12 and report.block.lower() == 'atri':
                met_four = 'received' in [met['child1_measles_calc'] or met['prev_child1_measles_calc']]
                self.four = condition_image(MEASLEVACC_Y, MEASLEVACC_N, met_four)


        if self.status == 'pregnant':
            if report.block.lower() == 'wazirganj':
                if child_age > 23 and '1' in birth_spacing_prompt:
                    self.five = img_elem % SPACING_PROMPT_Y
                else:
                    self.five = img_elem % SPACING_PROMPT_N
            else:
                self.five = ''

        if report.block.lower() == 'atri':
            if child_age == 24:
                self.cash = '<span style="color: green;">Rs. 2000</span>'
            elif child_age == 36:
                self.cash = '<span style="color: green;">Rs. 3000</span>'

        met_or_not = not False in [met_one, met_two, met_three, met_four, met_five]
        block = report.block.lower()
        year_end_condition = '1' in birth_spacing_prompt if block is 'wazirganj' else met['interpret_grade_1'] is 'normal'
        year_end_condition_img_Y = (SPACING_PROMPT_Y if block is 'wazirganj' else GRADE_NORMAL_Y)
        year_end_condition_img_N = (SPACING_PROMPT_N if block is 'wazirganj' else GRADE_NORMAL_N)
        if child_age == 24 and year_end_condition and met_or_not:
            self.five = img_elem % year_end_condition_img_Y
            self.cash = '<span style="color: green;">Rs. 2250</span>'   
        if child_age == 24 and year_end_condition and not met_or_not:
            self.five = img_elem % year_end_condition_img_Y
            self.cash = '<span style="color: green;">Rs. 2000</span>'
        if child_age == 24 and not year_end_condition and met_or_not:
            self.five = img_elem % year_end_condition_img_N
            self.cash = '<span style="color: green;">Rs. 250</span>'
        if child_age == 36 and year_end_condition and met_or_not:
            self.five = img_elem % year_end_condition_img_Y
            self.cash = '<span style="color: green;">Rs. 3250</span>'  
        if child_age == 36 and year_end_condition and not met_or_not:
            self.five = img_elem % year_end_condition_img_Y
            self.cash = '<span style="color: green;">Rs. 3000</span>'
        if child_age == 36 and not year_end_condition and met_or_not:
            self.five = img_elem % year_end_condition_img_N
            self.cash = '<span style="color: green;">Rs. 250</span>'
        elif met_or_not:
            self.cash = '<span style="color: green;">Rs. 250</span>'
        else:
            self.cash = '<span style="color: red;">Rs. 0</span>'

        if report.is_rendered_as_email:
            with localize('hin'):
                self.status = _(self.status)
Exemplo n.º 14
0
    def __init__(self, case, report):
        if report.snapshot is not None:
            report.filter(
                lambda key: case["_source"][key],
                # case.awc_name, case.block_name
                [("awc_name", "awcs"), ("block_name", "block"), ("owner_id", "gp"), ("closed", "is_open")],
            )
        img_elem = '<div style="width:100px !important;"><img src="/static/opm/img/%s"></div>'

        met = {
            "window_1_1": None,
            "window_1_2": None,
            "window_2_1": None,
            "window_2_2": None,
            "attendance_vhnd_3": None,
            "attendance_vhnd_6": None,
            "child1_vhndattend_calc": None,
            "prev_child1_vhndattend_calc": None,
            "child1_attendance_vhnd": None,
            "weight_tri_1": None,
            "prev_weight_tri_1": None,
            "weight_tri_2": None,
            "prev_weight_tri_2": None,
            "child1_growthmon_calc": None,
            "prev_child1_growthmon_calc": None,
            "child1_excl_breastfeed_calc": None,
            "prev_child1_excl_breastfeed_calc": None,
            "child1_ors_calc": None,
            "prev_child1_ors_calc": None,
            "child1_weight_calc": None,
            "child1_register_calc": None,
            "child1_measles_calc": None,
            "prev_child1_weight_calc": None,
            "prev_child1_register_calc": None,
            "prev_child1_measles_calc": None,
            "child1_suffer_diarrhea": None,
            "interpret_grade_1": None,
        }

        def get_property(obj, name, default=None):
            if name in obj:
                if type(obj[name]) is dict:
                    return obj[name]
                return obj[name]
            else:
                return default if default is not None else EMPTY_FIELD

        def get_property_from_forms(forms, met_properties):
            for form in forms:
                for k, v in met_properties.iteritems():
                    if k == "child1_suffer_diarrhea":
                        if "child_1" in form.form and k in form.form["child_1"]:
                            met_properties[k] = form.form["child_1"][k]
                    else:
                        if k in form.form:
                            met_properties[k] = form.form[k]
            return met_properties

        case_obj = CommCareCase.get(case["_source"]["_id"])
        self.case_id = get_property(case_obj, "_id", "")
        self.block_name = get_property(case_obj, "block_name", "")
        self.owner_id = get_property(case_obj, "owner_id", "")
        self.closed = get_property(case_obj, "closed", False)
        forms = case_obj.get_forms()
        birth_spacing_prompt = []
        for form in forms:
            if "birth_spacing_prompt" in form.form:
                birth_spacing_prompt.append(form.form["birth_spacing_prompt"])

        filtered_forms = [
            form
            for form in case_obj.get_forms()
            if report.datespan.startdate <= form.received_on <= report.datespan.enddate
        ]

        get_property_from_forms(filtered_forms, met)

        dod = get_property(case_obj, "dod")
        if dod and dod != EMPTY_FIELD:
            try:
                child_age = len(
                    months_between(datetime.datetime(dod.year, dod.month, dod.day), datetime.datetime.now())
                )
            except AssertionError:
                child_age = -1
        else:
            child_age = -1

        if get_property(case_obj, "mother_preg_outcome", "") == "1":
            self.status = "mother"
        elif get_property(case_obj, "mother_preg_outcome", "") == "":
            self.status = "pregnant"
        else:
            raise InvalidRow

        met_one = False
        met_two = False
        met_three = False
        met_four = False
        met_five = False
        preg_month = get_property(case_obj, "pregnancy_month", 0) or 0
        if self.status == "pregnant":
            if "1" in [
                met["window_1_1"],
                met["window_1_2"],
                met["window_2_1"],
                met["window_2_2"],
                met["attendance_vhnd_3"],
                met["attendance_vhnd_6"],
            ]:
                met_one = True
            if (preg_month == "6" and "1" in [met["weight_tri_1"], met["prev_weight_tri_1"]]) or (
                preg_month == "9" and "1" in [met["weight_tri_2"], met["prev_weight_tri_2"]]
            ):
                met_two = True
        elif self.status == "mother":
            if "1" in [
                met["child1_vhndattend_calc"],
                met["prev_child1_vhndattend_calc"],
                met["child1_attendance_vhnd"],
            ]:
                met_one = True
            if "1" in [met["child1_growthmon_calc"], met["prev_child1_growthmon_calc"]]:
                met_two = True
            if (
                (child_age == 3 and (met["child1_weight_calc"] or met["prev_child1_weight_calc"]))
                or (child_age == 6 and (met["child1_register_calc"] or met["prev_child1_register_calc"]))
                or (child_age == 9 and (met["child1_measles_calc"] or met["prev_child1_measles_calc"]))
            ):
                met_three = True
            if child_age == 6 and (
                "1" in [met["child1_excl_breastfeed_calc"], met["prev_child1_excl_breastfeed_calc"]]
            ):
                met_four = True
            if child_age in [6, 9, 12] and "1" in [met["child1_ors_calc"], met["prev_child1_ors_calc"]]:
                met_five = True

        self.name = get_property(case_obj, "name")
        self.awc_name = get_property(case_obj, "awc_name")
        self.husband_name = get_property(case_obj, "husband_name")
        self.window = get_property(case_obj, "which_window")
        if self.status == "pregnant":
            self.preg_month = get_property(case_obj, "pregnancy_month")
            self.child_age = EMPTY_FIELD
            self.one = img_elem % M_ATTENDANCE_Y if preg_month == "9" else img_elem % M_ATTENDANCE_N
            self.two = img_elem % M_WEIGHT_Y if preg_month in ["6", "9"] else img_elem % M_WEIGHT_N
            self.three = img_elem % IFA_Y if int(preg_month) < 7 else img_elem % IFA_N
            self.four = ""
            if report.block.lower() == "wazirganj":
                if child_age > 23 and "1" in birth_spacing_prompt:
                    self.five = img_elem % SPACING_PROMPT_Y
                else:
                    self.five = img_elem % SPACING_PROMPT_N
            else:
                self.five = ""

        elif self.status == "mother":
            self.preg_month = EMPTY_FIELD
            if child_age != -1:
                self.child_age = child_age
            else:
                self.child_age = EMPTY_FIELD

            self.one = img_elem % C_ATTENDANCE_Y if 0 <= child_age <= 1 else img_elem % C_ATTENDANCE_N
            self.two = img_elem % C_WEIGHT_Y if child_age % 3 == 0 else img_elem % C_WEIGHT_N

            if child_age == 3:
                if met["child1_weight_calc"] or met["prev_child1_weight_calc"]:
                    self.three = img_elem % CHILD_WEIGHT_Y
                else:
                    self.three = img_elem % CHILD_WEIGHT_N
            elif child_age == 6:
                if met["child1_register_calc"] or met["prev_child1_register_calc"]:
                    self.three = img_elem % C_REGISTER_Y
                else:
                    self.three = img_elem % C_REGISTER_N
            elif child_age == 9:
                if met["child1_measles_calc"] or met["prev_child1_measles_calc"]:
                    self.three = img_elem % MEASLEVACC_Y
                else:
                    self.three = img_elem % MEASLEVACC_N
            else:
                self.three = ""

            self.four = img_elem % EXCBREASTFED_Y if child_age == 6 else img_elem % EXCBREASTFED_N

            if child_age in [3, 6, 9]:
                if met["child1_suffer_diarrhea"] == "1":
                    self.five = img_elem % ORSZNTREAT_Y
                else:
                    self.five = img_elem % ORSZNTREAT_N
            elif child_age == [24, 36]:
                if met["interpret_grade_1"] == "normal":
                    self.five = img_elem % GRADE_NORMAL_Y
                else:
                    self.five = img_elem % GRADE_NORMAL_N
            else:
                self.five = ""

        if report.block.lower() == "atri":
            if child_age == 24:
                self.cash = '<span style="color: green;">Rs. 2000</span>'
            elif child_age == 36:
                self.cash = '<span style="color: green;">Rs. 3000</span>'
            elif met_one or met_two or met_three or met_four or met_five:
                self.cash = '<span style="color: green;">Rs. 250</span>'
            else:
                self.cash = '<span style="color: red;">Rs. 0</span>'

        elif report.block.lower() == "wazirganj":
            if met_one or met_two or met_four or met_five:
                self.cash = '<span style="color: green;">Rs. 250</span>'
            else:
                self.cash = '<span style="color: red;">Rs. 0</span>'
Exemplo n.º 15
0
def get_case(form):
    case_id = form.form['case']['@case_id']
    return CommCareCase.get(case_id)
Exemplo n.º 16
0
    def __init__(self, case, report):
        report.filter(
            lambda key: case['_source'][key],
            # case.awc_name, case.block_name
            [('awc_name', 'awcs'), ('block_name', 'block'), ('owner_id', 'gp'), ('closed', 'is_open')],
        )
        img_elem = '<div style="width:100px !important;"><img src="/static/opm/img/%s"></div>'

        met = {
            'window_1_1': None,
            'window_1_2': None,
            'window_2_1': None,
            'window_2_2': None,
            'attendance_vhnd_3': None,
            'attendance_vhnd_6': None,
            'child1_vhndattend_calc': None,
            'prev_child1_vhndattend_calc': None,
            'child1_attendance_vhnd': None,
            'weight_tri_1': None,
            'prev_weight_tri_1': None,
            'weight_tri_2': None,
            'prev_weight_tri_2': None,
            'child1_growthmon_calc': None,
            'prev_child1_growthmon_calc': None,
            'child1_excl_breastfeed_calc': None,
            'prev_child1_excl_breastfeed_calc': None,
            'child1_ors_calc': None,
            'prev_child1_ors_calc': None,
            'child1_weight_calc': None,
            'child1_register_calc': None,
            'child1_measles_calc': None,
            'prev_child1_weight_calc': None,
            'prev_child1_register_calc': None,
            'prev_child1_measles_calc': None,
            'child1_suffer_diarrhea': None,
            'interpret_grade_1': None
        }

        def get_property(obj, name, default=None):
            if name in obj:
                if type(obj[name]) is dict:
                    return obj[name]
                return obj[name]
            else:
                return default if default is not None else EMPTY_FIELD

        def get_property_from_forms(forms, met_properties):
            for form in forms:
                for k, v in met_properties.iteritems():
                    if k == 'child1_suffer_diarrhea':
                        if 'child_1' in form.form and k in form.form['child_1']:
                            met_properties[k] = form.form['child_1'][k]
                    else:
                        if k in form.form:
                            met_properties[k] = form.form[k]
            return met_properties

        case_obj = CommCareCase.get(case['_source']['_id'])
        self.block_name = get_property(case_obj, 'block_name', '')
        self.owner_id = get_property(case_obj, 'owner_id', '')
        self.closed = get_property(case_obj, 'closed', False)
        forms = case_obj.get_forms()
        birth_spacing_prompt = []
        for form in forms:
            if 'birth_spacing_prompt' in form.form:
                birth_spacing_prompt.append(form.form['birth_spacing_prompt'])

        filtered_forms = [form for form in case_obj.get_forms() if report.datespan.startdate <= form.received_on <= report.datespan.enddate]

        get_property_from_forms(filtered_forms, met)

        dod = get_property(case_obj, 'dod')
        if dod and dod != EMPTY_FIELD:
            try:
                child_age = len(months_between(datetime.datetime(dod.year, dod.month, dod.day), datetime.datetime.now()))
            except AssertionError:
                child_age = -1
        else:
            child_age = -1

        if get_property(case_obj, 'mother_preg_outcome', '') == '1':
            self.status = 'mother'
        elif get_property(case_obj, 'mother_preg_outcome', '') == '':
            self.status = 'pregnant'
        else:
            raise InvalidRow

        met_one = False
        met_two = False
        met_three = False
        met_four = False
        met_five = False
        preg_month = get_property(case_obj, 'pregnancy_month', 0) or 0
        if self.status == 'pregnant':
            if '1' in [met['window_1_1'], met['window_1_2'], met['window_2_1'], met['window_2_2'], met['attendance_vhnd_3'], met['attendance_vhnd_6']]:
                met_one = True
            if (preg_month == '6' and '1' in [met['weight_tri_1'], met['prev_weight_tri_1']]) or (preg_month == '9' and '1' in [met['weight_tri_2'], met['prev_weight_tri_2']]):
                met_two = True
        elif self.status == 'mother':
            if '1' in [met['child1_vhndattend_calc'], met['prev_child1_vhndattend_calc'], met['child1_attendance_vhnd']]:
                met_one = True
            if '1' in [met['child1_growthmon_calc'], met['prev_child1_growthmon_calc']]:
                met_two = True
            if (child_age == 3 and (met['child1_weight_calc'] or met['prev_child1_weight_calc'])) or \
                    (child_age == 6 and (met['child1_register_calc'] or met['prev_child1_register_calc'])) or \
                    (child_age == 9 and (met['child1_measles_calc'] or met['prev_child1_measles_calc'])):
                met_three = True
            if child_age == 6 and ('1' in [met['child1_excl_breastfeed_calc'], met['prev_child1_excl_breastfeed_calc']]):
                met_four = True
            if child_age in [6, 9, 12] and '1' in [met['child1_ors_calc'], met['prev_child1_ors_calc']]:
                met_five = True

        self.name = get_property(case_obj, 'name')
        self.awc_name = get_property(case_obj, 'awc_name')
        self.husband_name = get_property(case_obj, 'husband_name')
        self.window = get_property(case_obj, 'which_window')
        if self.status == 'pregnant':
            self.month = get_property(case_obj, 'pregnancy_month')
            self.one = img_elem % M_ATTENDANCE_Y if preg_month == '9' else img_elem % M_ATTENDANCE_N
            self.two = img_elem % M_WEIGHT_Y if preg_month in ['6', '9'] else img_elem % M_WEIGHT_N
            self.three = img_elem % IFA_Y if int(preg_month) < 7 else img_elem % IFA_N
            self.four = ''
            if report.block.lower() == 'wazirganj':
                if child_age > 23 and '1' in birth_spacing_prompt:
                    self.five = img_elem % SPACING_PROMPT_Y
                else:
                    self.five = img_elem % SPACING_PROMPT_N
            else:
                self.five = ''

        elif self.status == 'mother':
            if child_age != -1:
                self.month = child_age
            else:
                self.month = EMPTY_FIELD

            self.one = img_elem % C_ATTENDANCE_Y if 0 <= child_age <= 1 else img_elem % C_ATTENDANCE_N
            self.two = img_elem % C_WEIGHT_Y if child_age % 3 == 0 else img_elem % C_WEIGHT_N

            if child_age == 3:
                if met['child1_weight_calc'] or met['prev_child1_weight_calc']:
                    self.three = img_elem % CHILD_WEIGHT_Y
                else:
                    self.three = img_elem % CHILD_WEIGHT_N
            elif child_age == 6:
                if met['child1_register_calc'] or met['prev_child1_register_calc']:
                    self.three = img_elem % C_REGISTER_Y
                else:
                    self.three = img_elem % C_REGISTER_N
            elif child_age == 9:
                if met['child1_measles_calc'] or met['prev_child1_measles_calc']:
                    self.three = img_elem % MEASLEVACC_Y
                else:
                    self.three = img_elem % MEASLEVACC_N
            else:
                self.three = ''

            self.four = img_elem % EXCBREASTFED_Y if child_age == 6 else img_elem % EXCBREASTFED_N

            if child_age in [3, 6, 9]:
                if met['child1_suffer_diarrhea'] == '1':
                    self.five = img_elem % ORSZNTREAT_Y
                else:
                    self.five = img_elem % ORSZNTREAT_N
            elif child_age == [24, 36]:
                if met['interpret_grade_1'] == 'normal':
                    self.five = img_elem % GRADE_NORMAL_Y
                else:
                    self.five = img_elem % GRADE_NORMAL_N
            else:
                self.five = ''

        if report.block.lower() == 'atri':
            if child_age == 24:
                self.cash = '<span style="color: green;">Rs. 2000</span>'
            elif child_age == 36:
                self.cash = '<span style="color: green;">Rs. 3000</span>'
            elif met_one or met_two or met_three or met_four or met_five:
                self.cash = '<span style="color: green;">Rs. 250</span>'
            else:
                self.cash = '<span style="color: red;">Rs. 0</span>'

        elif report.block.lower == 'wazirganj':
            if met_one or met_two or met_four or met_five:
                self.cash = '<span style="color: green;">Rs. 250</span>'
            else:
                self.cash = '<span style="color: red;">Rs. 0</span>'
Exemplo n.º 17
0
def get_case(form):
    case_id = form.form['case']['@case_id']
    return CommCareCase.get(case_id)