<s> from urllib import unquote <EOL> from django . contrib . gis . db . models import GeometryField <EOL> from django . utils import simplejson <EOL> from django . contrib . gis . geos import GEOSGeometry <EOL> from tastypie . fields import ApiField , CharField <EOL> from tastypie import resources <EOL> class GeometryApiField ( ApiField ) : <EOL> """<STR_LIT>""" <EOL> dehydrated_type = '<STR_LIT>' <EOL> help_text = '<STR_LIT>' <EOL> def hydrate ( self , bundle ) : <EOL> value = super ( GeometryApiField , self ) . hydrate ( bundle ) <EOL> if value is None : <EOL> return value <EOL> return simplejson . dumps ( value ) <EOL> def dehydrate ( self , obj ) : <EOL> return self . convert ( super ( GeometryApiField , self ) . dehydrate ( obj ) ) <EOL> def convert ( self , value ) : <EOL> if value is None : <EOL> return None <EOL> if isinstance ( value , dict ) : <EOL> return value <EOL> return simplejson . loads ( value . geojson ) <EOL> class ModelResource ( resources . ModelResource ) : <EOL> """<STR_LIT>""" <EOL> @ classmethod <EOL> def api_field_from_django_field ( cls , f , default = CharField ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( f , GeometryField ) : <EOL> return GeometryApiField <EOL> return super ( ModelResource , cls ) . api_field_from_django_field ( f , default ) <EOL> def filter_value_to_python ( self , value , field_name , filters , filter_expr , <EOL> filter_type ) : <EOL> value = super ( ModelResource , self ) . filter_value_to_python ( <EOL> value , field_name , filters , filter_expr , filter_type ) <EOL> if isinstance ( self . fields [ field_name ] , GeometryApiField ) : <EOL> try : <EOL> value = GEOSGeometry ( unquote ( value ) ) <EOL> except ValueError : <EOL> pass <EOL> return value </s>
