From 72c5733869c38897f31e82cbae3c6a24f7292d11 Mon Sep 17 00:00:00 2001 From: Adrian Holovaty Date: Mon, 14 Mar 2011 05:22:39 +0000 Subject: [PATCH] Fixed #15604 -- Changed django.db.models.permalink to use wraps() so that it doesn't eat the docstring. Thanks for the report, sfllaw. Also added tests. git-svn-id: http://code.djangoproject.com/svn/django/trunk@15798 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/db/models/__init__.py | 2 ++ tests/regressiontests/model_permalink/__init__.py | 0 tests/regressiontests/model_permalink/models.py | 10 ++++++++++ tests/regressiontests/model_permalink/tests.py | 14 ++++++++++++++ tests/regressiontests/model_permalink/urls.py | 5 +++++ 5 files changed, 31 insertions(+) create mode 100644 tests/regressiontests/model_permalink/__init__.py create mode 100644 tests/regressiontests/model_permalink/models.py create mode 100644 tests/regressiontests/model_permalink/tests.py create mode 100644 tests/regressiontests/model_permalink/urls.py diff --git a/django/db/models/__init__.py b/django/db/models/__init__.py index 9709976b19..3582720e55 100644 --- a/django/db/models/__init__.py +++ b/django/db/models/__init__.py @@ -13,6 +13,7 @@ from django.db.models.fields.files import FileField, ImageField from django.db.models.fields.related import ForeignKey, OneToOneField, ManyToManyField, ManyToOneRel, ManyToManyRel, OneToOneRel from django.db.models.deletion import CASCADE, PROTECT, SET, SET_NULL, SET_DEFAULT, DO_NOTHING, ProtectedError from django.db.models import signals +from django.utils.decorators import wraps # Admin stages. ADD, CHANGE, BOTH = 1, 2, 3 @@ -28,6 +29,7 @@ def permalink(func): (viewname, viewargs, viewkwargs) """ from django.core.urlresolvers import reverse + @wraps(func) def inner(*args, **kwargs): bits = func(*args, **kwargs) return reverse(bits[0], None, *bits[1:3]) diff --git a/tests/regressiontests/model_permalink/__init__.py b/tests/regressiontests/model_permalink/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/regressiontests/model_permalink/models.py b/tests/regressiontests/model_permalink/models.py new file mode 100644 index 0000000000..b2b3f704e9 --- /dev/null +++ b/tests/regressiontests/model_permalink/models.py @@ -0,0 +1,10 @@ +from django.db import models + +class Guitarist(models.Model): + name = models.CharField(max_length=50) + slug = models.CharField(max_length=50) + + @models.permalink + def url(self): + "Returns the URL for this guitarist." + return ('guitarist_detail', [self.slug]) diff --git a/tests/regressiontests/model_permalink/tests.py b/tests/regressiontests/model_permalink/tests.py new file mode 100644 index 0000000000..40b6cd0292 --- /dev/null +++ b/tests/regressiontests/model_permalink/tests.py @@ -0,0 +1,14 @@ +from django.test import TestCase +from regressiontests.model_permalink.models import Guitarist + +class PermalinkTests(TestCase): + urls = 'regressiontests.model_permalink.urls' + + def test_permalink(self): + g = Guitarist(name='Adrien Moignard', slug='adrienmoignard') + self.assertEqual(g.url(), '/guitarists/adrienmoignard/') + + def test_wrapped_docstring(self): + "Methods using the @permalink decorator retain their docstring." + g = Guitarist(name='Adrien Moignard', slug='adrienmoignard') + self.assertEqual(g.url.__doc__, "Returns the URL for this guitarist.") diff --git a/tests/regressiontests/model_permalink/urls.py b/tests/regressiontests/model_permalink/urls.py new file mode 100644 index 0000000000..6a84117a9b --- /dev/null +++ b/tests/regressiontests/model_permalink/urls.py @@ -0,0 +1,5 @@ +from django.conf.urls.defaults import * + +urlpatterns = patterns('', + url(r'^guitarists/(\w{1,50})/$', 'unimplemented_view_placeholder', name='guitarist_detail'), +)