Ejemplo n.º 1
0
def and_calc(calculators, date_provider=default_date, indicator_calculator=None,
            group_by_provider=None, window=timedelta(days=1)):
    """
    Shortcut function for creating a SimpleCalculator with a filter that combines
    the filters of the calculators in an ANDFilter
    """
    return SimpleCalculator(
        date_provider=date_provider,
        filter=ANDFilter([calc._filter for calc in calculators if calc._filter]),
        indicator_calculator=indicator_calculator,
        group_by_provider=group_by_provider,
        window=window
    )
Ejemplo n.º 2
0
def _filtered_calc_alias(xmlns=None, property_path=None, property_value=None,
                         operator=xcalculators.EQUAL, indicator_calculator=None,
                         window=timedelta(days=1)):
    filter = FormPropertyFilter(xmlns=xmlns, property_path=property_path,
                                property_value=property_value,
                                operator=operator)

    return SimpleCalculator(
        date_provider=xcalculators.default_date,
        filter=filter,
        indicator_calculator=indicator_calculator,
        window=window
    )
Ejemplo n.º 3
0
def filtered_form_calc(xmlns=None, property_path=None, property_value=None,
                         operator=EQUAL, date_provider=default_date,
                         indicator_calculator=None, group_by_provider=None,
                         window=timedelta(days=1)):
    """
    Shortcut function for creating a SimpleCalculator with a FormPropertyFilter
    """
    filter = FormPropertyFilter(xmlns=xmlns, property_path=property_path,
                                property_value=property_value,
                                operator=operator)

    return SimpleCalculator(
        date_provider=date_provider,
        filter=filter,
        indicator_calculator=indicator_calculator,
        group_by_provider=group_by_provider,
        window=window
    )
