Beispiel #1
0
    def get_context_data(self, **kwargs):
        context = super(TargetAreaMap, self).get_context_data(**kwargs)
        spray_date = parse_spray_date(self.request)
        filter_list = [['target_area_id', operator.eq, self.object.pk]]
        if spray_date:
            filter_list.append([
                'spray_date', operator.eq,
                self.request.GET.get('spray_date')
            ])
        druid_result = get_druid_data(filter_list=filter_list,
                                      dimensions=[
                                          'target_area_id', 'target_area_name',
                                          'target_area_structures',
                                          'district_name', 'district_id',
                                          'rhc_name', 'rhc_id'
                                      ])
        data, totals = process_druid_data(druid_result)
        if data:
            ta_data = TargetAreaSerializer(self.object,
                                           druid_data=data[0]).data
        else:
            ta_data = TargetAreaSerializer(self.object).data

        sprayed_duplicates = get_duplicates(ta_pk=self.object.id, sprayed=True)
        not_sprayed_duplicates = get_duplicates(ta_pk=self.object.id,
                                                sprayed=False)
        context['sprayed_duplicates'] = len(sprayed_duplicates)
        context['sprayed_duplicates_data'] = sprayed_duplicates
        context['not_sprayed_duplicates'] = len(not_sprayed_duplicates)
        context['not_sprayed_duplicates_data'] = not_sprayed_duplicates
        context['spray_dates'] = ta_data['properties']['spray_dates']
        context['spray_date'] = spray_date
        context['target_data'] = JSONRenderer().render(ta_data)
        context['districts'] = Location.objects.filter(parent=None)\
            .values_list('id', 'code', 'name').order_by('name')

        context.update({'map_menu': True})
        context.update(get_location_dict(self.object.pk))
        return context
Beispiel #2
0
    def get_context_data(self, **kwargs):
        context = super(SprayAreaView, self).get_context_data(**kwargs)
        queryset = (context["object_list"].extra(
            select={
                "xmin": 'ST_xMin("main_location"."geom")',
                "ymin": 'ST_yMin("main_location"."geom")',
                "xmax": 'ST_xMax("main_location"."geom")',
                "ymax": 'ST_yMax("main_location"."geom")',
            }).values(
                "pk",
                "code",
                "level",
                "name",
                "parent",
                "structures",
                "xmin",
                "ymin",
                "xmax",
                "ymax",
                "num_of_spray_areas",
                "num_new_structures",
                "total_structures",
                "parent__name",
                "parent__parent__name",
                "parent__parent__pk",
                "parent__pk",
                "is_sensitized",
            ).order_by("parent__parent__name", "parent__name", "name"))
        if self.request.GET.get("format") != "csv":
            serializer_class = TargetAreaSerializer
            serializer = serializer_class(
                queryset, many=True, context={"request": self.request})
            context["district_list"] = serializer.data
        context["qs"] = queryset
        context.update(get_location_dict(None))
        context.update(DEFINITIONS["ta"])

        return context
Beispiel #3
0
    def get_context_data(self, **kwargs):
        context = super(MapView, self).get_context_data(**kwargs)
        serializer_class = (TargetAreaQuerySerializer if settings.SITE_NAME
                            == "namibia" else TargetAreaSerializer)
        location = context["object"]
        if location.level == "RHC":
            location = get_location_qs(Location.objects.filter(pk=location.pk),
                                       "RHC").first()
        serializer = serializer_class(location,
                                      context={"request": self.request})
        context["target_data"] = serializer.data
        spray_date = parse_spray_date(self.request)
        if spray_date:
            context["spray_date"] = spray_date
        if settings.MSPRAY_SPATIAL_QUERIES or context["object"].geom:
            response = TargetAreaViewSet.as_view({"get": "retrieve"
                                                  })(self.request,
                                                     pk=context["object"].pk,
                                                     format="geojson")
            response.render()
            context["not_sprayable_value"] = getattr(settings,
                                                     "NOT_SPRAYABLE_VALUE",
                                                     "noteligible")
            context["ta_geojson"] = response.content.decode()
            bgeom = settings.HH_BUFFER and settings.OSM_SUBMISSIONS

            if self.object.level in ["district", "RHC"]:
                data = GeoTargetAreaSerializer(
                    get_location_qs(self.object.location_set.all(),
                                    self.object.level),
                    many=True,
                    context={
                        "request": self.request
                    },
                ).data
                context["hh_geojson"] = json.dumps(data)
            else:
                loc = context["object"]
                hhview = TargetAreaHouseholdsViewSet.as_view(
                    {"get": "retrieve"})
                response = hhview(
                    self.request,
                    pk=loc.pk,
                    bgeom=bgeom,
                    spray_date=spray_date,
                    format="geojson",
                )
                response.render()
                context["hh_geojson"] = response.content.decode()
                sprayed_duplicates = list(get_duplicates(
                    loc, True, spray_date))
                not_sprayed_duplicates = list(
                    get_duplicates(loc, False, spray_date))
                context["sprayed_duplicates_data"] = json.dumps(
                    sprayed_duplicates)
                context["sprayed_duplicates"] = count_duplicates(
                    loc, True, spray_date)
                context["not_sprayed_duplicates_data"] = json.dumps(
                    not_sprayed_duplicates)
                context["not_sprayed_duplicates"] = count_duplicates(
                    loc, False)

        context["districts"] = (Location.objects.filter(
            parent=None).values_list("id", "code", "name").order_by("name"))

        context.update({"map_menu": True})
        context.update(get_location_dict(self.object.pk))
        context["not_sprayed_reasons"] = json.dumps(
            settings.MSPRAY_UNSPRAYED_REASON_OTHER)

        return context
