mirror of
https://github.com/Hopiu/wagtail.git
synced 2026-05-01 12:04:47 +00:00
try unencoding url if redirect not found
This commit is contained in:
parent
240fa9153b
commit
0f1ad6c6bf
2 changed files with 42 additions and 1 deletions
|
|
@ -2,6 +2,7 @@ from __future__ import absolute_import, unicode_literals
|
|||
|
||||
import django
|
||||
from django import http
|
||||
from django.utils.encoding import uri_to_iri
|
||||
from django.utils.six.moves.urllib.parse import urlparse
|
||||
|
||||
from wagtail.wagtailredirects import models
|
||||
|
|
@ -13,7 +14,7 @@ else:
|
|||
MiddlewareMixin = object
|
||||
|
||||
|
||||
def get_redirect(request, path):
|
||||
def _get_redirect(request, path):
|
||||
try:
|
||||
return models.Redirect.get_for_site(request.site).get(old_path=path)
|
||||
except models.Redirect.MultipleObjectsReturned:
|
||||
|
|
@ -23,6 +24,14 @@ def get_redirect(request, path):
|
|||
return None
|
||||
|
||||
|
||||
def get_redirect(request, path):
|
||||
redirect = _get_redirect(request, path)
|
||||
if not redirect:
|
||||
# try unencoding the path
|
||||
redirect = _get_redirect(request, uri_to_iri(path))
|
||||
return redirect
|
||||
|
||||
|
||||
# Originally pinched from: https://github.com/django/django/blob/master/django/contrib/redirects/middleware.py
|
||||
class RedirectMiddleware(MiddlewareMixin):
|
||||
def process_response(self, request, response):
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
from __future__ import absolute_import, unicode_literals
|
||||
|
||||
from django.core.urlresolvers import reverse
|
||||
|
|
@ -85,6 +86,19 @@ class TestRedirects(TestCase):
|
|||
normalise_path('!#@%$*')
|
||||
normalise_path('C:\\Program Files (x86)\\Some random program\\file.txt')
|
||||
|
||||
def test_unicode_path_normalisation(self):
|
||||
normalise_path = models.Redirect.normalise_path
|
||||
|
||||
self.assertEqual(
|
||||
'/here/tésting-ünicode', # stays the same
|
||||
normalise_path('/here/tésting-ünicode')
|
||||
)
|
||||
|
||||
self.assertNotEqual( # Doesn't remove unicode characters
|
||||
'/here/testing-unicode',
|
||||
normalise_path('/here/tésting-ünicode')
|
||||
)
|
||||
|
||||
def test_basic_redirect(self):
|
||||
# Create a redirect
|
||||
redirect = models.Redirect(old_path='/redirectme', redirect_link='/redirectto')
|
||||
|
|
@ -241,6 +255,24 @@ class TestRedirects(TestCase):
|
|||
response = self.client.get('/xmas/', HTTP_HOST='other.example.com')
|
||||
self.assertRedirects(response, 'http://localhost/events/christmas/', status_code=301, fetch_redirect_response=False)
|
||||
|
||||
def test_redirect_with_unicode_in_url(self):
|
||||
redirect = models.Redirect(old_path='/tésting-ünicode', redirect_link='/redirectto')
|
||||
redirect.save()
|
||||
|
||||
# Navigate to it
|
||||
response = self.client.get('/tésting-ünicode/')
|
||||
|
||||
self.assertRedirects(response, '/redirectto', status_code=301, fetch_redirect_response=False)
|
||||
|
||||
def test_redirect_with_encoded_url(self):
|
||||
redirect = models.Redirect(old_path='/t%C3%A9sting-%C3%BCnicode', redirect_link='/redirectto')
|
||||
redirect.save()
|
||||
|
||||
# Navigate to it
|
||||
response = self.client.get('/t%C3%A9sting-%C3%BCnicode/')
|
||||
|
||||
self.assertRedirects(response, '/redirectto', status_code=301, fetch_redirect_response=False)
|
||||
|
||||
|
||||
class TestRedirectsIndexView(TestCase, WagtailTestUtils):
|
||||
def setUp(self):
|
||||
|
|
|
|||
Loading…
Reference in a new issue