diff --git a/djangoapps/courseware/views.py b/djangoapps/courseware/views.py
index 9cfdc2fe9f..6f991e261c 100644
--- a/djangoapps/courseware/views.py
+++ b/djangoapps/courseware/views.py
@@ -227,7 +227,15 @@ def modx_dispatch(request, module=None, dispatch=None, id=None):
ajax_url = settings.MITX_ROOT_URL + '/modx/'+module+'/'+id+'/'
# Grab the XML corresponding to the request from course.xml
- xml = content_parser.module_xml(request.user, module, 'id', id)
+ try:
+ xml = content_parser.module_xml(request.user, module, 'id', id)
+ except:
+ record_exception(log, "Unable to load module during ajax call")
+ if 'text/html' in request.accepted_types:
+ 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"}))
+ return response
# Create the module
system = I4xSystem(track_function = make_track_function(request),
@@ -242,8 +250,11 @@ def modx_dispatch(request, module=None, dispatch=None, id=None):
id,
state=oldstate)
except:
- record_exception(log, "Unable to load module during ajax call")
- response = HttpResponse(json.dumps({'success': "We're sorry, this module is temporarily unavailable. Our staff is working to fix it as soon as possible"}))
+ record_exception(log, "Unable to load module instance during ajax call")
+ if 'text/html' in request.accepted_types:
+ 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"}))
return response
# Let the module handle the AJAX
diff --git a/envs/common.py b/envs/common.py
index 1ae4a90e8d..3576b31968 100644
--- a/envs/common.py
+++ b/envs/common.py
@@ -226,6 +226,7 @@ TEMPLATE_LOADERS = (
MIDDLEWARE_CLASSES = (
'util.middleware.ExceptionLoggingMiddleware',
+ 'util.middleware.AcceptMiddleware',
'django.middleware.cache.UpdateCacheMiddleware',
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
diff --git a/lib/util/middleware.py b/lib/util/middleware.py
index 342fff1790..84ea2b4656 100644
--- a/lib/util/middleware.py
+++ b/lib/util/middleware.py
@@ -13,3 +13,30 @@ class ExceptionLoggingMiddleware(object):
def process_exception(self, request, exception):
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/settings.py b/settings.py
index 21458b5fef..e0119c6fb0 100644
--- a/settings.py
+++ b/settings.py
@@ -140,6 +140,7 @@ TEMPLATE_LOADERS = (
MIDDLEWARE_CLASSES = (
'util.middleware.ExceptionLoggingMiddleware',
+ 'util.middleware.AcceptMiddleware',
'django.middleware.cache.UpdateCacheMiddleware',
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
diff --git a/templates/courseware-error.html b/templates/courseware-error.html
index 547c43e221..0863ffe7e8 100644
--- a/templates/courseware-error.html
+++ b/templates/courseware-error.html
@@ -2,43 +2,11 @@
<%block name="bodyclass">courseware%block>
<%block name="title">
Courseware – MITx 6.002x%block>
-<%block name="headextra">
-
-%block>
-
-<%block name="js_extra">
-##Is there a reason this isn't in header_extra? Is it important that the javascript is at the bottom of the generated document?
-
-
-%block>
-
<%include file="navigation.html" args="active_page='courseware'" />
-
We're sorry, this module is temporarily unavailable. Our staff is working to fix it as soon as possible. Please email us at technical@mitx.mit.edu to report any problems or downtime.