Ticket #10927: 10927.diff
File 10927.diff, 3.7 KB (added by , 16 years ago) |
---|
-
django/contrib/contenttypes/views.py
diff --git a/django/contrib/contenttypes/views.py b/django/contrib/contenttypes/views.py index 4285be3..07800cb 100644
a b 1 1 from django import http 2 2 from django.contrib.contenttypes.models import ContentType 3 3 from django.contrib.sites.models import Site 4 from django.core.exceptions import ObjectDoesNotExist 4 from django.core.exceptions import ObjectDoesNotExist, ValidationError 5 5 6 6 def shortcut(request, content_type_id, object_id): 7 7 "Redirect to an object's page based on a content-type ID and an object ID." 8 8 # Look up the object, making sure it's got a get_absolute_url() function. 9 def raise_invalid(): 10 raise http.Http404("Content type %s object %s doesn't exist" % (content_type_id, object_id)) 11 try: 12 content_type_id = int(content_type_id) 13 except ValueError: 14 raise_invalid() 9 15 try: 10 16 content_type = ContentType.objects.get(pk=content_type_id) 17 except ObjectDoesNotExist: 18 raise_invalid() 19 try: 20 object_id = content_type.model_class()._meta.pk.to_python(object_id) 21 except ValidationError: 22 raise_invalid() 23 try: 11 24 obj = content_type.get_object_for_this_type(pk=object_id) 12 25 except ObjectDoesNotExist: 13 raise http.Http404("Content type %s object %s doesn't exist" % (content_type_id, object_id))26 raise_invalid() 14 27 try: 15 28 absurl = obj.get_absolute_url() 16 29 except AttributeError: -
tests/regressiontests/views/tests/defaults.py
diff --git a/tests/regressiontests/views/tests/defaults.py b/tests/regressiontests/views/tests/defaults.py index bf490d7..bef80e4 100644
a b class DefaultsTests(TestCase): 10 10 """Test django views in django/views/defaults.py""" 11 11 fixtures = ['testdata.json'] 12 12 13 def test_shor cut_with_absolute_url(self):13 def test_shortcut_with_absolute_url(self): 14 14 "Can view a shortcut an Author object that has with a get_absolute_url method" 15 15 for obj in Author.objects.all(): 16 16 short_url = '/views/shortcut/%s/%s/' % (ContentType.objects.get_for_model(Author).id, obj.pk) … … class DefaultsTests(TestCase): 25 25 response = self.client.get(short_url) 26 26 self.assertEquals(response.status_code, 404) 27 27 28 def test_wrong_type_pk(self): 29 an_article = Article.objects.all()[0] 30 short_url = '/views/shortcut/%s/%s/' % (ContentType.objects.get_for_model(Article).id, 'nobody/expects') 31 response = self.client.get(short_url) 32 self.assertEquals(response.status_code, 404) 33 34 def test_shortcut_bad_pk(self): 35 "Shortcuts for an object that has with a get_absolute_url method raises 404" 36 short_url = '/views/shortcut/%s/%s/' % (ContentType.objects.get_for_model(Article).id, '4242424242') 37 response = self.client.get(short_url) 38 self.assertEquals(response.status_code, 404) 39 40 def test_nonint_content_type(self): 41 an_article = Article.objects.all()[0] 42 short_url = '/views/shortcut/%s/%s/' % ('spam', an_article.pk) 43 response = self.client.get(short_url) 44 self.assertEquals(response.status_code, 404) 45 46 def test_bad_content_type(self): 47 an_article = Article.objects.all()[0] 48 short_url = '/views/shortcut/%s/%s/' % (4242424242, an_article.pk) 49 response = self.client.get(short_url) 50 self.assertEquals(response.status_code, 404) 51 28 52 def test_page_not_found(self): 29 53 "A 404 status is returned by the page_not_found view" 30 54 non_existing_urls = ['/views/non_existing_url/', # this is in urls.py