<s> from django . http import HttpResponse <EOL> from comrade . utils import extract <EOL> from comrade . exceptions import BadRequestError <EOL> try : <EOL> import piston . utils <EOL> import piston . emitters <EOL> except ImportError : <EOL> pass <EOL> else : <EOL> piston . emitters . Emitter . register ( '<STR_LIT>' , piston . emitters . XMLEmitter , <EOL> '<STR_LIT>' ) <EOL> piston . emitters . Emitter . register ( '<STR_LIT:application/json>' , <EOL> piston . emitters . JSONEmitter , '<STR_LIT>' ) <EOL> class ContentNegotiationMixin ( object ) : <EOL> """<STR_LIT>""" <EOL> api_context_include_keys = set ( ) <EOL> api_context_exclude_keys = set ( ) <EOL> fields = ( ) <EOL> def get_fields ( self ) : <EOL> return self . fields <EOL> def get_minimal_context_keys ( self , context ) : <EOL> """<STR_LIT>""" <EOL> if self . api_context_include_keys : <EOL> return self . api_context_include_keys <EOL> elif self . api_context_exclude_keys : <EOL> return set ( context . keys ( ) ) - self . api_context_exclude_keys <EOL> else : <EOL> return context . keys ( ) <EOL> def get_minimal_context ( self , context ) : <EOL> """<STR_LIT>""" <EOL> return extract ( context , self . get_minimal_context_keys ( context ) ) <EOL> def get_api_context_data ( self , context ) : <EOL> return self . get_minimal_context ( context ) <EOL> def get_api_response ( self , context , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> accepted_types = self . _determine_accepted_types ( self . request ) <EOL> for accepted_type in accepted_types : <EOL> try : <EOL> emitter , ct = piston . emitters . Emitter . get ( accepted_type ) <EOL> except ValueError : <EOL> pass <EOL> else : <EOL> srl = emitter ( self . get_api_context_data ( context ) , { } , None , <EOL> self . fields ) <EOL> try : <EOL> stream = srl . render ( self . request ) <EOL> if not isinstance ( stream , HttpResponse ) : <EOL> response = HttpResponse ( stream , mimetype = ct , ** kwargs ) <EOL> else : <EOL> response = stream <EOL> return response <EOL> except piston . utils . HttpStatusCode , e : <EOL> return e . response <EOL> def render_to_response ( self , context , ** kwargs ) : <EOL> response = self . get_api_response ( context , ** kwargs ) <EOL> if not response : <EOL> response = super ( ContentNegotiationMixin , self <EOL> ) . render_to_response ( context , ** kwargs ) <EOL> return response <EOL> def _determine_accepted_types ( self , request ) : <EOL> """<STR_LIT>""" <EOL> if ( "<STR_LIT>" in request . accepted_types <EOL> or request . accepted_types == [ '<STR_LIT>' ] ) : <EOL> return [ ] <EOL> return request . accepted_types <EOL> class BatchJSONMixin ( object ) : <EOL> """<STR_LIT>""" <EOL> batch_json_key = '<STR_LIT:list>' <EOL> def get_batch_json_key ( self ) : <EOL> return self . batch_json_key <EOL> def get_batch_json ( self , key = None ) : <EOL> key = key or self . get_batch_json_key ( ) <EOL> batch = self . request . data . get ( key ) <EOL> if batch is None : <EOL> raise BadRequestError ( "<STR_LIT>" ) <EOL> return batch <EOL> def post_json ( self , request , * args , ** kwargs ) : <EOL> batch = self . get_batch_json ( ) <EOL> form_class = self . get_form_class ( ) <EOL> form = None <EOL> for data in batch : <EOL> self . form_data = data <EOL> form = form_class ( ** self . get_form_kwargs ( ) ) <EOL> if not form . is_valid ( ) : <EOL> return self . form_invalid ( form ) <EOL> form . save ( ) <EOL> return self . get_success_response ( form ) <EOL> def post ( self , request , * args , ** kwargs ) : <EOL> content_type = self . request . META . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if ( '<STR_LIT:application/json>' in content_type <EOL> and self . get_batch_json_key ( ) in request . data ) : <EOL> return self . post_json ( request , * args , ** kwargs ) <EOL> return super ( BatchJSONMixin , self ) . post ( request , * args , ** kwargs ) </s>
