<s> """<STR_LIT>""" <EOL> from django . template import loader , RequestContext <EOL> from django . http import HttpResponse , Http404 <EOL> from django . http import HttpResponseRedirect , HttpResponsePermanentRedirect <EOL> from django . db . models . base import ModelBase <EOL> from django . db . models . manager import Manager <EOL> from django . db . models . query import QuerySet <EOL> from django . core import urlresolvers <EOL> from django . utils import six <EOL> import datetime <EOL> try : <EOL> import json <EOL> except Exception , e : <EOL> import simplejson as json <EOL> from . dumper import DataDumper <EOL> from dicttoxml import dicttoxml as dict2xml <EOL> from xml . dom . minidom import parseString <EOL> import yaml <EOL> def render_to_easy_api_response ( * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> httpresponse_kwargs = { '<STR_LIT>' : kwargs . pop ( '<STR_LIT>' , None ) } <EOL> context = kwargs . pop ( '<STR_LIT>' ) <EOL> processors = context . context_processors <EOL> request = processors [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> if request . GET . has_key ( '<STR_LIT>' ) : <EOL> api_type = request . GET [ '<STR_LIT>' ] <EOL> for arg in args : <EOL> passed = arg <EOL> dump_me = { } <EOL> for key in passed . keys ( ) : <EOL> value = passed [ key ] <EOL> dump_me [ key ] = dump_object ( value ) <EOL> if api_type == '<STR_LIT>' : <EOL> def replace_spaces ( dump_me ) : <EOL> new = { } <EOL> for k , v in dump_me . iteritems ( ) : <EOL> if isinstance ( v , dict ) : <EOL> v = replace_spaces ( v ) <EOL> new [ k . replace ( '<STR_LIT:U+0020>' , '<STR_LIT:_>' ) ] = v <EOL> return new <EOL> new = replace_spaces ( dump_me ) <EOL> dump_me = dict2xml ( new ) <EOL> dom = parseString ( dump_me ) <EOL> pretty = dom . toprettyxml ( ) <EOL> return HttpResponse ( pretty , content_type = '<STR_LIT>' ) <EOL> if api_type == '<STR_LIT>' : <EOL> yml = yaml . safe_dump ( dump_me ) <EOL> return HttpResponse ( yml , content_type = '<STR_LIT>' ) <EOL> else : <EOL> dump_me = json . dumps ( dump_me , indent = <NUM_LIT:2> ) <EOL> return HttpResponse ( dump_me , content_type = '<STR_LIT:application/json>' ) <EOL> return HttpResponse ( loader . render_to_string ( * args , ** kwargs ) , ** httpresponse_kwargs ) <EOL> def render_to_response ( * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> return render_to_easy_api_response ( * args , ** kwargs ) <EOL> def dump_object ( queryset ) : <EOL> if str ( type ( queryset ) ) != "<STR_LIT>" : <EOL> d = DataDumper ( ) <EOL> ret = d . dump ( queryset ) <EOL> return ret <EOL> try : <EOL> modelName = queryset [ <NUM_LIT:0> ] . __class__ . __name__ <EOL> modelNameData = [ ] <EOL> fields = get_fields ( queryset [ <NUM_LIT:0> ] ) <EOL> for obj in queryset : <EOL> temp_dict = dict ( ) <EOL> for field in fields : <EOL> try : <EOL> attribute = getattr ( obj , str ( field ) ) <EOL> temp_dict [ field ] = attribute <EOL> except Exception , e : <EOL> continue <EOL> modelNameData . append ( temp_dict ) <EOL> dthandler = lambda obj : obj . isoformat ( ) if isinstance ( obj , datetime . datetime ) or isinstance ( obj , datetime . date ) else None <EOL> return json . loads ( json . dumps ( modelNameData , default = dthandler ) ) <EOL> except Exception , e : <EOL> return '<STR_LIT>' <EOL> def get_fields ( model ) : <EOL> try : <EOL> if hasattr ( model , "<STR_LIT>" ) : <EOL> fields = model . easy_api_fields ( ) <EOL> else : <EOL> try : <EOL> fields = model . to_dict ( ) . keys ( ) <EOL> except Exception , e : <EOL> fields = model . _meta . get_all_field_names ( ) <EOL> return fields <EOL> except Exception , e : <EOL> return [ ] </s>
