Ticket #8997: get_absolute_url_patch.diff

File get_absolute_url_patch.diff, 2.8 KB (added by joshlory, 14 years ago)

Patch to allow AttributeError to propagate + tests

  • django/contrib/contenttypes/views.py

     
    1313        obj = content_type.get_object_for_this_type(pk=object_id)
    1414    except (ObjectDoesNotExist, ValueError):
    1515        raise http.Http404("Content type %s object %s doesn't exist" % (content_type_id, object_id))
    16     try:
    17         absurl = obj.get_absolute_url()
    18     except AttributeError:
     16    if not hasattr(obj, 'get_absolute_url'):
    1917        raise http.Http404("%s objects don't have get_absolute_url() methods" % content_type.name)
     18    absurl = obj.get_absolute_url()
    2019
    2120    # Try to figure out the object's domain, so we can do a cross-site redirect
    2221    # if necessary.
  • tests/regressiontests/contenttypes_regress/tests.py

     
     1from django import db
     2from django.conf import settings
     3from django.contrib.contenttypes.models import ContentType
     4from django.contrib.contenttypes.views import shortcut
     5from django.http import HttpRequest
     6from django.test import TestCase
     7
     8
     9class ContentTypesRegression(TestCase):
     10
     11    def setUp(self):
     12        self.old_DEBUG = settings.DEBUG
     13        settings.DEBUG = True
     14        ContentType.objects.clear_cache()
     15        db.reset_queries()
     16
     17    def tearDown(self):
     18        settings.DEBUG = self.old_DEBUG
     19
     20    def test_shortcut_attribute_error(self):
     21        """
     22        Check that the shortcut view (used for the admin "view on site"
     23        functionality) does not catch an AttributeError raised by the model's
     24        get_absolute_url method
     25        """
     26       
     27        request = HttpRequest()
     28        request.META = {
     29            "SERVER_NAME": "Example.com",
     30            "SERVER_PORT": "80",
     31        }
     32        from django.contrib.auth.models import Group
     33        Group.get_absolute_url = lambda x: x.invalid_field
     34        user_ct = ContentType.objects.get_for_model(Group)
     35        obj = Group.objects.create(name="staff")
     36        with self.assertRaises(AttributeError):
     37            shortcut(request, user_ct.id, obj.id)
  • tests/regressiontests/contenttypes_regress/models.py

     
     1# models.py file for tests to run.
Back to Top