Django

Code

Changeset 4865

Show
Ignore:
Timestamp:
03/30/07 01:46:36 (2 years ago)
Author:
mtredinnick
Message:

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

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/http/__init__.py

    r4615 r4865  
    156156class HttpResponse(object): 
    157157    "A basic HTTP response, with content and dictionary-accessed headers" 
     158 
     159    status_code = 200 
     160 
    158161    def __init__(self, content='', mimetype=None): 
    159162        from django.conf import settings 
     
    169172        self.headers = {'Content-Type': mimetype} 
    170173        self.cookies = SimpleCookie() 
    171         self.status_code = 200 
    172174 
    173175    def __str__(self): 
     
    255257 
    256258class HttpResponseRedirect(HttpResponse): 
     259    status_code = 302 
     260 
    257261    def __init__(self, redirect_to): 
    258262        HttpResponse.__init__(self) 
    259263        self['Location'] = quote(redirect_to, safe=RESERVED_CHARS) 
    260         self.status_code = 302 
    261264 
    262265class HttpResponsePermanentRedirect(HttpResponse): 
     266    status_code = 301 
     267 
    263268    def __init__(self, redirect_to): 
    264269        HttpResponse.__init__(self) 
    265270        self['Location'] = quote(redirect_to, safe=RESERVED_CHARS) 
    266         self.status_code = 301 
    267271 
    268272class HttpResponseNotModified(HttpResponse): 
    269     def __init__(self): 
    270         HttpResponse.__init__(self) 
    271         self.status_code = 304 
     273    status_code = 304 
    272274 
    273275class HttpResponseNotFound(HttpResponse): 
    274     def __init__(self, *args, **kwargs): 
    275         HttpResponse.__init__(self, *args, **kwargs) 
    276         self.status_code = 404 
     276    status_code = 404 
    277277 
    278278class HttpResponseForbidden(HttpResponse): 
    279     def __init__(self, *args, **kwargs): 
    280         HttpResponse.__init__(self, *args, **kwargs) 
    281         self.status_code = 403 
     279    status_code = 403 
    282280 
    283281class HttpResponseNotAllowed(HttpResponse): 
     282    status_code = 405 
     283 
    284284    def __init__(self, permitted_methods): 
    285285        HttpResponse.__init__(self) 
    286286        self['Allow'] = ', '.join(permitted_methods) 
    287         self.status_code = 405 
    288287 
    289288class HttpResponseGone(HttpResponse): 
     289    status_code = 410 
     290 
    290291    def __init__(self, *args, **kwargs): 
    291292        HttpResponse.__init__(self, *args, **kwargs) 
    292         self.status_code = 410 
    293293 
    294294class HttpResponseServerError(HttpResponse): 
     295    status_code = 500 
     296 
    295297    def __init__(self, *args, **kwargs): 
    296298        HttpResponse.__init__(self, *args, **kwargs) 
    297         self.status_code = 500 
    298299 
    299300def get_host(request):