Beispiel #4
0
    def get_context_data(self, **kwargs):  # pylint: disable=R0912,R0914,R0915
        context = super(DistrictView, self).get_context_data(**kwargs)

        not_targeted = None

        queryset = (context["object_list"].extra(
            select={
                "xmin": 'ST_xMin("main_location"."geom")',
                "ymin": 'ST_yMin("main_location"."geom")',
                "xmax": 'ST_xMax("main_location"."geom")',
                "ymax": 'ST_yMax("main_location"."geom")',
            }).values(
                "pk",
                "code",
                "level",
                "name",
                "parent",
                "structures",
                "xmin",
                "ymin",
                "xmax",
                "ymax",
                "num_of_spray_areas",
                "num_new_structures",
                "total_structures",
                "visited",
                "sprayed",
                "is_sensitized",
                "priority",
            ))
        location_id = self.kwargs.get(self.slug_field)
        if location_id is None:
            serializer_class = DistrictSerializer
        else:
            level = self.object_list.first().level if self.object_list.first(
            ) else ""
            if level == "RHC":
                serializer_class = DistrictSerializer
            else:
                serializer_class = (TargetAreaQuerySerializer
                                    if settings.SITE_NAME == "namibia" else
                                    TargetAreaSerializer)
            # no location structures
            try:
                obj = get_object_or_404(Location, pk=location_id, target=True)
            except Location.DoesNotExist:
                pass
            else:
                not_targeted = get_not_targeted_within_geom(obj.geom)[0]
                context["no_location"] = not_targeted
                if obj.level == "district":
                    context["the_district"] = obj
                else:
                    context["the_district"] = (obj.get_ancestors().filter(
                        level="district").first())
        serializer = serializer_class(
            queryset, many=True, context={"request": self.request})
        context["district_list"] = serializer.data
        fields = [
            "structures",
            "visited_total",
            "visited_sprayed",
            "visited_not_sprayed",
            "visited_refused",
            "visited_other",
            "not_visited",
            "found",
            "num_of_spray_areas",
        ]
        totals = {}
        for rec in serializer.data:
            for field in fields:
                try:
                    totals[field] = rec[field] + (totals[field]
                                                  if field in totals else 0)
                except KeyError:
                    pass
        not_captured = getattr(settings, "NOT_CAPTURED", {})
        if not_targeted and not location_id:
            if obj.level != "district":
                not_targeted_fields = [
                    ("visited_total", "found"),
                    ("visited_sprayed", "sprayed"),
                    ("visited_not_sprayed", "not_sprayed"),
                    ("visited_refused", "refused"),
                    ("visited_other", "other"),
                ]
                not_captured_data = not_captured.get(obj.pk)
                if not_captured_data:
                    not_targeted["found"] += not_captured_data
                    not_targeted["sprayed"] += not_captured_data
                for total_field, not_field in not_targeted_fields:
                    try:
                        totals[total_field] += not_targeted[not_field]
                    except KeyError:
                        totals[total_field] = not_targeted[not_field]

        district_code = self.kwargs.get(self.slug_field)
        context.update(get_location_dict(district_code))
        context["district_totals"] = totals
        if not district_code:
            context.update(DEFINITIONS["district"])
        else:
            loc = context.get("district")
            level = "RHC" if loc.level == "district" else "ta"
            context.update(DEFINITIONS[level])

        return context