Exemplo n.º 1
0
    def list(self, request, *args, **kwargs):
        survey_tag = self.request.query_params.get('survey_tag', None)
        survey_id = self.request.query_params.get('survey_id', None)
        admin2_id = self.request.query_params.get('admin2_id', None)

        if admin2_id:
            qset = self.queryset.filter(admin2_id=admin2_id)
        else:
            raise ParseError("Mandatory parameter admin2_id not passed")

        if survey_tag:
            qset = qset.filter(
                gp__surveyelectionboundaryagg__survey_tag=survey_tag)
        elif survey_id:
            qset = qset.filter(
                gp__surveyelectionboundaryagg__survey_id=survey_id)
        else:
            raise ParseError(
                "Mandatory parameter survey_id or survey_tag not passed")

        gp_ids = qset.values_list('gp__id', flat=True)
        eb_qs = ElectionBoundary.objects.filter(id__in=gp_ids)
        response = ElectionBoundarySerializer(eb_qs, many=True).data
        return Response(response)
Exemplo n.º 2
0
 def get_ward(self, obj):
     if obj.ward is None:
         return None
     return ElectionBoundarySerializer(obj.ward).data
Exemplo n.º 3
0
 def get_assembly(self, obj):
     if obj.mla is None:
         return None
     return ElectionBoundarySerializer(obj.mla).data
Exemplo n.º 4
0
 def get_parliament(self, obj):
     if obj.mp is None:
         return None
     return ElectionBoundarySerializer(obj.mp).data
Exemplo n.º 5
0
    def get(self, request, format=None):
        response = {
            'pre_schools': [],
            'primary_schools': [],
            'boundaries': [],
            'assemblies': [],
            'parliaments': [],
            'pincodes': [],
        }

        context = {
            'request': request,
            'view': self
        }

        params = request.query_params
        text = params.get('text', '')

        if not text:
            return Response({
                'error': 'A text must be provided to search'
            }, status=404)

        response['pre_schools'] = LeanInstitutionSummarySerializer(
            Institution.objects.filter(
                (Q(name__icontains=text) | Q(id__contains=text)
                    | Q(dise__school_code__contains=text)),
                Q(status='AC'),
                Q(coord__isnull=False),
                Q(institution_type__name='Preschool')
            )[:3],
            many=True,
            context=context
        ).data

        response['primary_schools'] = LeanInstitutionSummarySerializer(
            Institution.objects.filter(
                (Q(name__icontains=text) | Q(id__contains=text)
                    | Q(dise__school_code__contains=text)),
                Q(status='AC'),
                Q(coord__isnull=False),
                Q(institution_type__name='Primary School')
            )[:3],
            many=True,
            context=context
        ).data

        response['boundaries'] = BoundarySerializer(
            Boundary.objects.filter(
                status='AC',
                name__icontains=text,
                geom__isnull=False
            )[:10],
            many=True,
            context=context
        ).data

        response['assemblies'] = ElectionBoundarySerializer(
            ElectionBoundary.objects.filter(const_ward_type='MLA').filter(
                const_ward_name__icontains=text,
                geom__isnull=False
            )[:10],
            many=True,
            context=context
        ).data

        response['parliaments'] = ElectionBoundarySerializer(
            ElectionBoundary.objects.filter(const_ward_type='MP').filter(
                const_ward_name__icontains=text,
                geom__isnull=False
            )[:10],
            many=True,
            context=context
        ).data

        # response['pincodes'] = PincodeSerializer(
        #     Postal.objects.filter(
        #         pincode__icontains=text,
        #         coord__isnull=False
        #     )[:10],
        #     many=True,
        #     context=context
        # ).data

        return Response(response)