<s> import re <EOL> import django <EOL> from django . http import HttpResponseNotAllowed , QueryDict <EOL> from django . views . decorators . csrf import csrf_exempt <EOL> from django . shortcuts import get_object_or_404 <EOL> from django . db import IntegrityError <EOL> from django . contrib . auth . models import User <EOL> from django . utils . decorators import method_decorator <EOL> from . models import Device <EOL> from . forms import DeviceForm <EOL> from . decorators import api_authentication_required <EOL> from . http import HttpResponseNotImplemented , JSONResponse <EOL> class BaseResource ( object ) : <EOL> """<STR_LIT>""" <EOL> allowed_methods = ( '<STR_LIT:GET>' , '<STR_LIT:POST>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> @ method_decorator ( api_authentication_required ) <EOL> @ csrf_exempt <EOL> def route ( self , request , ** kwargs ) : <EOL> method = request . method <EOL> if method in self . allowed_methods : <EOL> if hasattr ( self , method . lower ( ) ) : <EOL> if method == '<STR_LIT>' : <EOL> request . PUT = QueryDict ( request . body if django . VERSION >= ( <NUM_LIT:1> , <NUM_LIT:4> ) else request . raw_post_data ) . copy ( ) <EOL> return getattr ( self , method . lower ( ) ) ( request , ** kwargs ) <EOL> return HttpResponseNotImplemented ( ) <EOL> return HttpResponseNotAllowed ( self . allowed_methods ) <EOL> class DeviceResource ( BaseResource ) : <EOL> """<STR_LIT>""" <EOL> allowed_methods = ( '<STR_LIT:GET>' , '<STR_LIT:POST>' , '<STR_LIT>' ) <EOL> def get ( self , request , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> device = get_object_or_404 ( Device , ** kwargs ) <EOL> return JSONResponse ( device ) <EOL> def post ( self , request , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> token = request . POST . get ( '<STR_LIT>' ) <EOL> if token is not None : <EOL> token = re . sub ( '<STR_LIT>' , '<STR_LIT>' , token ) <EOL> devices = Device . objects . filter ( token = token , <EOL> service__id = int ( request . POST . get ( '<STR_LIT>' , <NUM_LIT:0> ) ) ) <EOL> if devices . exists ( ) : <EOL> device = devices . get ( ) <EOL> device . is_active = True <EOL> device . save ( ) <EOL> return JSONResponse ( device ) <EOL> form = DeviceForm ( request . POST ) <EOL> if form . is_valid ( ) : <EOL> device = form . save ( commit = False ) <EOL> device . is_active = True <EOL> device . save ( ) <EOL> return JSONResponse ( device , status = <NUM_LIT> ) <EOL> return JSONResponse ( form . errors , status = <NUM_LIT> ) <EOL> def put ( self , request , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> device = Device . objects . get ( ** kwargs ) <EOL> except Device . DoesNotExist : <EOL> return JSONResponse ( { '<STR_LIT:error>' : '<STR_LIT>' % <EOL> ( kwargs [ '<STR_LIT>' ] , kwargs [ '<STR_LIT>' ] ) } , status = <NUM_LIT> ) <EOL> if '<STR_LIT>' in request . PUT : <EOL> try : <EOL> user_ids = request . PUT . getlist ( '<STR_LIT>' ) <EOL> device . users . remove ( * [ u . id for u in device . users . all ( ) ] ) <EOL> device . users . add ( * User . objects . filter ( id__in = user_ids ) ) <EOL> except ( ValueError , IntegrityError ) as e : <EOL> return JSONResponse ( { '<STR_LIT:error>' : e . message } , status = <NUM_LIT> ) <EOL> del request . PUT [ '<STR_LIT>' ] <EOL> for key , value in request . PUT . items ( ) : <EOL> setattr ( device , key , value ) <EOL> device . save ( ) <EOL> return JSONResponse ( device ) <EOL> class Router ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> self . device = DeviceResource ( ) . route <EOL> routes = Router ( ) </s>