Ejemplo n.º 4
0
class UnicefMalawiFluff(fluff.IndicatorDocument):
    document_class = XFormInstance
    document_filter = ANDFilter([
        NOTFilter(
            xcalc.FormPropertyFilter(
                xmlns='http://openrosa.org/user-registration')),
        NOTFilter(
            xcalc.FormPropertyFilter(
                xmlns='http://openrosa.org/user/registration')),
        NOTFilter(
            xcalc.FormPropertyFilter(
                xmlns='http://code.javarosa.org/devicereport')),
        xcalc.CustomFilter(lambda f: get_user_id(f) != 'demo_user'),
        xcalc.CustomFilter(lambda f: get_group_id(f)),
    ])

    domains = ('cvsulive', )
    group_by = (
        'domain',
        fluff.AttributeGetter('user_id', get_user_id),
        fluff.AttributeGetter('group_id', get_group_id),
        fluff.AttributeGetter('age', get_age),
        fluff.AttributeGetter('sex', get_sex),
    )
    group_by_type_map = {'age': fluff.TYPE_INTEGER}

    # ---------------------------------------------------------------------
    # incident resolution
    # ---------------------------------------------------------------------

    resolution_resolved_at_cvsu = SimpleCalculator(
        date_provider=date_provided,
        filter=ORFilter([
            ANDFilter([
                filter_action('mediation_provided'),
                filter_outcome('resolved', REPORT_INCIDENT_XMLNS)
            ]),
            filter_outcome('resolved', FOLLOWUP_FORM_XMLNS)
        ]))

    resolution_unresolved = SimpleCalculator(
        date_provider=date_provided,
        filter=ORFilter([
            ANDFilter([
                filter_action('mediation_provided'),
                filter_outcome('unresolved', REPORT_INCIDENT_XMLNS)
            ]),
            filter_outcome('unresolved', FOLLOWUP_FORM_XMLNS)
        ]))

    resolution_case_withdrawn = SimpleCalculator(
        date_provider=date_provided,
        filter=ORFilter([
            ANDFilter([
                filter_action('mediation_provided'),
                filter_outcome('case_withdrawn', REPORT_INCIDENT_XMLNS)
            ]),
            filter_outcome('case_withdrawn', FOLLOWUP_FORM_XMLNS)
        ]))

    resolution_referred_ta = SimpleCalculator(
        date_provider=date_reported_provided_mediated,
        filter=ORFilter([
            ANDFilter([
                filter_action('immediate_referral'),
                filter_immediate_referral_org('ta')
            ]),
            ANDFilter([
                filter_outcome('mediation_outcome_referred'),
                filter_referral_org('ta')
            ])
        ]))

    resolution_referral_ta_court = SimpleCalculator(
        date_provider=date_reported_provided_mediated,
        filter=ORFilter([
            ANDFilter([
                filter_action('immediate_referral'),
                filter_immediate_referral_org('ta_court')
            ]),
            ANDFilter([
                filter_outcome('mediation_outcome_referred'),
                filter_referral_org('ta_court')
            ])
        ]))

    resolution_referral_police = SimpleCalculator(
        date_provider=date_reported_provided_mediated,
        filter=ORFilter([
            ANDFilter([
                filter_action('immediate_referral'),
                filter_immediate_referral_org('police')
            ]),
            ANDFilter([
                filter_outcome('mediation_outcome_referred'),
                filter_referral_org('med_ref_police')
            ])
        ]))

    resolution_referral_social_welfare = SimpleCalculator(
        date_provider=date_reported_provided_mediated,
        filter=ORFilter([
            ANDFilter([
                filter_action('immediate_referral'),
                filter_immediate_referral_org('social_welfare')
            ]),
            ANDFilter([
                filter_outcome('mediation_outcome_referred'),
                filter_referral_org('med_ref_social_welfare')
            ])
        ]))

    resolution_referral_ngo = SimpleCalculator(
        date_provider=date_reported_provided_mediated,
        filter=ORFilter([
            ANDFilter([
                filter_action('immediate_referral'),
                filter_immediate_referral_org('ngo')
            ]),
            ANDFilter([
                filter_outcome('mediation_outcome_referred'),
                filter_referral_org('med_ref_ngo')
            ])
        ]))

    resolution_referral_other = SimpleCalculator(
        date_provider=date_reported_provided_mediated,
        filter=ORFilter([
            ANDFilter([
                filter_action('immediate_referral'),
                filter_immediate_referral_org('referral_other')
            ]),
            ANDFilter([
                filter_outcome('mediation_outcome_referred'),
                filter_referral_org('med_ref_other')
            ])
        ]))

    resolution_other = SimpleCalculator(
        date_provider=date_reported_provided_mediated,
        filter=ORFilter([
            filter_action('actions_other'),
            filter_outcome('other_mediation_outcome', REPORT_INCIDENT_XMLNS),
            filter_outcome('other', FOLLOWUP_FORM_XMLNS)
        ]),
    )

    resolution_total = xcalc.or_calc(
        [
            resolution_resolved_at_cvsu, resolution_referred_ta,
            resolution_referral_ta_court, resolution_referral_police,
            resolution_referral_social_welfare, resolution_referral_ngo,
            resolution_referral_other, resolution_unresolved, resolution_other
        ],
        date_provider=date_reported_provided_mediated,
    )

    # ---------------------------------------------------------------------
    # services
    # ---------------------------------------------------------------------

    service_referral = SimpleCalculator(
        date_provider=date_reported_mediated,
        filter=ORFilter([
            filter_action('immediate_referral'),
            filter_service('referral_hostpital'),
            filter_outcome('mediation_outcome_referred')
        ]))

    service_mediation = SimpleCalculator(
        date_provider=date_reported,
        filter=ORFilter([
            filter_action('mediation_scheduled'),
            filter_action('mediation_provided')
        ]))

    service_counselling = SimpleCalculator(date_provider=date_reported,
                                           filter=ORFilter([
                                               filter_service('counselling'),
                                               filter_service('couselling')
                                           ]))

    service_psychosocial_support = SimpleCalculator(
        date_provider=date_reported,
        filter=filter_service('psychosocial_support'))

    service_first_aid = SimpleCalculator(date_provider=date_reported,
                                         filter=filter_service('first_aid'))

    service_shelter = SimpleCalculator(date_provider=date_reported,
                                       filter=filter_service('shelter'))

    service_other = SimpleCalculator(date_provider=date_reported,
                                     filter=ORFilter([
                                         filter_action('actions_other'),
                                         filter_service('services_other')
                                     ]))

    service_total = xcalc.or_calc(
        [
            service_referral, service_mediation, service_counselling,
            service_psychosocial_support, service_first_aid, service_shelter,
            service_other
        ],
        date_provider=date_reported_mediated,
    )

    # ---------------------------------------------------------------------
    # outreach
    # ---------------------------------------------------------------------

    incidents = SimpleCalculator(
        date_provider=date_reported,
        filter=xcalc.FormPropertyFilter(xmlns=REPORT_INCIDENT_XMLNS))

    outreach = SimpleCalculator(
        date_provider=lambda form: form.form.get('date', form.received_on),
        filter=xcalc.FormPropertyFilter(xmlns=OUTREACH_FORM_XMLNS))

    iga = SimpleCalculator(
        date_provider=lambda form: form.form.get('start_date', form.received_on
                                                 ),
        filter=xcalc.FormPropertyFilter(xmlns=IGA_FORM_XMLNS))

    # ---------------------------------------------------------------------
    # abuse
    # ---------------------------------------------------------------------

    abuse_children_in_household = SimpleCalculator(
        date_provider=date_reported,
        filter=xcalc.FormPropertyFilter(xmlns=REPORT_INCIDENT_XMLNS),
        indicator_calculator=IntegerPropertyReference(
            'form/nr_children_in_household',
            transform=lambda x: 0 if x == 999 else x)  # unknown value = 999
    )

    abuse_children_abused = SimpleCalculator(
        date_provider=date_reported,
        filter=xcalc.FormPropertyFilter(xmlns=REPORT_INCIDENT_XMLNS),
        indicator_calculator=IntegerPropertyReference(
            'form/no_children_abused',
            transform=lambda x: 0 if x == 999 else x)  # unknown value = 999
    )

    abuse_category_physical = SimpleCalculator(date_provider=date_reported,
                                               filter=filter_abuse('physical'))

    abuse_category_sexual = SimpleCalculator(date_provider=date_reported,
                                             filter=filter_abuse('sexual'))

    abuse_category_psychological = SimpleCalculator(
        date_provider=date_reported, filter=filter_abuse('psychological'))

    abuse_category_exploitation = SimpleCalculator(
        date_provider=date_reported, filter=filter_abuse('exploitation'))

    abuse_category_neglect = SimpleCalculator(date_provider=date_reported,
                                              filter=filter_abuse('neglect'))

    abuse_category_other = SimpleCalculator(date_provider=date_reported,
                                            filter=filter_abuse('abuse_other'))

    abuse_category_total = xcalc.or_calc([
        abuse_category_physical, abuse_category_sexual,
        abuse_category_psychological, abuse_category_exploitation,
        abuse_category_neglect, abuse_category_other
    ],
                                         date_provider=date_reported)

    class Meta:
        app_label = 'cvsu'
Ejemplo n.º 5
0
def _and_alias(calculators):
    return SimpleCalculator(
        date_provider=xcalculators.default_date,
        filter=ANDFilter([calc._filter for calc in calculators if calc._filter]),
        window=timedelta(days=1)
    )