<s> from urllib import unquote <EOL> from django . conf import settings <EOL> from django . conf . urls . defaults import url <EOL> from django . http import HttpResponse <EOL> from django . middleware . csrf import _sanitize_token , constant_time_compare <EOL> from django . utils . http import same_origin <EOL> from tastypie . authentication import ApiKeyAuthentication <EOL> from tastypie . bundle import Bundle <EOL> from tastypie . fields import ApiField , CharField <EOL> from tastypie . paginator import Paginator <EOL> from tastypie . resources import ModelResource , Resource <EOL> from tastypie . serializers import Serializer <EOL> from tastypie . utils . urls import trailing_slash <EOL> from panda . fields import JSONField <EOL> from panda . models import UserProxy <EOL> PANDA_CACHE_CONTROL = '<STR_LIT>' <EOL> class JSONApiField ( ApiField ) : <EOL> """<STR_LIT>""" <EOL> dehydrated_type = '<STR_LIT>' <EOL> help_text = '<STR_LIT>' <EOL> def dehydrate ( self , obj ) : <EOL> return self . convert ( super ( JSONApiField , self ) . dehydrate ( obj ) ) <EOL> def convert ( self , value ) : <EOL> if value is None : <EOL> return None <EOL> return value <EOL> class PandaResource ( Resource ) : <EOL> """<STR_LIT>""" <EOL> def create_response ( self , request , data , response_class = HttpResponse , ** response_kwargs ) : <EOL> """<STR_LIT>""" <EOL> response = super ( PandaResource , self ) . create_response ( request , data , response_class , ** response_kwargs ) <EOL> response [ '<STR_LIT>' ] = PANDA_CACHE_CONTROL <EOL> return response <EOL> class PandaModelResource ( ModelResource ) : <EOL> """<STR_LIT>""" <EOL> @ classmethod <EOL> def api_field_from_django_field ( cls , f , default = CharField ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( f , JSONField ) : <EOL> return JSONApiField <EOL> return super ( PandaModelResource , cls ) . api_field_from_django_field ( f , default ) <EOL> def create_response ( self , request , data , response_class = HttpResponse , ** response_kwargs ) : <EOL> """<STR_LIT>""" <EOL> response = super ( PandaModelResource , self ) . create_response ( request , data , response_class , ** response_kwargs ) <EOL> response [ '<STR_LIT>' ] = PANDA_CACHE_CONTROL <EOL> return response <EOL> class SluggedModelResource ( PandaModelResource ) : <EOL> """<STR_LIT>""" <EOL> def get_resource_uri ( self , bundle_or_obj ) : <EOL> """<STR_LIT>""" <EOL> kwargs = { <EOL> '<STR_LIT>' : self . _meta . resource_name , <EOL> } <EOL> if isinstance ( bundle_or_obj , Bundle ) : <EOL> kwargs [ '<STR_LIT>' ] = bundle_or_obj . obj . slug <EOL> else : <EOL> kwargs [ '<STR_LIT>' ] = bundle_or_obj . slug <EOL> if self . _meta . api_name is not None : <EOL> kwargs [ '<STR_LIT>' ] = self . _meta . api_name <EOL> return self . _build_reverse_url ( "<STR_LIT>" , kwargs = kwargs ) <EOL> def base_urls ( self ) : <EOL> """<STR_LIT>""" <EOL> return [ <EOL> url ( r"<STR_LIT>" % ( self . _meta . resource_name , trailing_slash ( ) ) , self . wrap_view ( '<STR_LIT>' ) , name = "<STR_LIT>" ) , <EOL> url ( r"<STR_LIT>" % ( self . _meta . resource_name , trailing_slash ( ) ) , self . wrap_view ( '<STR_LIT>' ) , name = "<STR_LIT>" ) , <EOL> url ( r"<STR_LIT>" % self . _meta . resource_name , self . wrap_view ( '<STR_LIT>' ) , name = "<STR_LIT>" ) , <EOL> url ( r"<STR_LIT>" % ( self . _meta . resource_name , trailing_slash ( ) ) , self . wrap_view ( '<STR_LIT>' ) , name = "<STR_LIT>" ) , <EOL> ] <EOL> class PandaAuthentication ( ApiKeyAuthentication ) : <EOL> """<STR_LIT>""" <EOL> def try_sessions ( self , request , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> csrf_token = _sanitize_token ( request . COOKIES . get ( settings . CSRF_COOKIE_NAME , '<STR_LIT>' ) ) <EOL> if request . is_secure ( ) : <EOL> referer = request . META . get ( '<STR_LIT>' ) <EOL> if referer is None : <EOL> return False <EOL> good_referer = '<STR_LIT>' % request . get_host ( ) <EOL> if not same_origin ( referer , good_referer ) : <EOL> return False <EOL> request_csrf_token = request . META . get ( '<STR_LIT>' , '<STR_LIT>' ) or request . POST . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if not constant_time_compare ( request_csrf_token , csrf_token ) : <EOL> return False <EOL> return request . user . is_authenticated ( ) <EOL> def try_api_keys ( self , request , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> email = request . META . get ( '<STR_LIT>' ) or request . GET . get ( '<STR_LIT:email>' ) <EOL> api_key = request . META . get ( '<STR_LIT>' ) or request . GET . get ( '<STR_LIT>' ) <EOL> if email : <EOL> email = unquote ( email ) <EOL> if not email or not api_key : <EOL> return False <EOL> try : <EOL> user = UserProxy . objects . get ( username = email . lower ( ) ) <EOL> except ( UserProxy . DoesNotExist , UserProxy . MultipleObjectsReturned ) : <EOL> return False <EOL> if not user . is_active : <EOL> return False <EOL> request . user = user <EOL> return self . get_key ( user , api_key ) <EOL> def is_authenticated ( self , request , ** kwargs ) : <EOL> authenticated = self . try_sessions ( request , ** kwargs ) <EOL> if authenticated : <EOL> return True <EOL> authenticated = self . try_api_keys ( request , ** kwargs ) <EOL> if authenticated : <EOL> return True <EOL> return self . _unauthorized ( ) <EOL> def get_identifier ( self , request ) : <EOL> """<STR_LIT>""" <EOL> return request . user . username <EOL> class PandaSerializer ( Serializer ) : <EOL> """<STR_LIT>""" <EOL> def format_datetime ( self , data ) : <EOL> return data . strftime ( '<STR_LIT>' ) <EOL> class PandaPaginator ( Paginator ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , request_data , objects , resource_uri = None , limit = None , offset = <NUM_LIT:0> , count = None ) : <EOL> self . count = count <EOL> super ( PandaPaginator , self ) . __init__ ( request_data , objects , resource_uri , limit , offset ) <EOL> def get_count ( self ) : <EOL> if self . count is not None : <EOL> return self . count <EOL> return super ( PandaPaginator , self ) . get_count ( ) </s>
