Skip to content

Commit 203d0f6

Browse files
committed
Fixed #11891 -- Ensured that attributes of get_absolute_url are preserved 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
1 parent ef0be29 commit 203d0f6

File tree

3 files changed

+14
-4
lines changed

3 files changed

+14
-4
lines changed

django/db/models/base.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from django.db.models.loading import register_models, get_model
1616
from django.utils.translation import ugettext_lazy as _
1717
import django.utils.copycompat as copy
18-
from django.utils.functional import curry
18+
from django.utils.functional import curry, update_wrapper
1919
from django.utils.encoding import smart_str, force_unicode, smart_unicode
2020
from django.utils.text import get_text_list, capfirst
2121
from django.conf import settings
@@ -232,7 +232,8 @@ def _prepare(cls):
232232
cls.__doc__ = "%s(%s)" % (cls.__name__, ", ".join([f.attname for f in opts.fields]))
233233

234234
if hasattr(cls, 'get_absolute_url'):
235-
cls.get_absolute_url = curry(get_absolute_url, opts, cls.get_absolute_url)
235+
cls.get_absolute_url = update_wrapper(curry(get_absolute_url, opts, cls.get_absolute_url),
236+
cls.get_absolute_url)
236237

237238
signals.class_prepared.send(sender=cls)
238239

tests/regressiontests/views/models.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ class UrlArticle(BaseArticle):
3939

4040
def get_absolute_url(self):
4141
return '/urlarticles/%s/' % self.slug
42+
get_absolute_url.purge = True
4243

4344
class DateArticle(BaseArticle):
4445
"""

tests/regressiontests/views/tests/defaults.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from django.test import TestCase
55
from django.contrib.contenttypes.models import ContentType
66

7-
from regressiontests.views.models import Author, Article
7+
from regressiontests.views.models import Author, Article, UrlArticle
88

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

2121
def test_shortcut_no_absolute_url(self):
@@ -59,3 +59,11 @@ def test_server_error(self):
5959
"The server_error view raises a 500 status"
6060
response = self.client.get('/views/server_error/')
6161
self.assertEquals(response.status_code, 500)
62+
63+
def test_get_absolute_url_attributes(self):
64+
"A model can set attributes on the get_absolute_url method"
65+
self.assertTrue(getattr(UrlArticle.get_absolute_url, 'purge', False),
66+
'The attributes of the original get_absolute_url must be added.')
67+
article = UrlArticle.objects.get(pk=1)
68+
self.assertTrue(getattr(article.get_absolute_url, 'purge', False),
69+
'The attributes of the original get_absolute_url must be added.')

0 commit comments

Comments
 (0)