Refs #18974 -- Removed @models.permalink() decorator per deprecation timeline.

This commit is contained in:
Tim Graham 2017-09-02 20:47:47 -04:00
parent 5e31be1b96
commit 4502489a46
7 changed files with 3 additions and 98 deletions

View File

@ -30,36 +30,6 @@ from django.db.models.fields.related import ( # isort:skip
)
def permalink(func):
"""
Decorator that calls urls.reverse() to return a URL using parameters
returned by the decorated function "func".
"func" should be a function that returns a tuple in one of the
following formats:
(viewname, viewargs)
(viewname, viewargs, viewkwargs)
"""
import warnings
from functools import wraps
from django.urls import reverse
from django.utils.deprecation import RemovedInDjango21Warning
warnings.warn(
'permalink() is deprecated in favor of calling django.urls.reverse() '
'in the decorated method.',
RemovedInDjango21Warning,
stacklevel=2,
)
@wraps(func)
def inner(*args, **kwargs):
bits = func(*args, **kwargs)
return reverse(bits[0], None, *bits[1:3])
return inner
__all__ = aggregates_all + fields_all + indexes_all
__all__ += [
'ObjectDoesNotExist', 'signals',
@ -72,5 +42,5 @@ __all__ += [
'Prefetch', 'Q', 'QuerySet', 'prefetch_related_objects', 'DEFERRED', 'Model',
'FilteredRelation',
'ForeignKey', 'ForeignObject', 'OneToOneField', 'ManyToManyField',
'ManyToOneRel', 'ManyToManyRel', 'OneToOneRel', 'permalink',
'ManyToOneRel', 'ManyToManyRel', 'OneToOneRel',
]

View File

@ -249,3 +249,5 @@ how to remove usage of these features.
* The ``authenticate()`` method of authentication backends requires ``request``
as the first positional argument.
* The ``django.db.models.permalink()`` decorator is removed.

View File

@ -1,30 +0,0 @@
import warnings
from django.db import models
from django.utils.deprecation import RemovedInDjango21Warning
def set_attr(name, value):
def wrapper(function):
setattr(function, name, value)
return function
return wrapper
with warnings.catch_warnings():
warnings.simplefilter('ignore', category=RemovedInDjango21Warning)
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])
@models.permalink
@set_attr('attribute', 'value')
def url_with_attribute(self):
"Returns the URL for this guitarist and holds an attribute"
return ('guitarist_detail', [self.slug])

View File

@ -1,25 +0,0 @@
from django.test import SimpleTestCase, override_settings
from .models import Guitarist
@override_settings(ROOT_URLCONF='model_permalink.urls')
class PermalinkTests(SimpleTestCase):
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.")
def test_wrapped_attribute(self):
"""
Methods using the @permalink decorator can have attached attributes
from other decorators
"""
g = Guitarist(name='Adrien Moignard', slug='adrienmoignard')
self.assertTrue(hasattr(g.url_with_attribute, 'attribute'))
self.assertEqual(g.url_with_attribute.attribute, 'value')

View File

@ -1,7 +0,0 @@
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^guitarists/(\w{1,50})/$', views.empty_view, name='guitarist_detail'),
]

View File

@ -1,5 +0,0 @@
from django.http import HttpResponse
def empty_view(request, *args, **kwargs):
return HttpResponse('')