Ticket #12747: 12747-3.diff

File 12747-3.diff, 4.9 KB (added by Claude Paroz, 12 years ago)

Updated patch, tuple instead of string

  • django/core/handlers/wsgi.py

    diff --git a/django/core/handlers/wsgi.py b/django/core/handlers/wsgi.py
    index 7d2ee44..3b0273f 100644
    a b class WSGIHandler(base.BaseHandler):  
    238238            signals.request_finished.send(sender=self.__class__)
    239239
    240240        try:
    241             status_text = STATUS_CODE_TEXT[response.status_code]
     241            status_text = response.status_text or STATUS_CODE_TEXT[response.status_code]
    242242        except KeyError:
    243243            status_text = 'UNKNOWN STATUS CODE'
    244244        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 49acd57..0b1d279 100644
    a b class HttpResponseBase(object):  
    537537    """
    538538
    539539    status_code = 200
     540    status_text = None
    540541
    541542    def __init__(self, content_type=None, status=None, mimetype=None):
    542543        # _headers is a mapping of the lower-case name to the original case of
    class HttpResponseBase(object):  
    554555                    self._charset)
    555556        self.cookies = SimpleCookie()
    556557        if status:
    557             self.status_code = status
     558            if isinstance(status, (list, tuple)):
     559                self.status_code, self.status_text = status
     560            else:
     561                self.status_code = status
    558562
    559563        self['Content-Type'] = content_type
    560564
  • docs/ref/request-response.txt

    diff --git a/docs/ref/request-response.txt b/docs/ref/request-response.txt
    index 89d0fe8..cc0fc51 100644
    a b Attributes  
    622622
    623623    The `HTTP Status code`_ for the response.
    624624
     625.. attribute:: HttpResponse.status_text
     626
     627    .. versionadded:: 1.5
     628
     629    The `HTTP Status code`_ text for the response. This is the human readable
     630    string passed with the status code in the status line.  If not provided, the
     631    standard text corresponding to ``status_code`` is used.
     632
    625633.. attribute:: HttpResponse.streaming
    626634
    627635    This is always ``False``.
    Methods  
    650658
    651659    Historically, this parameter was called ``mimetype`` (now deprecated).
    652660
    653     ``status`` is the `HTTP Status code`_ for the response.
     661    ``status`` is either the `HTTP Status code`_ for the response or a tuple
     662    containing the status code together with a customized status text. For
     663    example::
     664
     665        status=(404, "We are so sorry, this page has not been found")
    654666
    655667
    656668.. method:: HttpResponse.__setitem__(header, value)
  • tests/regressiontests/httpwrappers/tests.py

    diff --git a/tests/regressiontests/httpwrappers/tests.py b/tests/regressiontests/httpwrappers/tests.py
    index bfb4ae1..33e1d88 100644
    a b class HttpResponseTests(unittest.TestCase):  
    283283        self.assertRaises(BadHeaderError, r.__setitem__, 'test\rstr', 'test')
    284284        self.assertRaises(BadHeaderError, r.__setitem__, 'test\nstr', 'test')
    285285
     286    def test_status(self):
     287        """
     288        Test setting of status code and status text
     289        """
     290        r = HttpResponse()
     291        self.assertEqual(r.status_code, 200)
     292        self.assertEqual(r.status_text, None)
     293        r = HttpResponse(status=(202, "Thanks!"))
     294        self.assertEqual(r.status_code, 202)
     295        self.assertEqual(r.status_text, "Thanks!")
     296
    286297    def test_dict_behavior(self):
    287298        """
    288299        Test for bug #14020: Make HttpResponse.get work like dict.get
  • tests/regressiontests/wsgi/tests.py

    diff --git a/tests/regressiontests/wsgi/tests.py b/tests/regressiontests/wsgi/tests.py
    index 6c1483d..4a6ec97 100644
    a b class WSGITest(TestCase):  
    4242            bytes(response),
    4343            b"Content-Type: text/html; charset=utf-8\r\n\r\nHello World!")
    4444
     45    def test_custom_status(self):
     46        application = get_wsgi_application()
     47        environ = RequestFactory().get('/status/').environ
     48
     49        response_data = {}
     50
     51        def start_response(status, headers):
     52            response_data["status"] = status
     53            response_data["headers"] = headers
     54
     55        response = application(environ, start_response)
     56
     57        self.assertEqual(response_data["status"], "220 Pretty good")
     58
    4559
    4660class GetInternalWSGIApplicationTest(unittest.TestCase):
    4761    @override_settings(WSGI_APPLICATION="regressiontests.wsgi.wsgi.application")
  • tests/regressiontests/wsgi/urls.py

    diff --git a/tests/regressiontests/wsgi/urls.py b/tests/regressiontests/wsgi/urls.py
    index 9639f0f..f8d40d6 100644
    a b from django.http import HttpResponse  
    44def helloworld(request):
    55    return HttpResponse("Hello World!")
    66
     7def custom_status(request):
     8    return HttpResponse("Hello", status=[220, "Pretty good"])
     9
    710urlpatterns = patterns(
    811    "",
    9     url("^$", helloworld)
     12    url("^$", helloworld),
     13    url("^status/$", custom_status),
    1014    )
Back to Top