I want to be able to display set of data differently according to url parameters.
My URL looks like /page/{limit}/{offset}/{format}/.
For example:
/page/20/0/xml/ - subset [0:20) in xml
/page/100/20/json/ - subset [20:100) in json
Also I want to be able to do the same for csv, text, excel, pdf, html, etc...
I have to be able to set different mimetypes and content-types for different formats. For XML should be application/xhtml+xml, for csv - text/plain, etc...
In HTML mode I want to be able to pass this data into some template (I'm using Django).
I'm planing to make set look like:
dataset = {
"meta" : {"offset" : 15, "limit" : 10, "total" : 1000},
"columns" : {"name" : "Name", "status" : "Status", "creation_date" : "Creation Date"}
"items" :
[
{"name" : "John Smith", "status" : 1, "creation_date" : "2009-06-30 10:10:09"},
{"name" : "Joe The Plummer", "status" : 2, "creation_date" : "2009-06-30 10:10:09"}
]
};
and have output like this:
CSV output:
Name, Status, Creation Date
John Smith, 1, 2009-06-30 10:10:09
Joe The Plummer, 2, 2009-06-30 10:10:09
XML output:
<items>
<item id="1">
<name>John Smith</name>
<status>1</status>
<creation_date>2009-06-30 10:10:09</creation_date>
</item>
<item id="2">
<name>Joe The Plummer</name>
<status>2</status>
<creation_date>2009-06-30 10:10:09</creation_date>
</item>
</items>
So I think to have implemented my own renderers for each type - like XMLRenderer, RSSRenderer, JSONRenderer, etc...
if format == "xml":
context = XMLRenderer().render(data = dataset)
return HttpResponse(content, mimetype="application/xhtml+xml")
elif format == "json":
context = JSONRenderer().render(data = dataset)
return HttpResponse(content, mimetype="text/plain")
elif format == "rss":
context = RSSRenderer(title="Some long title here", link="/page/10/10/rss/").render(data = dataset)
return HttpResponse(content, mimetype="application/xhtml+xml")
# few more formats...
else:
return render_to_response(SOME_TEMPLATE, dataset)
Is it correct approach?