示例#1
0
    def get_fixture(self, restore_user, location_id):
        """
        Generate a fixture representation of all locations available to the user
        <fixture id="fixture:user-locations" user_id="4ce8b1611c38e953d3b3b84dd3a7ac18">
            <locations>
                <location name="Location 1" id="1039d1611c38e953d3b3b84ddc01d93"
                <!-- ... -->
            </locations>
        </fixture>
        """
        root = ElementTree.Element('fixture', {
            'id': self.id,
            'user_id': restore_user.user_id
        })

        locations_element = ElementTree.Element('locations')
        location = get_location(location_id)
        location_element = ElementTree.Element('location', {
            'name': location.name,
            'id': location.location_id
        })
        locations_element.append(location_element)

        root.append(locations_element)
        return root
def calculate_form_data(self, form):
    try:
        case_id = form["form"]["case"]["@case_id"]
        case = get_case_by_id(case_id)
    except KeyError:
        case = EMPTY_FIELD

    amount_due = EMPTY_FIELD
    if form["form"].get("registration_amount", None) is not None:
        amount_due = form["form"].get("registration_amount", None)
    elif form["form"].get("immunization_amount", None) is not None:
        amount_due = form["form"].get("immunization_amount", None)

    service_type = form["form"].get("service_type", EMPTY_FIELD)
    form_id = form["_id"]
    location_name = EMPTY_FIELD
    location_parent_name = EMPTY_FIELD
    location_id = form["form"].get("location_id", None)

    if location_id is not None:
        location = get_location(location_id)
        location_name = location.name
        location_parent = location.parent
        if location_parent is not None and location_parent.location_type_name != 'state':
            while location_parent is not None and location_parent.location_type_name not in ('district', 'lga'):
                location_parent = location_parent.parent
        location_parent_name = location_parent.name if location_parent is not None else EMPTY_FIELD

    return {'case': case, 'service_type': service_type, 'location_name': location_name,
            'location_parent_name': location_parent_name, 'amount_due': amount_due, 'form_id': form_id}
示例#3
0
    def get_fixture(self, restore_user, location_id):
        """
        Generate a fixture representation of all locations available to the user
        <fixture id="fixture:user-locations" user_id="4ce8b1611c38e953d3b3b84dd3a7ac18">
            <locations>
                <location name="Location 1" id="1039d1611c38e953d3b3b84ddc01d93"
                <!-- ... -->
            </locations>
        </fixture>
        """
        root = ElementTree.Element('fixture', {
            'id': self.id,
            'user_id': restore_user.user_id
        })

        locations_element = ElementTree.Element('locations')
        location = get_location(location_id)
        location_element = ElementTree.Element('location', {
            'name': location.name,
            'id': location.location_id
        })
        locations_element.append(location_element)

        root.append(locations_element)
        return root
示例#4
0
 def data_providers(self):
     config = self.report_config
     data_providers = []
     if config['location_id']:
         data_providers = [
             RandRSubmissionData(config=config, css_class='row_chart_all')
         ]
         location = get_location(config['location_id'])
         if location.location_type_name in ['REGION', 'MSDZONE', 'MOHSW']:
             data_providers.append(
                 RRStatus(config=config, css_class='row_chart_all'))
         elif location.location_type_name == 'FACILITY':
             return [
                 InventoryHistoryData(config=config),
                 RandRHistory(config=config),
                 Notes(config=config),
                 RecentMessages(config=config),
                 RegistrationData(config=dict(loc_type='FACILITY',
                                              **config),
                                  css_class='row_chart_all'),
                 RegistrationData(config=dict(loc_type='DISTRICT',
                                              **config),
                                  css_class='row_chart_all'),
                 RegistrationData(config=dict(loc_type='REGION', **config),
                                  css_class='row_chart_all')
             ]
         else:
             data_providers.append(
                 RRReportingHistory(config=config,
                                    css_class='row_chart_all'))
     return data_providers
 def data_providers(self):
     config = self.report_config
     location_types = [loc_type.name for loc_type in [loc_type for loc_type in Domain.get_by_name(self.domain).location_types if not loc_type.administrative]]
     if not self.needs_filters and get_location(config['location_id']).location_type_name in location_types:
         if self.is_rendered_as_email:
             return [FacilityReportData(config)]
         else:
             return [FacilityReportData(config),
                     StockLevelsLegend(config),
                     InputStock(config),
                     UsersData(config),
                     InventoryManagementData(config),
                     ProductSelectionPane(config, hide_columns=False)]
