コード例 #1
0
ファイル: api_views.py プロジェクト: karna1995/dise_dashboard
    def get_queryset(self):
        queryset = super(SchoolListView, self).get_queryset()

        if 'management' in self.request.query_params and self.request.query_params.get('management', ''):
            if self.request.query_params.get('management') == 'govt':
                queryset = queryset.filter(
                    sch_management__in=[1, 7]
                )
            elif self.request.query_params.get('management') == 'pvt':
                queryset = queryset.exclude(
                    sch_management__in=[1, 7]
                )

        if 'area' in self.request.query_params and self.request.query_params.get('area', ''):
            queryset = queryset.filter(
                rural_urban=common_utils.search_choices(
                    common_utils.AREA, self.request.query_params.get('area').title()
                )
            )

        return queryset
コード例 #2
0
    def _search(self, params):
        # This seaches all the base models, depending on the session and retuns
        # list of schools
        result = dict()
        result['query'] = params
        SchoolModel = get_models(params.get('session', '10-11'), 'school')

        if len(params.keys()) > 1:
            schools = SchoolModel.objects.only(*self.only_fields)

        if 'name' in params and params.get('name', ''):
            schools = schools.filter(school_name__icontains=params.get('name'))

        if 'cluster' in params and params.get('cluster', ''):
            schools = schools.filter(
                cluster_name__icontains=params.get('cluster'))

        if 'block' in params and params.get('block', ''):
            schools = schools.filter(
                block_name__icontains=params.get('block'))

        if 'district' in params and params.get('district', ''):
            schools = schools.filter(
                district__icontains=params.get('district'))

        if 'limit' in params and params.get('limit', 0):
            schools = schools[:params.get('limit')]

        if 'area' in params and params.get('area', ''):
            schools = schools.filter(
                rural_urban=search_choices(AREA, params.get('area').title())
            )

        if 'management' in params and params.get('management', ''):
            if params.get('management') == 'govt':
                schools = schools.filter(
                    sch_management__in=[1, 7]
                )
            elif params.get('management') == 'pvt':
                schools = schools.exclude(
                    sch_management__in=[1, 7]
                )

        if 'f' in params and params.get('f', ''):
            f = params.get('f')
            f = json.loads(urllib2.unquote(f).decode('utf8'))

            for filt in f.get('facilities', []):
                if filt == 'repair':
                    schools = schools.filter(
                        classrooms_require_minor_repair=search_choices(YESNO, 'Yes'),
                        classrooms_require_major_repair=search_choices(YESNO, 'Yes')
                    )
                elif filt == 'toilet':
                    schools = schools.filter(
                        toilet_common=0,
                        toilet_boys=0,
                        toilet_girls=0
                    )
                elif filt == 'toilet_for_girls':
                    schools = schools.filter(
                        toilet_girls=0
                    )
                elif filt == 'electricity':
                    schools = schools.filter(
                        electricity=search_choices(YESNO, 'No')
                    )
                elif filt == 'secure_wall':
                    schools = schools.exclude(
                        boundary_wall__in=[
                            search_choices(BOUNDARY_WALL, "Pucca"),
                            search_choices(BOUNDARY_WALL, "Barbed wire fencing"),
                        ]
                    )
                elif filt == 'library':
                    schools = schools.filter(
                        library_yn=search_choices(YESNO, 'No')
                    )
                elif filt == 'ramp':
                    schools = schools.filter(
                        ramps=search_choices(YESNO, 'No')
                    )
                elif filt == 'blackboard':
                    schools = schools.filter(
                        blackboard=search_choices(YESNO, 'No')
                    )
                elif filt == 'playground':
                    schools = schools.filter(
                        playground=search_choices(YESNO, 'No')
                    )
                elif filt == 'drinking_water':
                    schools = schools.filter(
                        drinking_water=search_choices(YESNO, 'No')
                    )

        result['total_count'] = schools.count()

        if 'bbox' in params and params.get('bbox', ''):
            # &bbox="75.73974609375,12.5223906020692,79.4476318359375,13.424352095715332"
            # southwest_lng,southwest_lat,northeast_lng,northeast_lat
            # xmin,ymin,xmax,ymax
            coords_match = re.match(
                r"([\d\.]+),([\d\.]+),([\d\.]+),([\d\.]+)", params.get('bbox'))
            if coords_match and len(coords_match.groups()) == 4:
                bbox = map(lambda x: float(x), coords_match.groups())
                geom = Polygon.from_bbox(bbox)
                schools = schools.filter(centroid__contained=geom)

        print schools.query
        temp_l = []
        for sch in schools:
            temp_l.append(self._get_geojson(sch))
        result['results'] = FeatureCollection(temp_l)
        return result