Ticket #10927: shortcut.diff

File shortcut.diff, 2.8 KB (added by Alex Gaynor, 15 years ago)

simpler :)

  • django/contrib/contenttypes/views.py

    diff --git a/django/contrib/contenttypes/views.py b/django/contrib/contenttypes/views.py
    index 4285be3..2696120 100644
    a b def shortcut(request, content_type_id, object_id):  
    99    try:
    1010        content_type = ContentType.objects.get(pk=content_type_id)
    1111        obj = content_type.get_object_for_this_type(pk=object_id)
    12     except ObjectDoesNotExist:
     12    except (ObjectDoesNotExist, ValueError):
    1313        raise http.Http404("Content type %s object %s doesn't exist" % (content_type_id, object_id))
    1414    try:
    1515        absurl = obj.get_absolute_url()
  • 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):  
    1010    """Test django views in django/views/defaults.py"""
    1111    fixtures = ['testdata.json']
    1212
    13     def test_shorcut_with_absolute_url(self):
     13    def test_shortcut_with_absolute_url(self):
    1414        "Can view a shortcut an Author object that has with a get_absolute_url method"
    1515        for obj in Author.objects.all():
    1616            short_url = '/views/shortcut/%s/%s/' % (ContentType.objects.get_for_model(Author).id, obj.pk)
    class DefaultsTests(TestCase):  
    2525            response = self.client.get(short_url)
    2626            self.assertEquals(response.status_code, 404)
    2727
     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
    2852    def test_page_not_found(self):
    2953        "A 404 status is returned by the page_not_found view"
    3054        non_existing_urls = ['/views/non_existing_url/', # this is in urls.py
Back to Top