Fixed #16727 -- Added protocol-relative URL support to contenttypes.views.shortcut.

This commit is contained in:
Thomas Sorrel 2014-03-02 21:46:51 +01:00 committed by Baptiste Mispelon
parent 7bbb6958dc
commit 53c576452e
5 changed files with 50 additions and 2 deletions

View File

@ -34,7 +34,7 @@ def shortcut(request, content_type_id, object_id):
# if necessary. # if necessary.
# If the object actually defines a domain, we're done. # If the object actually defines a domain, we're done.
if absurl.startswith('http://') or absurl.startswith('https://'): if absurl.startswith(('http://', 'https://', '//')):
return http.HttpResponseRedirect(absurl) return http.HttpResponseRedirect(absurl)
# Otherwise, we need to introspect the object's relationships for a # Otherwise, we need to introspect the object's relationships for a

View File

@ -1147,6 +1147,9 @@ Miscellaneous
``django.utils.translation.trans_real.get_supported_language_variant()`` ``django.utils.translation.trans_real.get_supported_language_variant()``
now no longer have a ``supported`` argument. now no longer have a ``supported`` argument.
* The ``shortcut`` view in ``django.contrib.contenttypes.views`` now supports
protocol-relative URLs (e.g. ``//example.com``).
.. _deprecated-features-1.7: .. _deprecated-features-1.7:
Features deprecated in 1.7 Features deprecated in 1.7

View File

@ -36,6 +36,27 @@
"date_created": "3000-01-01 21:22:23" "date_created": "3000-01-01 21:22:23"
} }
}, },
{
"pk": 1,
"model": "contenttypes_tests.schemeincludedurl",
"fields": {
"url": "http://test_scheme_included_http/"
}
},
{
"pk": 2,
"model": "contenttypes_tests.schemeincludedurl",
"fields": {
"url": "https://test_scheme_included_https/"
}
},
{
"pk": 3,
"model": "contenttypes_tests.schemeincludedurl",
"fields": {
"url": "//test_default_scheme_kept/"
}
},
{ {
"pk": 1, "pk": 1,
"model": "sites.site", "model": "sites.site",

View File

@ -24,3 +24,14 @@ class Article(models.Model):
def __str__(self): def __str__(self):
return self.title return self.title
@python_2_unicode_compatible
class SchemeIncludedURL(models.Model):
url = models.URLField(max_length=100)
def __str__(self):
return self.url
def get_absolute_url(self):
return self.url

View File

@ -12,7 +12,7 @@ from django.test import TestCase
from django.test.utils import override_settings from django.test.utils import override_settings
from django.utils.encoding import force_str from django.utils.encoding import force_str
from .models import Author, Article from .models import Author, Article, SchemeIncludedURL
class ContentTypesViewsTests(TestCase): class ContentTypesViewsTests(TestCase):
@ -27,6 +27,19 @@ class ContentTypesViewsTests(TestCase):
self.assertRedirects(response, 'http://testserver%s' % obj.get_absolute_url(), self.assertRedirects(response, 'http://testserver%s' % obj.get_absolute_url(),
status_code=302, target_status_code=404) status_code=302, target_status_code=404)
def test_shortcut_with_absolute_url_including_scheme(self):
"""
Can view a shortcut when object's get_absolute_url returns a full URL
the tested URLs are in fixtures/testdata.json :
"http://...", "https://..." and "//..."
"""
for obj in SchemeIncludedURL.objects.all():
short_url = '/shortcut/%s/%s/' % (ContentType.objects.get_for_model(SchemeIncludedURL).id, obj.pk)
response = self.client.get(short_url)
self.assertRedirects(response, obj.get_absolute_url(),
status_code=302,
fetch_redirect_response=False)
def test_shortcut_no_absolute_url(self): def test_shortcut_no_absolute_url(self):
"Shortcuts for an object that has no get_absolute_url method raises 404" "Shortcuts for an object that has no get_absolute_url method raises 404"
for obj in Article.objects.all(): for obj in Article.objects.all():