community-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From n...@apache.org
Subject svn commit: r1239947 - /comdev/nearby_people/nearby/shortcuts.py
Date Fri, 03 Feb 2012 00:05:11 GMT
Author: nick
Date: Fri Feb  3 00:05:11 2012
New Revision: 1239947

URL: http://svn.apache.org/viewvc?rev=1239947&view=rev
Log:
Helper for identifying the requested response type, from either HTTP Request Headers or a
URL parameter, initially for HTML+JSON+RDF (where requested by a supporting page)

Modified:
    comdev/nearby_people/nearby/shortcuts.py

Modified: comdev/nearby_people/nearby/shortcuts.py
URL: http://svn.apache.org/viewvc/comdev/nearby_people/nearby/shortcuts.py?rev=1239947&r1=1239946&r2=1239947&view=diff
==============================================================================
--- comdev/nearby_people/nearby/shortcuts.py (original)
+++ comdev/nearby_people/nearby/shortcuts.py Fri Feb  3 00:05:11 2012
@@ -21,6 +21,11 @@ from django.http import HttpRequest, Htt
 from django.shortcuts import render_to_response
 from django.template import RequestContext
 
+mimetypes = {
+   "json": "application/json",
+   "rdf":  "application/rdf+xml"
+}
+
 def render(request, template_name, data_dict=None, content_type=None):
     assert isinstance(request, HttpRequest)
     return render_to_response(
@@ -29,6 +34,31 @@ def render(request, template_name, data_
         mimetype=content_type
     )
 
+def identify_type(request, allowed_types=None):
+    """Identifies, based on Content Type, Accept Headers etc, the required
+       response type, such as HTML or JSON"""
+    if allowed_types == None or len(allowed_types) == 0:
+        allowed_types = ["html", "json"]
+
+    # Did they specify a format?
+    if request.GET.has_key("format"):
+        format = request.GET.get("format")
+        if format in allowed_types:
+            return format
+
+    # Check the inbound Content Type, then the Accept Header
+    meta_checks = ["CONTENT_TYPE", "HTTP_ACCEPT"]
+    for meta in meta_checks:
+        meta_value = request.META.get(meta, "")
+        if meta_value in mimetypes.values():
+            for type in mimetypes.keys():
+                if mimetypes[type] == meta_value:
+                   if type in allowed_types:
+                      return type
+
+    # Otherwise default to the first allowed type (normally html)
+    return allowed_types[0]
+
 def add_message(request, message):
     "Adds a message to be shown on next page load."
     request.user.message_set.create(message=message)



Mime
View raw message