Refactored the HttpResponse sub-classes so that adding a subclass that only

changes the HTTP status code requires less code (no need to duplicate the
__init__ method).


git-svn-id: http://code.djangoproject.com/svn/django/trunk@4865 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Malcolm Tredinnick 2007-03-30 06:46:36 +00:00
parent 179b5b8422
commit 3c6609ac0c
1 changed files with 16 additions and 15 deletions

View File

@ -155,6 +155,9 @@ def parse_cookie(cookie):
class HttpResponse(object): class HttpResponse(object):
"A basic HTTP response, with content and dictionary-accessed headers" "A basic HTTP response, with content and dictionary-accessed headers"
status_code = 200
def __init__(self, content='', mimetype=None): def __init__(self, content='', mimetype=None):
from django.conf import settings from django.conf import settings
self._charset = settings.DEFAULT_CHARSET self._charset = settings.DEFAULT_CHARSET
@ -168,7 +171,6 @@ class HttpResponse(object):
self._is_string = True self._is_string = True
self.headers = {'Content-Type': mimetype} self.headers = {'Content-Type': mimetype}
self.cookies = SimpleCookie() self.cookies = SimpleCookie()
self.status_code = 200
def __str__(self): def __str__(self):
"Full HTTP message, including headers" "Full HTTP message, including headers"
@ -254,47 +256,46 @@ class HttpResponse(object):
return sum([len(chunk) for chunk in self._container]) return sum([len(chunk) for chunk in self._container])
class HttpResponseRedirect(HttpResponse): class HttpResponseRedirect(HttpResponse):
status_code = 302
def __init__(self, redirect_to): def __init__(self, redirect_to):
HttpResponse.__init__(self) HttpResponse.__init__(self)
self['Location'] = quote(redirect_to, safe=RESERVED_CHARS) self['Location'] = quote(redirect_to, safe=RESERVED_CHARS)
self.status_code = 302
class HttpResponsePermanentRedirect(HttpResponse): class HttpResponsePermanentRedirect(HttpResponse):
status_code = 301
def __init__(self, redirect_to): def __init__(self, redirect_to):
HttpResponse.__init__(self) HttpResponse.__init__(self)
self['Location'] = quote(redirect_to, safe=RESERVED_CHARS) self['Location'] = quote(redirect_to, safe=RESERVED_CHARS)
self.status_code = 301
class HttpResponseNotModified(HttpResponse): class HttpResponseNotModified(HttpResponse):
def __init__(self): status_code = 304
HttpResponse.__init__(self)
self.status_code = 304
class HttpResponseNotFound(HttpResponse): class HttpResponseNotFound(HttpResponse):
def __init__(self, *args, **kwargs): status_code = 404
HttpResponse.__init__(self, *args, **kwargs)
self.status_code = 404
class HttpResponseForbidden(HttpResponse): class HttpResponseForbidden(HttpResponse):
def __init__(self, *args, **kwargs): status_code = 403
HttpResponse.__init__(self, *args, **kwargs)
self.status_code = 403
class HttpResponseNotAllowed(HttpResponse): class HttpResponseNotAllowed(HttpResponse):
status_code = 405
def __init__(self, permitted_methods): def __init__(self, permitted_methods):
HttpResponse.__init__(self) HttpResponse.__init__(self)
self['Allow'] = ', '.join(permitted_methods) self['Allow'] = ', '.join(permitted_methods)
self.status_code = 405
class HttpResponseGone(HttpResponse): class HttpResponseGone(HttpResponse):
status_code = 410
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
HttpResponse.__init__(self, *args, **kwargs) HttpResponse.__init__(self, *args, **kwargs)
self.status_code = 410
class HttpResponseServerError(HttpResponse): class HttpResponseServerError(HttpResponse):
status_code = 500
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
HttpResponse.__init__(self, *args, **kwargs) HttpResponse.__init__(self, *args, **kwargs)
self.status_code = 500
def get_host(request): def get_host(request):
"Gets the HTTP host from the environment or request headers." "Gets the HTTP host from the environment or request headers."