Code

Ticket #11891: get_absolute_url_attributes.diff

File get_absolute_url_attributes.diff, 2.7 KB (added by nfg, 5 years ago)

Preserve attributes of the original get_absolute_url method

Line 
1Index: django/db/models/base.py
2===================================================================
3--- django/db/models/base.py    (revisjon 11585)
4+++ django/db/models/base.py    (arbeidskopi)
5@@ -18,7 +18,7 @@
6 from django.db import connection, transaction, DatabaseError
7 from django.db.models import signals
8 from django.db.models.loading import register_models, get_model
9-from django.utils.functional import curry
10+from django.utils.functional import curry, update_wrapper
11 from django.utils.encoding import smart_str, force_unicode, smart_unicode
12 from django.conf import settings
13 
14@@ -232,7 +232,8 @@
15             cls.__doc__ = "%s(%s)" % (cls.__name__, ", ".join([f.attname for f in opts.fields]))
16 
17         if hasattr(cls, 'get_absolute_url'):
18-            cls.get_absolute_url = curry(get_absolute_url, opts, cls.get_absolute_url)
19+            cls.get_absolute_url = update_wrapper(curry(get_absolute_url, opts, cls.get_absolute_url),
20+                                                  cls.get_absolute_url)
21 
22         signals.class_prepared.send(sender=cls)
23 
24Index: tests/regressiontests/views/models.py
25===================================================================
26--- tests/regressiontests/views/models.py       (revisjon 11585)
27+++ tests/regressiontests/views/models.py       (arbeidskopi)
28@@ -39,6 +39,7 @@
29 
30     def get_absolute_url(self):
31         return '/urlarticles/%s/' % self.slug
32+    get_absolute_url.purge = True
33 
34 class DateArticle(BaseArticle):
35     """
36Index: tests/regressiontests/views/tests/defaults.py
37===================================================================
38--- tests/regressiontests/views/tests/defaults.py       (revisjon 11585)
39+++ tests/regressiontests/views/tests/defaults.py       (arbeidskopi)
40@@ -4,7 +4,7 @@
41 from django.test import TestCase
42 from django.contrib.contenttypes.models import ContentType
43 
44-from regressiontests.views.models import Author, Article
45+from regressiontests.views.models import Author, Article, UrlArticle
46 
47 class DefaultsTests(TestCase):
48     """Test django views in django/views/defaults.py"""
49@@ -37,3 +37,11 @@
50         "The server_error view raises a 500 status"
51         response = self.client.get('/views/server_error/')
52         self.assertEquals(response.status_code, 500)
53+
54+    def test_get_absolute_url_attributes(self):
55+        "A model can set attributes on the get_absolute_url method"
56+        self.assertTrue(getattr(UrlArticle.get_absolute_url, 'purge', False),
57+                        'The attributes of the original get_absolute_url must be added.')
58+        article = UrlArticle.objects.get(pk=1)
59+        self.assertTrue(getattr(article.get_absolute_url, 'purge', False),
60+                        'The attributes of the original get_absolute_url must be added.')