示例#6
0
def process_facility_statuses(facility_id, statuses, alerts=True):
    """
    For a given facility and list of statuses, update the appropriate
    data warehouse tables. This should only be called on supply points
    that are facilities.
    """
    facility = get_location(facility_id)
    for status in statuses:
        warehouse_date = _get_window_date(status.status_type,
                                          status.status_date)
        if _is_valid_status(facility, status.status_date, status.status_type):
            org_summary = OrganizationSummary.objects.get_or_create(
                location_id=facility_id, date=warehouse_date)[0]
            group_summary = GroupSummary.objects.get_or_create(
                org_summary=org_summary, title=status.status_type)[0]
            group_summary.total = 1
            if status.status_value not in (
                    SupplyPointStatusValues.REMINDER_SENT,
                    SupplyPointStatusValues.ALERT_SENT):
                # we've responded to this query
                group_summary.responded = 1
                if status.status_value in [
                        SupplyPointStatusValues.SUBMITTED,
                        SupplyPointStatusValues.RECEIVED
                ]:
                    group_summary.complete = 1
                else:
                    group_summary.complete = group_summary.complete or 0
                if group_summary.complete:
                    if is_on_time(status.status_date, warehouse_date,
                                  status.status_type):
                        group_summary.on_time = 1
                    else:
                        group_summary.on_time = group_summary.on_time
                else:
                    group_summary.on_time = 0

                group_summary.save()

                if alerts:
                    if status.status_value == SupplyPointStatusValues.NOT_SUBMITTED \
                            and status.status_type == SupplyPointStatusTypes.R_AND_R_FACILITY:
                        create_alert(facility_id, status.status_date,
                                     const.RR_NOT_SUBMITTED, {'number': 1})

                    if status.status_value == SupplyPointStatusValues.NOT_RECEIVED \
                            and status.status_type == SupplyPointStatusTypes.DELIVERY_FACILITY:
                        create_alert(facility_id, status.status_date,
                                     const.DELIVERY_NOT_RECEIVED,
                                     {'number': 1})
示例#7
0
def process_facility_statuses(facility_id, statuses, alerts=True):
    """
    For a given facility and list of statuses, update the appropriate
    data warehouse tables. This should only be called on supply points
    that are facilities.
    """
    facility = get_location(facility_id)
    for status in statuses:
        warehouse_date = _get_window_date(status.status_type, status.status_date)
        if _is_valid_status(facility, status.status_date, status.status_type):
            org_summary = OrganizationSummary.objects.get_or_create(
                location_id=facility_id,
                date=warehouse_date
            )[0]
            group_summary = GroupSummary.objects.get_or_create(
                org_summary=org_summary,
                title=status.status_type
            )[0]
            group_summary.total = 1
            if status.status_value not in (SupplyPointStatusValues.REMINDER_SENT,
                                           SupplyPointStatusValues.ALERT_SENT):
                # we've responded to this query
                group_summary.responded = 1
                if status.status_value in [SupplyPointStatusValues.SUBMITTED,
                                           SupplyPointStatusValues.RECEIVED]:
                    group_summary.complete = 1
                else:
                    group_summary.complete = group_summary.complete or 0
                if group_summary.complete:
                    if is_on_time(status.status_date, warehouse_date, status.status_type):
                        group_summary.on_time = 1
                    else:
                        group_summary.on_time = group_summary.on_time
                else:
                    group_summary.on_time = 0

                group_summary.save()

                if alerts:
                    if status.status_value == SupplyPointStatusValues.NOT_SUBMITTED \
                            and status.status_type == SupplyPointStatusTypes.R_AND_R_FACILITY:
                        create_alert(facility_id, status.status_date, const.RR_NOT_SUBMITTED,
                                     {'number': 1})

                    if status.status_value == SupplyPointStatusValues.NOT_RECEIVED \
                            and status.status_type == SupplyPointStatusTypes.DELIVERY_FACILITY:
                        create_alert(facility_id, status.status_date, const.DELIVERY_NOT_RECEIVED,
                                     {'number': 1})
