From 9859e8546dbd9ef822976fbbafb00a7efc5f1f0c Mon Sep 17 00:00:00 2001 From: Calen Pennington Date: Thu, 17 May 2012 13:23:20 -0400 Subject: [PATCH] Move accept header computations out of middleware --- djangoapps/courseware/views.py | 5 +++-- lib/util/middleware.py | 26 -------------------------- lib/util/views.py | 26 ++++++++++++++++++++++++++ 3 files changed, 29 insertions(+), 28 deletions(-) diff --git a/djangoapps/courseware/views.py b/djangoapps/courseware/views.py index 61323a1dba..56409b65a5 100644 --- a/djangoapps/courseware/views.py +++ b/djangoapps/courseware/views.py @@ -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"})) diff --git a/lib/util/middleware.py b/lib/util/middleware.py index 84ea2b4656..eeffa2668c 100644 --- a/lib/util/middleware.py +++ b/lib/util/middleware.py @@ -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) diff --git a/lib/util/views.py b/lib/util/views.py index d95f1e9a22..a071208e92 100644 --- a/lib/util/views.py +++ b/lib/util/views.py @@ -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]