2011-05-19 21:36:30 +04:00
|
|
|
"""
|
2011-12-29 17:31:12 +04:00
|
|
|
The :mod:`response` module provides Response classes you can use in your
|
|
|
|
views to return a certain HTTP response. Typically a response is *rendered*
|
2011-05-19 21:36:30 +04:00
|
|
|
into a HTTP response depending on what renderers are set on your view and
|
2011-12-29 17:31:12 +04:00
|
|
|
als depending on the accept header of the request.
|
2011-05-19 21:36:30 +04:00
|
|
|
"""
|
|
|
|
|
2011-01-24 02:08:16 +03:00
|
|
|
from django.core.handlers.wsgi import STATUS_CODE_TEXT
|
|
|
|
|
2011-05-10 19:01:58 +04:00
|
|
|
__all__ = ('Response', 'ErrorResponse')
|
2011-01-24 02:08:16 +03:00
|
|
|
|
2011-04-11 19:38:00 +04:00
|
|
|
# TODO: remove raw_content/cleaned_content and just use content?
|
2011-01-24 02:08:16 +03:00
|
|
|
|
|
|
|
class Response(object):
|
2011-05-10 19:01:58 +04:00
|
|
|
"""
|
|
|
|
An HttpResponse that may include content that hasn't yet been serialized.
|
|
|
|
"""
|
|
|
|
|
2011-06-25 19:13:58 +04:00
|
|
|
def __init__(self, status=200, content=None, headers=None):
|
2011-01-24 02:08:16 +03:00
|
|
|
self.status = status
|
2011-05-24 19:31:17 +04:00
|
|
|
self.media_type = None
|
2011-04-11 19:38:00 +04:00
|
|
|
self.has_content_body = content is not None
|
|
|
|
self.raw_content = content # content prior to filtering
|
|
|
|
self.cleaned_content = content # content after filtering
|
2011-06-25 19:13:58 +04:00
|
|
|
self.headers = headers or {}
|
2011-12-29 17:31:12 +04:00
|
|
|
|
2011-01-24 02:08:16 +03:00
|
|
|
@property
|
|
|
|
def status_text(self):
|
2011-05-10 19:01:58 +04:00
|
|
|
"""
|
|
|
|
Return reason text corresponding to our HTTP response status code.
|
|
|
|
Provided for convenience.
|
|
|
|
"""
|
2011-01-24 02:08:16 +03:00
|
|
|
return STATUS_CODE_TEXT.get(self.status, '')
|
|
|
|
|
|
|
|
|
2011-04-11 19:38:00 +04:00
|
|
|
class ErrorResponse(BaseException):
|
2011-05-10 19:01:58 +04:00
|
|
|
"""
|
|
|
|
An exception representing an Response that should be returned immediately.
|
|
|
|
Any content should be serialized as-is, without being filtered.
|
|
|
|
"""
|
|
|
|
|
2011-04-11 19:38:00 +04:00
|
|
|
def __init__(self, status, content=None, headers={}):
|
2011-01-27 22:24:58 +03:00
|
|
|
self.response = Response(status, content=content, headers=headers)
|