<s> from django . forms . forms import BaseForm <EOL> from django . http import HttpResponse <EOL> from django . utils import simplejson as json <EOL> class JSONResponseMixin ( object ) : <EOL> def render_to_response ( self , context ) : <EOL> "<STR_LIT>" <EOL> return self . get_json_response ( self . convert_context_to_json ( context ) ) <EOL> def get_json_response ( self , content , ** httpresponse_kwargs ) : <EOL> "<STR_LIT>" <EOL> return HttpResponse ( content , <EOL> content_type = '<STR_LIT:application/json>' , <EOL> ** httpresponse_kwargs ) <EOL> def convert_context_to_json ( self , context ) : <EOL> for field , value in context . items ( ) : <EOL> if isinstance ( value , BaseForm ) : <EOL> instance_pk = None <EOL> if context [ field ] . instance : <EOL> instance_pk = context [ field ] . instance . pk <EOL> context [ field ] = { <EOL> '<STR_LIT>' : value . is_bound , <EOL> '<STR_LIT>' : value . is_valid ( ) , <EOL> '<STR_LIT:data>' : value . data , <EOL> '<STR_LIT>' : instance_pk , <EOL> '<STR_LIT>' : value . errors <EOL> } <EOL> return json . dumps ( context ) </s>
