<s> import json <EOL> from django . core . exceptions import ValidationError <EOL> from django . core import serializers <EOL> from django . forms . models import modelform_factory <EOL> from django . views . generic import FormView <EOL> from djng . views . mixins import JSONBaseMixin , JSONResponseException <EOL> class NgMissingParameterError ( ValueError ) : <EOL> pass <EOL> class NgCRUDView ( JSONBaseMixin , FormView ) : <EOL> """<STR_LIT>""" <EOL> model = None <EOL> fields = None <EOL> form_class = None <EOL> slug_field = '<STR_LIT>' <EOL> serializer_name = '<STR_LIT>' <EOL> serialize_natural_keys = False <EOL> allowed_methods = [ '<STR_LIT:GET>' , '<STR_LIT:POST>' , '<STR_LIT>' ] <EOL> exclude_methods = [ ] <EOL> def get_allowed_methods ( self ) : <EOL> return [ method for method in self . allowed_methods if method not in self . exclude_methods ] <EOL> def dispatch ( self , request , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> allowed_methods = self . get_allowed_methods ( ) <EOL> try : <EOL> if request . method == '<STR_LIT:GET>' and '<STR_LIT:GET>' in allowed_methods : <EOL> if '<STR_LIT>' in request . GET or self . slug_field in request . GET : <EOL> return self . ng_get ( request , * args , ** kwargs ) <EOL> return self . ng_query ( request , * args , ** kwargs ) <EOL> elif request . method == '<STR_LIT:POST>' and '<STR_LIT:POST>' in allowed_methods : <EOL> return self . ng_save ( request , * args , ** kwargs ) <EOL> elif request . method == '<STR_LIT>' and '<STR_LIT>' in allowed_methods : <EOL> return self . ng_delete ( request , * args , ** kwargs ) <EOL> except self . model . DoesNotExist as e : <EOL> return self . error_json_response ( e . args [ <NUM_LIT:0> ] , <NUM_LIT> ) <EOL> except NgMissingParameterError as e : <EOL> return self . error_json_response ( e . args [ <NUM_LIT:0> ] ) <EOL> except JSONResponseException as e : <EOL> return self . error_json_response ( e . args [ <NUM_LIT:0> ] , e . status_code ) <EOL> except ValidationError as e : <EOL> if hasattr ( e , '<STR_LIT>' ) : <EOL> return self . error_json_response ( '<STR_LIT>' , detail = e . message_dict ) <EOL> else : <EOL> return self . error_json_response ( e . message ) <EOL> return self . error_json_response ( '<STR_LIT>' . format ( request . method ) , <NUM_LIT> ) <EOL> def get_form_class ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . form_class or modelform_factory ( self . model , exclude = [ ] ) <EOL> def build_json_response ( self , data , ** kwargs ) : <EOL> return self . json_response ( self . serialize_queryset ( data ) , separators = ( '<STR_LIT:U+002C>' , '<STR_LIT::>' ) , ** kwargs ) <EOL> def error_json_response ( self , message , status_code = <NUM_LIT> , detail = None ) : <EOL> response_data = { <EOL> "<STR_LIT:message>" : message , <EOL> "<STR_LIT>" : detail , <EOL> } <EOL> return self . json_response ( response_data , status = status_code , separators = ( '<STR_LIT:U+002C>' , '<STR_LIT::>' ) ) <EOL> def serialize_queryset ( self , queryset ) : <EOL> """<STR_LIT>""" <EOL> object_data = [ ] <EOL> is_queryset = False <EOL> query_fields = self . get_fields ( ) <EOL> try : <EOL> iter ( queryset ) <EOL> is_queryset = True <EOL> raw_data = serializers . serialize ( self . serializer_name , queryset , fields = query_fields , <EOL> use_natural_keys = self . serialize_natural_keys ) <EOL> except TypeError : <EOL> raw_data = serializers . serialize ( self . serializer_name , [ queryset , ] , fields = query_fields , <EOL> use_natural_keys = self . serialize_natural_keys ) <EOL> for obj in raw_data : <EOL> obj [ '<STR_LIT>' ] [ '<STR_LIT>' ] = obj [ '<STR_LIT>' ] <EOL> object_data . append ( obj [ '<STR_LIT>' ] ) <EOL> if is_queryset : <EOL> return object_data <EOL> return object_data [ <NUM_LIT:0> ] <EOL> def get_form_kwargs ( self ) : <EOL> kwargs = super ( NgCRUDView , self ) . get_form_kwargs ( ) <EOL> kwargs [ '<STR_LIT:data>' ] = json . loads ( self . request . body . decode ( '<STR_LIT:utf-8>' ) ) <EOL> if '<STR_LIT>' in self . request . GET or self . slug_field in self . request . GET : <EOL> kwargs [ '<STR_LIT>' ] = self . get_object ( ) <EOL> return kwargs <EOL> def get_object ( self ) : <EOL> if '<STR_LIT>' in self . request . GET : <EOL> return self . model . objects . get ( pk = self . request . GET [ '<STR_LIT>' ] ) <EOL> elif self . slug_field in self . request . GET : <EOL> return self . model . objects . get ( ** { self . slug_field : self . request . GET [ self . slug_field ] } ) <EOL> raise NgMissingParameterError ( <EOL> "<STR_LIT>" ) <EOL> def get_fields ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . fields <EOL> def get_queryset ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . model . objects . all ( ) <EOL> def ng_query ( self , request , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> return self . build_json_response ( self . get_queryset ( ) ) <EOL> def ng_get ( self , request , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> return self . build_json_response ( self . get_object ( ) ) <EOL> def ng_save ( self , request , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> form = self . get_form ( self . get_form_class ( ) ) <EOL> if form . is_valid ( ) : <EOL> obj = form . save ( ) <EOL> return self . build_json_response ( obj ) <EOL> raise ValidationError ( form . errors ) <EOL> def ng_delete ( self , request , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if '<STR_LIT>' not in request . GET : <EOL> raise NgMissingParameterError ( "<STR_LIT>" ) <EOL> obj = self . get_object ( ) <EOL> response = self . build_json_response ( obj ) <EOL> obj . delete ( ) <EOL> return response </s>
