From e1643e3535c4160c1a56c80f4d4db9848dc65e7a Mon Sep 17 00:00:00 2001 From: Florian Apolloner Date: Thu, 7 Jun 2012 17:34:25 +0200 Subject: [PATCH] Don't escape object ids when passing to the contenttypes.shortcut view. This commit also changes the string pk to string_pk instead of id, to test if the admin uses .pk throughout the codebase. --- .../contrib/admin/templates/admin/change_form.html | 2 +- .../admin_views/fixtures/string-primary-key.xml | 6 +++--- tests/regressiontests/admin_views/models.py | 7 +++++-- tests/regressiontests/admin_views/tests.py | 14 +++++++++++--- 4 files changed, 20 insertions(+), 9 deletions(-) diff --git a/django/contrib/admin/templates/admin/change_form.html b/django/contrib/admin/templates/admin/change_form.html index 79c4f1bdf43..ead37b187fd 100644 --- a/django/contrib/admin/templates/admin/change_form.html +++ b/django/contrib/admin/templates/admin/change_form.html @@ -31,7 +31,7 @@ {% endif %}{% endif %} diff --git a/tests/regressiontests/admin_views/fixtures/string-primary-key.xml b/tests/regressiontests/admin_views/fixtures/string-primary-key.xml index 8e1dbf047fb..1792cb2d7e5 100644 --- a/tests/regressiontests/admin_views/fixtures/string-primary-key.xml +++ b/tests/regressiontests/admin_views/fixtures/string-primary-key.xml @@ -1,6 +1,6 @@ - #%" {}|\^[]`]]> - - \ No newline at end of file + #%" {}|\^[]`]]> + + diff --git a/tests/regressiontests/admin_views/models.py b/tests/regressiontests/admin_views/models.py index 17533f9f800..2da86e20999 100644 --- a/tests/regressiontests/admin_views/models.py +++ b/tests/regressiontests/admin_views/models.py @@ -93,10 +93,13 @@ class CustomArticle(models.Model): class ModelWithStringPrimaryKey(models.Model): - id = models.CharField(max_length=255, primary_key=True) + string_pk = models.CharField(max_length=255, primary_key=True) def __unicode__(self): - return self.id + return self.string_pk + + def get_absolute_url(self): + return u'/dummy/%s/' % self.string_pk class Color(models.Model): diff --git a/tests/regressiontests/admin_views/tests.py b/tests/regressiontests/admin_views/tests.py index f9a9b151145..e8d05f78ff4 100644 --- a/tests/regressiontests/admin_views/tests.py +++ b/tests/regressiontests/admin_views/tests.py @@ -1403,7 +1403,7 @@ class AdminViewStringPrimaryKeyTest(TestCase): def test_url_conflicts_with_add(self): "A model with a primary key that ends with add should be visible" - add_model = ModelWithStringPrimaryKey(id="i have something to add") + add_model = ModelWithStringPrimaryKey(pk="i have something to add") add_model.save() response = self.client.get('/test_admin/admin/admin_views/modelwithstringprimarykey/%s/' % quote(add_model.pk)) should_contain = """

Change model with string primary key

""" @@ -1411,7 +1411,7 @@ class AdminViewStringPrimaryKeyTest(TestCase): def test_url_conflicts_with_delete(self): "A model with a primary key that ends with delete should be visible" - delete_model = ModelWithStringPrimaryKey(id="delete") + delete_model = ModelWithStringPrimaryKey(pk="delete") delete_model.save() response = self.client.get('/test_admin/admin/admin_views/modelwithstringprimarykey/%s/' % quote(delete_model.pk)) should_contain = """

Change model with string primary key

""" @@ -1419,12 +1419,20 @@ class AdminViewStringPrimaryKeyTest(TestCase): def test_url_conflicts_with_history(self): "A model with a primary key that ends with history should be visible" - history_model = ModelWithStringPrimaryKey(id="history") + history_model = ModelWithStringPrimaryKey(pk="history") history_model.save() response = self.client.get('/test_admin/admin/admin_views/modelwithstringprimarykey/%s/' % quote(history_model.pk)) should_contain = """

Change model with string primary key

""" self.assertContains(response, should_contain) + def test_shortcut_view_with_escaping(self): + "'View on site should' work properly with char fields" + model = ModelWithStringPrimaryKey(pk='abc_123') + model.save() + response = self.client.get('/test_admin/admin/admin_views/modelwithstringprimarykey/%s/' % quote(model.pk)) + should_contain = '/%s/" class="viewsitelink">' % model.pk + self.assertContains(response, should_contain) + @override_settings(PASSWORD_HASHERS=('django.contrib.auth.hashers.SHA1PasswordHasher',)) class SecureViewTests(TestCase):