Move accept header computations out of middleware
This commit is contained in:
@@ -18,6 +18,7 @@ from module_render import render_module, make_track_function, I4xSystem
|
||||
from models import StudentModule
|
||||
from student.models import UserProfile
|
||||
from util.errors import record_exception
|
||||
from util.views import accepts
|
||||
from multicourse import multicourse_settings
|
||||
|
||||
import courseware.content_parser as content_parser
|
||||
@@ -258,7 +259,7 @@ def modx_dispatch(request, module=None, dispatch=None, id=None):
|
||||
xml = content_parser.module_xml(request.user, module, 'id', id, coursename)
|
||||
except:
|
||||
record_exception(log, "Unable to load module during ajax call")
|
||||
if 'text/html' in request.accepted_types:
|
||||
if accepts(request, 'text/html'):
|
||||
return render_to_response("module-error.html", {})
|
||||
else:
|
||||
response = HttpResponse(json.dumps({'success': "We're sorry, this module is temporarily unavailable. Our staff is working to fix it as soon as possible"}))
|
||||
@@ -278,7 +279,7 @@ def modx_dispatch(request, module=None, dispatch=None, id=None):
|
||||
state=oldstate)
|
||||
except:
|
||||
record_exception(log, "Unable to load module instance during ajax call")
|
||||
if 'text/html' in request.accepted_types:
|
||||
if accepts(request, 'text/html'):
|
||||
return render_to_response("module-error.html", {})
|
||||
else:
|
||||
response = HttpResponse(json.dumps({'success': "We're sorry, this module is temporarily unavailable. Our staff is working to fix it as soon as possible"}))
|
||||
|
||||
@@ -14,29 +14,3 @@ class ExceptionLoggingMiddleware(object):
|
||||
log.exception(exception)
|
||||
return HttpResponseServerError("Server Error - Please try again later.")
|
||||
|
||||
# From http://djangosnippets.org/snippets/1042/
|
||||
def parse_accept_header(accept):
|
||||
"""Parse the Accept header *accept*, returning a list with pairs of
|
||||
(media_type, q_value), ordered by q values.
|
||||
"""
|
||||
result = []
|
||||
for media_range in accept.split(","):
|
||||
parts = media_range.split(";")
|
||||
media_type = parts.pop(0)
|
||||
media_params = []
|
||||
q = 1.0
|
||||
for part in parts:
|
||||
(key, value) = part.lstrip().split("=", 1)
|
||||
if key == "q":
|
||||
q = float(value)
|
||||
else:
|
||||
media_params.append((key, value))
|
||||
result.append((media_type, tuple(media_params), q))
|
||||
result.sort(lambda x, y: -cmp(x[2], y[2]))
|
||||
return result
|
||||
|
||||
class AcceptMiddleware(object):
|
||||
def process_request(self, request):
|
||||
accept = parse_accept_header(request.META.get("HTTP_ACCEPT", ""))
|
||||
request.accept = accept
|
||||
request.accepted_types = map(lambda (t, p, q): t, accept)
|
||||
|
||||
@@ -66,3 +66,29 @@ def mitxhome(request):
|
||||
if settings.ENABLE_MULTICOURSE:
|
||||
return render_to_response("mitxhome.html", {})
|
||||
return info(request)
|
||||
|
||||
# From http://djangosnippets.org/snippets/1042/
|
||||
def parse_accept_header(accept):
|
||||
"""Parse the Accept header *accept*, returning a list with pairs of
|
||||
(media_type, q_value), ordered by q values.
|
||||
"""
|
||||
result = []
|
||||
for media_range in accept.split(","):
|
||||
parts = media_range.split(";")
|
||||
media_type = parts.pop(0)
|
||||
media_params = []
|
||||
q = 1.0
|
||||
for part in parts:
|
||||
(key, value) = part.lstrip().split("=", 1)
|
||||
if key == "q":
|
||||
q = float(value)
|
||||
else:
|
||||
media_params.append((key, value))
|
||||
result.append((media_type, tuple(media_params), q))
|
||||
result.sort(lambda x, y: -cmp(x[2], y[2]))
|
||||
return result
|
||||
|
||||
def accepts(request, media_type):
|
||||
"""Return whether this request has an Accept header that matches type"""
|
||||
accept = parse_accept_header(request.META.get("HTTP_ACCEPT", ""))
|
||||
return media_type in [t for (t, p, q) in accept]
|
||||
|
||||
Reference in New Issue
Block a user