Skip to content

Commit

Permalink
Fixed #11891 -- Ensured that attributes of get_absolute_url are prese…
Browse files Browse the repository at this point in the history
…rved through the metaclass. Thanks to nfg for the report and patch.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@12766 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information
freakboy3742 committed Mar 12, 2010
1 parent ef0be29 commit 203d0f6
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
5 changes: 3 additions & 2 deletions django/db/models/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from django.db.models.loading import register_models, get_model
from django.utils.translation import ugettext_lazy as _
import django.utils.copycompat as copy
from django.utils.functional import curry
from django.utils.functional import curry, update_wrapper
from django.utils.encoding import smart_str, force_unicode, smart_unicode
from django.utils.text import get_text_list, capfirst
from django.conf import settings
Expand Down Expand Up @@ -232,7 +232,8 @@ def _prepare(cls):
cls.__doc__ = "%s(%s)" % (cls.__name__, ", ".join([f.attname for f in opts.fields]))

if hasattr(cls, 'get_absolute_url'):
cls.get_absolute_url = curry(get_absolute_url, opts, cls.get_absolute_url)
cls.get_absolute_url = update_wrapper(curry(get_absolute_url, opts, cls.get_absolute_url),
cls.get_absolute_url)

signals.class_prepared.send(sender=cls)

Expand Down
1 change: 1 addition & 0 deletions tests/regressiontests/views/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class UrlArticle(BaseArticle):

def get_absolute_url(self):
return '/urlarticles/%s/' % self.slug
get_absolute_url.purge = True

class DateArticle(BaseArticle):
"""
Expand Down
12 changes: 10 additions & 2 deletions tests/regressiontests/views/tests/defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from django.test import TestCase
from django.contrib.contenttypes.models import ContentType

from regressiontests.views.models import Author, Article
from regressiontests.views.models import Author, Article, UrlArticle

class DefaultsTests(TestCase):
"""Test django views in django/views/defaults.py"""
Expand All @@ -15,7 +15,7 @@ def test_shortcut_with_absolute_url(self):
for obj in Author.objects.all():
short_url = '/views/shortcut/%s/%s/' % (ContentType.objects.get_for_model(Author).id, obj.pk)
response = self.client.get(short_url)
self.assertRedirects(response, 'http://testserver%s' % obj.get_absolute_url(),
self.assertRedirects(response, 'http://testserver%s' % obj.get_absolute_url(),
status_code=302, target_status_code=404)

def test_shortcut_no_absolute_url(self):
Expand Down Expand Up @@ -59,3 +59,11 @@ def test_server_error(self):
"The server_error view raises a 500 status"
response = self.client.get('/views/server_error/')
self.assertEquals(response.status_code, 500)

def test_get_absolute_url_attributes(self):
"A model can set attributes on the get_absolute_url method"
self.assertTrue(getattr(UrlArticle.get_absolute_url, 'purge', False),
'The attributes of the original get_absolute_url must be added.')
article = UrlArticle.objects.get(pk=1)
self.assertTrue(getattr(article.get_absolute_url, 'purge', False),
'The attributes of the original get_absolute_url must be added.')

0 comments on commit 203d0f6

Please sign in to comment.