<s> from django . conf . urls import url <EOL> from django . core . exceptions import ObjectDoesNotExist <EOL> from django . http import HttpResponse , HttpResponseBadRequest <EOL> from tastypie . authentication import Authentication <EOL> from tastypie . authorization import Authorization <EOL> from tastypie . resources import Resource <EOL> from tastypie . serializers import Serializer <EOL> from . import signals <EOL> from . forms import RegisterDeviceForm , UnregisterDeviceForm <EOL> from . models import get_device_model <EOL> class DeviceResource ( Resource ) : <EOL> DEVICE_ID_FIELD_NAME = '<STR_LIT>' <EOL> model_class = get_device_model ( ) <EOL> register_form_class = RegisterDeviceForm <EOL> unregister_form_class = UnregisterDeviceForm <EOL> object = None <EOL> request = None <EOL> class Meta : <EOL> resource_name = '<STR_LIT>' <EOL> allowed_methods = [ '<STR_LIT>' ] <EOL> authentication = Authentication ( ) <EOL> authorization = Authorization ( ) <EOL> serializer = Serializer ( formats = [ '<STR_LIT>' ] ) <EOL> def prepend_urls ( self ) : <EOL> return [ <EOL> url ( r"<STR_LIT>" % <EOL> self . _meta . resource_name , self . wrap_view ( '<STR_LIT>' ) , <EOL> name = "<STR_LIT>" ) , <EOL> url ( r"<STR_LIT>" % <EOL> self . _meta . resource_name , self . wrap_view ( '<STR_LIT>' ) , <EOL> name = "<STR_LIT>" ) , <EOL> ] <EOL> def _verify ( self , request ) : <EOL> self . is_authenticated ( request ) <EOL> self . throttle_check ( request ) <EOL> self . method_check ( request , self . _meta . allowed_methods ) <EOL> def get_form_kwargs ( self ) : <EOL> data = self . deserialize ( self . request , self . request . body ) <EOL> kwargs = { '<STR_LIT:data>' : data } <EOL> try : <EOL> kwargs [ '<STR_LIT>' ] = self . get_instance ( data ) <EOL> except ObjectDoesNotExist : <EOL> pass <EOL> return kwargs <EOL> def get_form ( self , form_class ) : <EOL> return form_class ( ** self . get_form_kwargs ( ) ) <EOL> def get_queryset ( self ) : <EOL> return self . model_class . objects . all ( ) <EOL> def get_instance ( self , data ) : <EOL> kwargs = { self . DEVICE_ID_FIELD_NAME : <EOL> data . get ( self . DEVICE_ID_FIELD_NAME ) } <EOL> return self . get_queryset ( ) . get ( ** kwargs ) <EOL> def form_valid ( self , form ) : <EOL> self . object = form . save ( ) <EOL> return HttpResponse <EOL> def form_invalid ( self , form ) : <EOL> return HttpResponseBadRequest <EOL> def _form_processing ( self , form_class , request ) : <EOL> self . _verify ( request ) <EOL> self . request = request <EOL> form = self . get_form ( form_class ) <EOL> if form . is_valid ( ) : <EOL> response_class = self . form_valid ( form ) <EOL> else : <EOL> response_class = self . form_invalid ( form ) <EOL> return self . create_response ( request , data = { } , <EOL> response_class = response_class ) <EOL> def register ( self , request , ** kwargs ) : <EOL> form_class = self . register_form_class <EOL> response = self . _form_processing ( form_class , request ) <EOL> signals . device_registered . send ( sender = self , device = self . object , <EOL> request = request ) <EOL> return response <EOL> def unregister ( self , request , ** kwargs ) : <EOL> form_class = self . unregister_form_class <EOL> response = self . _form_processing ( form_class , request ) <EOL> signals . device_unregistered . send ( sender = self , device = self . object , <EOL> request = request ) <EOL> return response </s>