示例#8
0
def _get_location(form):
    loc_id = form.form.get('location_id')
    if loc_id:
        try:
            return get_location(loc_id)
        except SQLLocation.DoesNotExist:
            logging.info('Location %s Not Found.' % loc_id)
    else:
        user_id = form.user_id
        if not user_id:
            return None
        try:
            user = CouchUser.get_by_user_id(user_id)
            if isinstance(user, CommCareUser):
                return user.location
        except ResourceNotFound:
            logging.info('Location for user %s Not Found.' % user_id)
示例#9
0
def _get_location(form):
    loc_id = form.form.get('location_id')
    if loc_id:
        try:
            return get_location(loc_id)
        except SQLLocation.DoesNotExist:
            logging.info('Location %s Not Found.' % loc_id)
    else:
        user_id = form.user_id
        if not user_id:
            return None
        try:
            user = CouchUser.get_by_user_id(user_id)
            if isinstance(user, CommCareUser):
                return user.location
        except ResourceNotFound:
            logging.info('Location for user %s Not Found.' % user_id)
示例#10
0
def get_location_by_type(form, type):
    loc = _get_location(form)
    if not loc:
        district_name = form.form.get('district_name', None)
        loc = SQLLocation.objects.filter(domain=get_domain(form),
                                         name=district_name)
        if not loc:
            return None
        if loc.count() > 1:
            loc = loc.filter(location_type__name='District')
        loc = loc[0]
        if type == 'district':
            return loc
    for loc_id in loc.lineage:
        loc = get_location(loc_id)
        if unicode(loc.location_type_name).lower().replace(" ", "") == type:
            return loc
示例#11
0
def get_location_by_type(form, type):
    loc = _get_location(form)
    if not loc:
        district_name = form.form.get('district_name', None)
        loc = SQLLocation.objects.filter(
            domain=get_domain(form),
            name=district_name)
        if not loc:
            return None
        if loc.count() > 1:
            loc = loc.filter(location_type__name='District')
        loc = loc[0]
        if type == 'district':
            return loc
    for loc_id in loc.lineage:
        loc = get_location(loc_id)
        if six.text_type(loc.location_type_name).lower().replace(" ", "") == type:
            return loc
示例#12
0
    def calculate_rows(self):
        rows = IndicateursDeBaseData(config=self.config).rows
        rows_to_return = []

        for row in rows:
            location_name = row['location_name']

            min_date = row['min_date']
            max_date = row['max_date']
            date = '{} - {}'.format(min_date, max_date)

            nb_pps_enregistres = row['nb_pps_enregistres']
            nb_pps_visites = row['nb_pps_visites']
            couverture = '{:.2f} %'.format((nb_pps_visites / nb_pps_enregistres) * 100) \
                if nb_pps_enregistres != 0 else 'pas de données'

            if row['location_id']:
                loc = get_location(row['location_id'],
                                   domain=self.config['domain'])
                no_de_pps_aces_donnes_soumies = _locations_per_type(
                    self.config['domain'], 'PPS', loc)
                soumission = '{:.2f} %'.format((no_de_pps_aces_donnes_soumies / nb_pps_visites) * 100) \
                    if nb_pps_visites != 0 else 'pas de données'
            else:
                no_de_pps_aces_donnes_soumies = 'pas de données'
                soumission = 'pas de données'

            columns_for_location = [
                date, nb_pps_enregistres, nb_pps_visites,
                no_de_pps_aces_donnes_soumies, couverture, soumission
            ]
            rows_to_return.append([
                location_name,
            ])

            for column in columns_for_location:
                rows_to_return[-1].append({
                    'html': '{}'.format(column),
                    'sort_key': column,
                })

        return rows_to_return
 def data_providers(self):
     config = self.report_config
     location_types = [
         loc_type.name for loc_type in filter(
             lambda loc_type: not loc_type.administrative,
             Domain.get_by_name(self.domain).location_types)
     ]
     if not self.needs_filters and get_location(
             config['location_id']).location_type_name in location_types:
         if self.is_rendered_as_email:
             return [FacilityReportData(config)]
         else:
             return [
                 FacilityReportData(config),
                 StockLevelsLegend(config),
                 InputStock(config),
                 UsersData(config),
                 InventoryManagementData(config),
                 ProductSelectionPane(config, hide_columns=False)
             ]
