Ticket #11891: get_absolute_url_attributes.diff

File get_absolute_url_attributes.diff, 2.7 KB (added by Nils Fredrik Gjerull, 15 years ago)

Preserve attributes of the original get_absolute_url method

  • django/db/models/base.py

     
    1818from django.db import connection, transaction, DatabaseError
    1919from django.db.models import signals
    2020from django.db.models.loading import register_models, get_model
    21 from django.utils.functional import curry
     21from django.utils.functional import curry, update_wrapper
    2222from django.utils.encoding import smart_str, force_unicode, smart_unicode
    2323from django.conf import settings
    2424
     
    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

     
    3939
    4040    def get_absolute_url(self):
    4141        return '/urlarticles/%s/' % self.slug
     42    get_absolute_url.purge = True
    4243
    4344class DateArticle(BaseArticle):
    4445    """
  • tests/regressiontests/views/tests/defaults.py

     
    44from django.test import TestCase
    55from django.contrib.contenttypes.models import ContentType
    66
    7 from regressiontests.views.models import Author, Article
     7from regressiontests.views.models import Author, Article, UrlArticle
    88
    99class DefaultsTests(TestCase):
    1010    """Test django views in django/views/defaults.py"""
     
    3737        "The server_error view raises a 500 status"
    3838        response = self.client.get('/views/server_error/')
    3939        self.assertEquals(response.status_code, 500)
     40
     41    def test_get_absolute_url_attributes(self):
     42        "A model can set attributes on the get_absolute_url method"
     43        self.assertTrue(getattr(UrlArticle.get_absolute_url, 'purge', False),
     44                        'The attributes of the original get_absolute_url must be added.')
     45        article = UrlArticle.objects.get(pk=1)
     46        self.assertTrue(getattr(article.get_absolute_url, 'purge', False),
     47                        'The attributes of the original get_absolute_url must be added.')
Back to Top