1
0
mirror of https://github.com/django/django.git synced 2025-07-04 09:49:12 +00:00

[1.1.X] Fixed #11891 -- Ensured that attributes of get_absolute_url are preserved through the metaclass. Thanks to nfg for the report and patch.

Backport of r12766 from trunk.

git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.1.X@12767 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Russell Keith-Magee 2010-03-12 14:45:56 +00:00
parent 1e1b57bfbe
commit d1b889ec98
3 changed files with 14 additions and 4 deletions

View File

@ -18,7 +18,7 @@ from django.db import connection, transaction, DatabaseError
from django.db.models import signals from django.db.models import signals
from django.db.models.loading import register_models, get_model from django.db.models.loading import register_models, get_model
import django.utils.copycompat as copy 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.encoding import smart_str, force_unicode, smart_unicode
from django.conf import settings from django.conf import settings
@ -236,7 +236,8 @@ class ModelBase(type):
cls.__doc__ = "%s(%s)" % (cls.__name__, ", ".join([f.attname for f in opts.fields])) cls.__doc__ = "%s(%s)" % (cls.__name__, ", ".join([f.attname for f in opts.fields]))
if hasattr(cls, 'get_absolute_url'): 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) signals.class_prepared.send(sender=cls)

View File

@ -39,6 +39,7 @@ class UrlArticle(BaseArticle):
def get_absolute_url(self): def get_absolute_url(self):
return '/urlarticles/%s/' % self.slug return '/urlarticles/%s/' % self.slug
get_absolute_url.purge = True
class DateArticle(BaseArticle): class DateArticle(BaseArticle):
""" """

View File

@ -4,7 +4,7 @@ from django.conf import settings
from django.test import TestCase from django.test import TestCase
from django.contrib.contenttypes.models import ContentType 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): class DefaultsTests(TestCase):
"""Test django views in django/views/defaults.py""" """Test django views in django/views/defaults.py"""
@ -15,7 +15,7 @@ class DefaultsTests(TestCase):
for obj in Author.objects.all(): for obj in Author.objects.all():
short_url = '/views/shortcut/%s/%s/' % (ContentType.objects.get_for_model(Author).id, obj.pk) short_url = '/views/shortcut/%s/%s/' % (ContentType.objects.get_for_model(Author).id, obj.pk)
response = self.client.get(short_url) 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) status_code=302, target_status_code=404)
def test_shortcut_no_absolute_url(self): def test_shortcut_no_absolute_url(self):
@ -59,3 +59,11 @@ class DefaultsTests(TestCase):
"The server_error view raises a 500 status" "The server_error view raises a 500 status"
response = self.client.get('/views/server_error/') response = self.client.get('/views/server_error/')
self.assertEquals(response.status_code, 500) 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.')