Example #1
0
 def _district_summary_data(self):
     queryset = Location.objects.filter(level="district")
     queryset = (get_location_qs(queryset).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",
         ))
     serialized = DistrictSerializer(queryset, many=True)
     return serialized.data
Example #2
0
class TargetAreaHouseholdsViewSet(mixins.RetrieveModelMixin,
                                  viewsets.GenericViewSet):
    queryset = get_location_qs(Location.objects.filter())
    serializer_class = HouseholdSerializer

    def get_serializer_class(self):
        serializer_class = self.serializer_class
        if self.kwargs.get('bgeom'):
            serializer_class = HouseholdBSerializer
        return serializer_class

    def retrieve(self, request, **kwargs):
        data = []
        location = self.get_object()
        if location.geom is not None:
            tas = list(get_ta_in_location(location))
            households = Household.objects.filter(location__in=tas)

            if settings.OSM_SUBMISSIONS:
                spray_points = SprayDay.objects.exclude(geom=None)
                spray_date = self.kwargs.get('spray_date')
                if spray_date:
                    spray_points = spray_points.filter(
                        spray_date__lte=spray_date)
                spray_points = spray_points.filter(
                    location__in=tas).values('geom')
                exclude = households.filter(
                    hh_id__in=spray_points.values('osmid'))\
                    .values_list('pk', flat=True)
                households = households.exclude(pk__in=exclude)

            serializer = self.get_serializer(households, many=True)
            data = serializer.data

        return Response(data)
Example #3
0
class TargetAreaViewSet(mixins.RetrieveModelMixin, viewsets.GenericViewSet):
    queryset = get_location_qs(Location.objects.filter())
    serializer_class = TargetAreaSerializer

    def get_serializer_class(self):
        if self.format_kwarg == 'geojson':
            return GeoTargetAreaSerializer

        return super(TargetAreaViewSet, self).get_serializer_class()
Example #4
0
    def get_queryset(self):
        queryset = super(DistrictView, self).get_queryset().filter(target=True)
        location_id = self.kwargs.get(self.slug_field)
        if location_id is not None:
            queryset = queryset.filter(parent__pk=location_id)
        else:
            queryset = queryset.filter(parent=None).order_by("name")

        return get_location_qs(queryset)
Example #5
0
def get_district_summary_data():
    """
    Gets a queryset of Districts and serializes it
    """
    from mspray.apps.main.serializers import DistrictSerializer
    queryset = Location.objects.filter(level='district')
    queryset = get_location_qs(queryset).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')
    serialized = DistrictSerializer(queryset, many=True)
    return serialized.data
Example #6
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
Example #7
0
    def get_queryset(self):

        return get_location_qs(super(MapView, self).get_queryset())
Example #8
0
    def get_queryset(self):

        return get_location_qs(
            super(SprayAreaView, self).get_queryset().filter(
                level="ta", target=True))
Example #9
0
    def get_queryset(self):

        return get_location_qs(
            super(TargetAreaView, self).get_queryset().filter(target=True))