Kaydet (Commit) df0523de authored tarafından Berker Peksag's avatar Berker Peksag Kaydeden (comit) Tim Graham

Fixed #23531 -- Added CommonMiddleware.response_redirect_class.

üst 83daf536
...@@ -30,11 +30,16 @@ class CommonMiddleware(object): ...@@ -30,11 +30,16 @@ class CommonMiddleware(object):
urlpatterns, then an HTTP-redirect is returned to this new URL; urlpatterns, then an HTTP-redirect is returned to this new URL;
otherwise the initial URL is processed as usual. otherwise the initial URL is processed as usual.
This behavior can be customized by subclassing CommonMiddleware and
overriding the response_redirect_class attribute.
- ETags: If the USE_ETAGS setting is set, ETags will be calculated from - ETags: If the USE_ETAGS setting is set, ETags will be calculated from
the entire page content and Not Modified responses will be returned the entire page content and Not Modified responses will be returned
appropriately. appropriately.
""" """
response_redirect_class = http.HttpResponsePermanentRedirect
def process_request(self, request): def process_request(self, request):
""" """
Check for denied User-Agents and rewrite the URL based on Check for denied User-Agents and rewrite the URL based on
...@@ -100,7 +105,7 @@ class CommonMiddleware(object): ...@@ -100,7 +105,7 @@ class CommonMiddleware(object):
newurl += '?' + request.META['QUERY_STRING'].decode() newurl += '?' + request.META['QUERY_STRING'].decode()
except UnicodeDecodeError: except UnicodeDecodeError:
pass pass
return http.HttpResponsePermanentRedirect(newurl) return self.response_redirect_class(newurl)
def process_response(self, request, response): def process_response(self, request, response):
""" """
......
...@@ -66,6 +66,14 @@ Adds a few conveniences for perfectionists: ...@@ -66,6 +66,14 @@ Adds a few conveniences for perfectionists:
for each request by MD5-hashing the page content, and it'll take care of for each request by MD5-hashing the page content, and it'll take care of
sending ``Not Modified`` responses, if appropriate. sending ``Not Modified`` responses, if appropriate.
.. attribute:: CommonMiddleware.response_redirect_class
.. versionadded:: 1.8
Defaults to :class:`~django.http.HttpResponsePermanentRedirect`. Subclass
``CommonMiddleware`` and override the attribute to customize the redirects
issued by the middleware.
.. class:: BrokenLinkEmailsMiddleware .. class:: BrokenLinkEmailsMiddleware
* Sends broken link notification emails to :setting:`MANAGERS` (see * Sends broken link notification emails to :setting:`MANAGERS` (see
......
...@@ -304,6 +304,13 @@ Management Commands ...@@ -304,6 +304,13 @@ Management Commands
:setting:`FIXTURE_DIRS` contains duplicates or a default fixture directory :setting:`FIXTURE_DIRS` contains duplicates or a default fixture directory
path (``app_name/fixtures``), an exception is raised. path (``app_name/fixtures``), an exception is raised.
Middleware
^^^^^^^^^^
* The :attr:`CommonMiddleware.response_redirect_class
<django.middleware.common.CommonMiddleware.response_redirect_class>`
attribute allows you to customize the redirects issued by the middleware.
Migrations Migrations
^^^^^^^^^^ ^^^^^^^^^^
......
...@@ -9,7 +9,10 @@ from unittest import skipIf ...@@ -9,7 +9,10 @@ from unittest import skipIf
from django.conf import settings from django.conf import settings
from django.core import mail from django.core import mail
from django.http import HttpRequest, HttpResponse, StreamingHttpResponse from django.http import (
HttpRequest, HttpResponse, StreamingHttpResponse, HttpResponsePermanentRedirect,
HttpResponseRedirect,
)
from django.middleware.clickjacking import XFrameOptionsMiddleware from django.middleware.clickjacking import XFrameOptionsMiddleware
from django.middleware.common import CommonMiddleware, BrokenLinkEmailsMiddleware from django.middleware.common import CommonMiddleware, BrokenLinkEmailsMiddleware
from django.middleware.http import ConditionalGetMiddleware from django.middleware.http import ConditionalGetMiddleware
...@@ -242,6 +245,23 @@ class CommonMiddlewareTest(TestCase): ...@@ -242,6 +245,23 @@ class CommonMiddlewareTest(TestCase):
response = CommonMiddleware().process_request(request) response = CommonMiddleware().process_request(request)
self.assertEqual(response.status_code, 301) self.assertEqual(response.status_code, 301)
def test_response_redirect_class(self):
request = self._get_request('slash')
r = CommonMiddleware().process_request(request)
self.assertEqual(r.status_code, 301)
self.assertEqual(r.url, 'http://testserver/slash/')
self.assertIsInstance(r, HttpResponsePermanentRedirect)
def test_response_redirect_class_subclass(self):
class MyCommonMiddleware(CommonMiddleware):
response_redirect_class = HttpResponseRedirect
request = self._get_request('slash')
r = MyCommonMiddleware().process_request(request)
self.assertEqual(r.status_code, 302)
self.assertEqual(r.url, 'http://testserver/slash/')
self.assertIsInstance(r, HttpResponseRedirect)
@override_settings( @override_settings(
IGNORABLE_404_URLS=(re.compile(r'foo'),), IGNORABLE_404_URLS=(re.compile(r'foo'),),
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment