Ticket #12747: 0001-Ticket-12747-Set-custom-status_text-message.patch

File 0001-Ticket-12747-Set-custom-status_text-message.patch, 4.7 KB (added by gisle, 14 years ago)
  • django/core/handlers/wsgi.py

    From b0467bac975abdc2fa3b49e650f92d9f81fcd77e Mon Sep 17 00:00:00 2001
    From: Gisle Aas <gisle@aas.no>
    Date: Mon, 22 Feb 2010 17:31:54 -0500
    Subject: [PATCH 1/2] Ticket #12747 Set custom status_text message
    
    ---
     django/core/handlers/wsgi.py                |    2 +-
     django/http/__init__.py                     |   14 +++++++++++++-
     docs/ref/request-response.txt               |   11 ++++++++++-
     tests/regressiontests/httpwrappers/tests.py |   20 ++++++++++++++++++++
     4 files changed, 44 insertions(+), 3 deletions(-)
    
    diff --git a/django/core/handlers/wsgi.py b/django/core/handlers/wsgi.py
    index 927b098..0320048 100644
    a b class WSGIHandler(base.BaseHandler):  
    248248            signals.request_finished.send(sender=self.__class__)
    249249
    250250        try:
    251             status_text = STATUS_CODE_TEXT[response.status_code]
     251            status_text = response.status_text or STATUS_CODE_TEXT[response.status_code]
    252252        except KeyError:
    253253            status_text = 'UNKNOWN STATUS CODE'
    254254        status = '%s %s' % (response.status_code, status_text)
  • django/http/__init__.py

    diff --git a/django/http/__init__.py b/django/http/__init__.py
    index 6353637..b2cd794 100644
    a b def parse_cookie(cookie):  
    293293        cookiedict[key] = c.get(key).value
    294294    return cookiedict
    295295
     296class BadStatusError(ValueError):
     297    pass
     298
    296299class BadHeaderError(ValueError):
    297300    pass
    298301
    class HttpResponse(object):  
    300303    """A basic HTTP response, with content and dictionary-accessed headers."""
    301304
    302305    status_code = 200
     306    status_text = None
    303307
    304308    def __init__(self, content='', mimetype=None, status=None,
    305309            content_type=None):
    class HttpResponse(object):  
    318322            self._is_string = True
    319323        self.cookies = CompatCookie()
    320324        if status:
    321             self.status_code = status
     325            if isinstance(status, basestring):
     326                m = re.match(r'(\d{3}) +([^\n]*)$', status)
     327                if m:
     328                    self.status_code = int(m.group(1))
     329                    self.status_text = m.group(2)
     330                else:
     331                    raise BadStatusError("Bad HTTP status line string (%r)" % (status))
     332            else:
     333                self.status_code = status
    322334
    323335        # _headers is a mapping of the lower-case name to the original case of
    324336        # the header (required for working with legacy systems) and the header
  • docs/ref/request-response.txt

    diff --git a/docs/ref/request-response.txt b/docs/ref/request-response.txt
    index 1788e06..7e74dfd 100644
    a b Attributes  
    466466
    467467    The `HTTP Status code`_ for the response.
    468468
     469.. attribute:: HttpResponse.status_text
     470
     471    The `HTTP Status code`_ text for the response. This is the human readable
     472    string passed with the status code in the status line.  If not provided the
     473    standard text corresponding to ``status_code`` is used.
     474
    469475Methods
    470476-------
    471477
    Methods  
    478484    return strings, and those strings will be joined together to form the
    479485    content of the response.
    480486
    481     ``status`` is the `HTTP Status code`_ for the response.
     487    ``status`` is the `HTTP Status code`_ for the response.  Normally this will
     488    be an integer, but if passed as a string it should be on the form "200 OK"
     489    and this will then intialize both the ``status_code`` and ``status_text``
     490    attribute of the response.
    482491
    483492    .. versionadded:: 1.0
    484493
  • tests/regressiontests/httpwrappers/tests.py

    diff --git a/tests/regressiontests/httpwrappers/tests.py b/tests/regressiontests/httpwrappers/tests.py
    index 132472c..c842602 100644
    a b class Cookies(TestCase):  
    503503        c2.load(c.output())
    504504        self.assertEqual(c['test'].value, c2['test'].value)
    505505
     506class Response(TestCase):
     507
     508    def test_status(self):
     509        """
     510        Test setting of status code in the constructor call
     511        """
     512        r = HttpResponse()
     513        self.assertEqual(r.status_code, 200)
     514        self.assertEqual(r.status_text, None)
     515        r = HttpResponse(status=404)
     516        self.assertEqual(r.status_code, 404)
     517        self.assertEqual(r.status_text, None)
     518        r = HttpResponse(status="202 Thanks!")
     519        self.assertEqual(r.status_code, 202)
     520        self.assertEqual(r.status_text, "Thanks!")
     521
     522        from django.http import BadStatusError
     523        self.assertRaises(ValueError, lambda: HttpResponse(status="OK"))
     524        self.assertRaises(BadStatusError, lambda: HttpResponse(status="OK"))
     525
    506526if __name__ == "__main__":
    507527    import doctest
    508528    doctest.testmod()
Back to Top