<s> from django . http import HttpResponse , HttpResponseBadRequest <EOL> from django . utils . decorators import method_decorator <EOL> from django . views . decorators . csrf import csrf_exempt <EOL> from django . views . generic import View <EOL> from django . core . serializers . json import DjangoJSONEncoder <EOL> from jwt_auth . compat import json , smart_text <EOL> from jwt_auth . forms import JSONWebTokenForm <EOL> class ObtainJSONWebToken ( View ) : <EOL> http_method_names = [ '<STR_LIT>' ] <EOL> error_response_dict = { '<STR_LIT>' : [ '<STR_LIT>' ] } <EOL> json_encoder_class = DjangoJSONEncoder <EOL> @ method_decorator ( csrf_exempt ) <EOL> def dispatch ( self , request , * args , ** kwargs ) : <EOL> return super ( ObtainJSONWebToken , self ) . dispatch ( request , * args , ** kwargs ) <EOL> def post ( self , request , * args , ** kwargs ) : <EOL> try : <EOL> request_json = json . loads ( smart_text ( request . body ) ) <EOL> except ValueError : <EOL> return self . render_bad_request_response ( ) <EOL> form = JSONWebTokenForm ( request_json ) <EOL> if not form . is_valid ( ) : <EOL> return self . render_bad_request_response ( { '<STR_LIT>' : form . errors } ) <EOL> context_dict = { <EOL> '<STR_LIT>' : form . object [ '<STR_LIT>' ] <EOL> } <EOL> return self . render_response ( context_dict ) <EOL> def render_response ( self , context_dict ) : <EOL> json_context = json . dumps ( context_dict , cls = self . json_encoder_class ) <EOL> return HttpResponse ( json_context , content_type = '<STR_LIT:application/json>' ) <EOL> def render_bad_request_response ( self , error_dict = None ) : <EOL> if error_dict is None : <EOL> error_dict = self . error_response_dict <EOL> json_context = json . dumps ( error_dict , cls = self . json_encoder_class ) <EOL> return HttpResponseBadRequest ( <EOL> json_context , content_type = '<STR_LIT:application/json>' ) <EOL> obtain_jwt_token = ObtainJSONWebToken . as_view ( ) </s>
