<s> import re <EOL> from django . conf import settings <EOL> from django . contrib . gis . measure import D <EOL> from tastypie import fields <EOL> from tastypie . serializers import Serializer <EOL> from tastypie . constants import ALL , ALL_WITH_RELATIONS <EOL> from django . contrib . gis . geos import Polygon <EOL> from boundaryservice . authentication import NoOpApiKeyAuthentication <EOL> from boundaryservice . models import BoundarySet , Boundary <EOL> from boundaryservice . tastyhacks import SluggedResource <EOL> from boundaryservice . throttle import AnonymousThrottle <EOL> if getattr ( settings , '<STR_LIT>' , False ) : <EOL> throttle_cls = AnonymousThrottle ( ** settings . BOUNDARY_SERVICE_THROTTLE ) <EOL> else : <EOL> throttle_cls = False <EOL> class BoundarySetResource ( SluggedResource ) : <EOL> boundaries = fields . ToManyField ( <EOL> '<STR_LIT>' , '<STR_LIT>' ) <EOL> class Meta : <EOL> queryset = BoundarySet . objects . all ( ) <EOL> serializer = Serializer ( <EOL> formats = [ '<STR_LIT>' , '<STR_LIT>' ] , <EOL> content_types = { <EOL> '<STR_LIT>' : '<STR_LIT:application/json>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } ) <EOL> resource_name = '<STR_LIT>' <EOL> excludes = [ '<STR_LIT:id>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> allowed_methods = [ '<STR_LIT>' ] <EOL> authentication = NoOpApiKeyAuthentication ( ) <EOL> throttle = throttle_cls <EOL> class BoundaryResource ( SluggedResource ) : <EOL> set = fields . ForeignKey ( BoundarySetResource , '<STR_LIT>' ) <EOL> class Meta : <EOL> queryset = Boundary . objects . all ( ) <EOL> serializer = Serializer ( <EOL> formats = [ '<STR_LIT>' , '<STR_LIT>' ] , <EOL> content_types = { <EOL> '<STR_LIT>' : '<STR_LIT:application/json>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } ) <EOL> resource_name = '<STR_LIT>' <EOL> excludes = [ '<STR_LIT:id>' , '<STR_LIT>' ] <EOL> allowed_methods = [ '<STR_LIT>' ] <EOL> authentication = NoOpApiKeyAuthentication ( ) <EOL> throttle = throttle_cls <EOL> filtering = { <EOL> "<STR_LIT>" : ALL <EOL> } <EOL> def alter_list_data_to_serialize ( self , request , data ) : <EOL> """<STR_LIT>""" <EOL> shape_type = request . GET . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> for obj in data [ '<STR_LIT>' ] : <EOL> if shape_type != '<STR_LIT>' : <EOL> del obj . data [ '<STR_LIT>' ] <EOL> if shape_type != '<STR_LIT>' : <EOL> del obj . data [ '<STR_LIT>' ] <EOL> return data <EOL> def alter_detail_data_to_serialize ( self , request , bundle ) : <EOL> """<STR_LIT>""" <EOL> shape_type = request . GET . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if shape_type != '<STR_LIT>' : <EOL> del bundle . data [ '<STR_LIT>' ] <EOL> if shape_type != '<STR_LIT>' : <EOL> del bundle . data [ '<STR_LIT>' ] <EOL> return bundle <EOL> def build_filters ( self , filters = None ) : <EOL> """<STR_LIT>""" <EOL> if filters is None : <EOL> filters = { } <EOL> orm_filters = super ( BoundaryResource , self ) . build_filters ( filters ) <EOL> if '<STR_LIT>' in filters : <EOL> sets = filters [ '<STR_LIT>' ] . split ( '<STR_LIT:U+002C>' ) <EOL> orm_filters . update ( { '<STR_LIT>' : sets } ) <EOL> if '<STR_LIT>' in filters : <EOL> lat , lon = filters [ '<STR_LIT>' ] . split ( '<STR_LIT:U+002C>' ) <EOL> wkt_pt = '<STR_LIT>' % ( lon , lat ) <EOL> orm_filters . update ( { '<STR_LIT>' : wkt_pt } ) <EOL> if '<STR_LIT>' in filters : <EOL> lat , lon , range = filters [ '<STR_LIT>' ] . split ( '<STR_LIT:U+002C>' ) <EOL> wkt_pt = '<STR_LIT>' % ( lon , lat ) <EOL> numeral = re . match ( '<STR_LIT>' , range ) . group ( <NUM_LIT:1> ) <EOL> unit = range [ len ( numeral ) : ] <EOL> numeral = int ( numeral ) <EOL> kwargs = { unit : numeral } <EOL> orm_filters . update ( { '<STR_LIT>' : ( wkt_pt , D ( ** kwargs ) ) } ) <EOL> if '<STR_LIT>' in filters : <EOL> slug = filters [ '<STR_LIT>' ] <EOL> bounds = Boundary . objects . get ( slug = slug ) <EOL> orm_filters . update ( { '<STR_LIT>' : bounds . shape } ) <EOL> if '<STR_LIT>' in filters : <EOL> xmin , ymin , xmax , ymax = filters [ '<STR_LIT>' ] . split ( "<STR_LIT:U+002C>" ) <EOL> bbox = ( xmin , ymin , xmax , ymax ) <EOL> bbox = Polygon . from_bbox ( bbox ) <EOL> orm_filters . update ( { '<STR_LIT>' : bbox } ) <EOL> return orm_filters </s>