示例#14
0
 def data_providers(self):
     config = self.report_config
     data_providers = []
     if config['location_id']:
         data_providers = [RandRSubmissionData(config=config, css_class='row_chart_all')]
         location = get_location(config['location_id'])
         if location.location_type_name in ['REGION', 'MSDZONE', 'MOHSW']:
             data_providers.append(RRStatus(config=config, css_class='row_chart_all'))
         elif location.location_type_name == 'FACILITY':
             return [
                 InventoryHistoryData(config=config),
                 RandRHistory(config=config),
                 Notes(config=config),
                 RecentMessages(config=config),
                 RegistrationData(config=dict(loc_type='FACILITY', **config), css_class='row_chart_all'),
                 RegistrationData(config=dict(loc_type='DISTRICT', **config), css_class='row_chart_all'),
                 RegistrationData(config=dict(loc_type='REGION', **config), css_class='row_chart_all')
             ]
         else:
             data_providers.append(RRReportingHistory(config=config, css_class='row_chart_all'))
     return data_providers
示例#15
0
 def location(self):
     return get_location(self.kwargs['location_id'])
示例#16
0
def needed_status_types(org_summary):
    facility = get_location(org_summary.location_id)
    return [
        status_type for status_type in const.NEEDED_STATUS_TYPES
        if _is_valid_status(facility, org_summary.date, status_type)
    ]
        case = EMPTY_FIELD

    amount_due = EMPTY_FIELD
    if form["form"].get("registration_amount", None) is not None:
        amount_due = form["form"].get("registration_amount", None)
    elif form["form"].get("immunization_amount", None) is not None:
        amount_due = form["form"].get("immunization_amount", None)

    service_type = form["form"].get("service_type", EMPTY_FIELD)
    form_id = form["_id"]
    location_name = EMPTY_FIELD
    location_parent_name = EMPTY_FIELD
    location_id = form["form"].get("location_id", None)

    if location_id is not None:
        location = get_location(location_id)
        location_name = location.name
        location_parent = location.parent
        if location_parent is not None and location_parent.location_type_name != 'state':
            while location_parent is not None and location_parent.location_type_name not in (
                    'district', 'lga'):
                location_parent = location_parent.parent
        location_parent_name = location_parent.name if location_parent is not None else EMPTY_FIELD

    return {
        'case': case,
        'service_type': service_type,
        'location_name': location_name,
        'location_parent_name': location_parent_name,
        'amount_due': amount_due,
        'form_id': form_id
示例#18
0
 def location(self):
     return get_location(self.kwargs['location_id'])
示例#19
0
 def location(self):
     if self.request.GET.get('location_id'):
         return get_location(self.request.GET.get('location_id'))
示例#20
0
 def supply_point(self):
     if not self.location_id:
         return
     return get_location(self.location_id).linked_supply_point()
示例#21
0
 def supply_point(self):
     if not self.location_id:
         return
     return get_location(self.location_id).linked_supply_point()
示例#22
0
def needed_status_types(org_summary):
    facility = get_location(org_summary.location_id)
    return [status_type for status_type in const.NEEDED_STATUS_TYPES if _is_valid_status(facility,
                                                                                   org_summary.date, status_type)]
示例#23
0
 def location(self):
     if self.request.GET.get('location_id'):
         return get_location(self.request.GET.get('location_id'))