Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixed #16727 -- Added protocol-relative URL support to contenttypes.v…

…iews.shortcut.
  • Loading branch information...
commit 53c576452e2e8feef857cd67a5c17f9159d95eb6 1 parent 7bbb695
@tcsorrel tcsorrel authored bmispelon committed
View
2  django/contrib/contenttypes/views.py
@@ -34,7 +34,7 @@ def shortcut(request, content_type_id, object_id):
# if necessary.
# 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)
# Otherwise, we need to introspect the object's relationships for a
View
3  docs/releases/1.7.txt
@@ -1147,6 +1147,9 @@ Miscellaneous
``django.utils.translation.trans_real.get_supported_language_variant()``
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:
Features deprecated in 1.7
View
21 tests/contenttypes_tests/fixtures/testdata.json
@@ -38,6 +38,27 @@
},
{
"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,
"model": "sites.site",
"fields": {
"domain": "testserver",
View
11 tests/contenttypes_tests/models.py
@@ -24,3 +24,14 @@ class Article(models.Model):
def __str__(self):
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
15 tests/contenttypes_tests/tests.py
@@ -12,7 +12,7 @@
from django.test.utils import override_settings
from django.utils.encoding import force_str
-from .models import Author, Article
+from .models import Author, Article, SchemeIncludedURL
class ContentTypesViewsTests(TestCase):
@@ -27,6 +27,19 @@ def test_shortcut_with_absolute_url(self):
self.assertRedirects(response, 'http://testserver%s' % obj.get_absolute_url(),
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):
"Shortcuts for an object that has no get_absolute_url method raises 404"
for obj in Article.objects.all():
Please sign in to comment.
Something went wrong with that request. Please try again.