Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Linked data decorator hack for rdf/xml and html representations:
def company(request, company_id):
# return html for company
def company_rdf(request, company_id):
# return rdf/xml for company
import re
import django.core
import mimeparse
def rdf_view(f):
def f1(request, **kwargs):
# construct a http redirect response to html view
html_view = f.func_name.replace('_rdf', '')
html_url = django.core.urlresolvers.reverse(html_view, kwargs=kwargs)
html_redirect = responses.HttpResponseSeeOther(html_url)
# determine the clients preferred representation
available = ['text/html', 'application/rdf+xml']
accept = request.META.get('HTTP_ACCEPT', 'application/rdf+xml')
match = mimeparse.best_match(available, accept)
# figure out what user agent we are talking to
ua = request.META.get('HTTP_USER_AGENT')
if request.get_full_path().endswith('.rdf'):
return f(request, **kwargs)
elif ua and 'MSIE' in ua:
return html_redirect
elif match == 'application/rdf+xml':
response = f(request, **kwargs)
response['Vary'] = 'Accept'
return response
elif match == 'text/html':
return html_redirect
return responses.HttpResponseUnsupportedMediaType()
return f1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.