<s> from django . http import HttpResponse <EOL> from django . views . decorators . csrf import csrf_exempt <EOL> from rest_framework . renderers import JSONRenderer <EOL> from rest_framework . parsers import JSONParser <EOL> from apitestapp . models import Snippet <EOL> from apitestapp . serializers import SnippetSerializer <EOL> class JSONResponse ( HttpResponse ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , data , ** kwargs ) : <EOL> content = JSONRenderer ( ) . render ( data ) <EOL> kwargs [ '<STR_LIT>' ] = '<STR_LIT:application/json>' <EOL> super ( JSONResponse , self ) . __init__ ( content , ** kwargs ) <EOL> @ csrf_exempt <EOL> def snippet_list ( request ) : <EOL> """<STR_LIT>""" <EOL> if request . method == '<STR_LIT:GET>' : <EOL> snippets = Snippet . objects . all ( ) <EOL> serializer = SnippetSerializer ( snippets , many = True ) <EOL> return JSONResponse ( serializer . data ) <EOL> elif request . method == '<STR_LIT:POST>' : <EOL> data = JSONParser ( ) . parse ( request ) <EOL> serializer = SnippetSerializer ( data = data ) <EOL> if serializer . is_valid ( ) : <EOL> serializer . save ( ) <EOL> return JSONResponse ( serializer . data , status = <NUM_LIT> ) <EOL> return JSONResponse ( serializer . errors , status = <NUM_LIT> ) <EOL> @ csrf_exempt <EOL> def snippet_detail ( request , pk ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> snippet = Snippet . objects . get ( pk = pk ) <EOL> except Snippet . DoesNotExist : <EOL> return HttpResponse ( status = <NUM_LIT> ) <EOL> if request . method == '<STR_LIT:GET>' : <EOL> serializer = SnippetSerializer ( snippet ) <EOL> return JSONResponse ( serializer . data ) <EOL> elif request . method == '<STR_LIT>' : <EOL> data = JSONParser ( ) . parse ( request ) <EOL> serializer = SnippetSerializer ( snippet , data = data ) <EOL> if serializer . is_valid ( ) : <EOL> serializer . save ( ) <EOL> return JSONResponse ( serializer . data ) <EOL> return JSONResponse ( serializer . errors , status = <NUM_LIT> ) <EOL> elif request . method == '<STR_LIT>' : <EOL> snippet . delete ( ) <EOL> return HttpResponse ( status = <NUM_LIT> ) </s>
