Ticket #8997: 8997-2.diff

File 8997-2.diff, 2.3 KB (added by Claude Paroz, 13 years ago)

Patch following aaugustin example

  • django/contrib/contenttypes/tests.py

    diff --git a/django/contrib/contenttypes/tests.py b/django/contrib/contenttypes/tests.py
    index 1854664..36179c0 100644
    a b class FooWithUrl(FooWithoutUrl):  
    3030    def get_absolute_url(self):
    3131        return "/users/%s/" % urllib.quote(smart_str(self.name))
    3232
     33class FooWithBrokenAbsoluteUrl(FooWithoutUrl):
     34    """
     35    Fake model defining a ``get_absolute_url`` method containing an error
     36    """
     37
     38    def get_absolute_url(self):
     39        return "/users/%s/" % self.unknown_field
    3340
    3441class ContentTypesTests(TestCase):
    3542
    class ContentTypesTests(TestCase):  
    135142
    136143        self.assertRaises(Http404, shortcut, request, user_ct.id, obj.id)
    137144
     145    def test_shortcut_view_with_broken_get_absolute_url(self):
     146        """
     147        Check that the shortcut view does not catch an AttributeError raised
     148        by the model's get_absolute_url method.
     149        """
     150
     151        request = HttpRequest()
     152        request.META = {
     153            "SERVER_NAME": "Example.com",
     154            "SERVER_PORT": "80",
     155        }
     156        user_ct = ContentType.objects.get_for_model(FooWithBrokenAbsoluteUrl)
     157        obj = FooWithBrokenAbsoluteUrl.objects.create(name="john")
     158
     159        self.assertRaises(AttributeError, shortcut, request, user_ct.id, obj.id)
     160
    138161    def test_missing_model(self):
    139162        """
    140163        Ensures that displaying content types in admin (or anywhere) doesn't
  • django/contrib/contenttypes/views.py

    diff --git a/django/contrib/contenttypes/views.py b/django/contrib/contenttypes/views.py
    index ac0feff..e9802b0 100644
    a b def shortcut(request, content_type_id, object_id):  
    1414    except (ObjectDoesNotExist, ValueError):
    1515        raise http.Http404("Content type %s object %s doesn't exist" % (content_type_id, object_id))
    1616    try:
    17         absurl = obj.get_absolute_url()
     17        get_absolute_url = obj.get_absolute_url
    1818    except AttributeError:
    19         raise http.Http404("%s objects don't have get_absolute_url() methods" % content_type.name)
     19        raise http.Http404("%s objects don't have a get_absolute_url() method" % content_type.name)
     20    absurl = get_absolute_url()
    2021
    2122    # Try to figure out the object's domain, so we can do a cross-site redirect
    2223    # if necessary.
Back to Top