Display the appropriate type of error message depending on the type of the incoming request

This commit is contained in:
Calen Pennington
2012-05-16 13:45:00 -04:00
parent 698d0a167e
commit 69321b4497
5 changed files with 47 additions and 39 deletions

View File

@@ -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)