Refs #21221 -- Deprecated staticfiles and admin_static template tag libraries.

This commit is contained in:
Jon Dufresne 2018-01-21 13:38:52 -08:00 committed by Tim Graham
parent f0f383b635
commit 7d607127e3
9 changed files with 109 additions and 23 deletions

View File

@ -1,11 +1,16 @@
import warnings
from django.template import Library
from django.templatetags.static import static as _static
from django.utils.deprecation import RemovedInDjango30Warning
register = Library()
@register.simple_tag
def static(path):
# Backwards compatibility alias for django.templatetags.static.static().
# Deprecation should start in Django 2.0.
warnings.warn(
'{% load admin_static %} is deprecated in favor of {% load static %}.',
RemovedInDjango30Warning,
)
return _static(path)

View File

@ -1,19 +1,28 @@
import warnings
from django import template
from django.templatetags.static import (
do_static as _do_static, static as _static,
)
from django.utils.deprecation import RemovedInDjango30Warning
register = template.Library()
def static(path):
# Backwards compatibility alias for django.templatetags.static.static().
# Deprecation should start in Django 2.0.
warnings.warn(
'django.contrib.staticfiles.templatetags.static() is deprecated in '
'favor of django.templatetags.static.static().',
RemovedInDjango30Warning,
stacklevel=2,
)
return _static(path)
@register.tag('static')
def do_static(parser, token):
# Backwards compatibility alias for django.templatetags.static.do_static().
# Deprecation should start in Django 2.0.
warnings.warn(
'{% load staticfiles %} is deprecated in favor of {% load static %}.',
RemovedInDjango30Warning,
)
return _do_static(parser, token)

View File

@ -33,6 +33,11 @@ details on these changes.
* ``django.utils.http.cookie_date()`` will be removed.
* The ``staticfiles`` and ``admin_static`` template tag libraries will be
removed.
* ``django.contrib.staticfiles.templatetags.static()`` will be removed.
See the :ref:`Django 2.1 release notes <deprecated-features-2.1>` for more
details on these changes.

View File

@ -274,6 +274,12 @@ Miscellaneous
:func:`~django.utils.http.http_date`, which follows the format of the latest
RFC.
* ``{% load staticfiles %}`` and ``{% load admin_static %}`` are deprecated
in favor of ``{% load static %}``, which works the same.
* ``django.contrib.staticfiles.templatetags.static()`` is deprecated in favor
of ``django.templatetags.static.static()``.
.. _removed-features-2.1:
Features removed in 2.1

View File

@ -0,0 +1,30 @@
import warnings
from django.contrib.admin.templatetags.admin_static import static
from django.contrib.staticfiles.storage import staticfiles_storage
from django.test import SimpleTestCase
from django.utils.deprecation import RemovedInDjango30Warning
class AdminStaticDeprecationTests(SimpleTestCase):
def test(self):
"""
admin_static.static points to the collectstatic version
(as django.contrib.collectstatic is in INSTALLED_APPS).
"""
msg = (
'{% load admin_static %} is deprecated in favor of '
'{% load static %}.'
)
old_url = staticfiles_storage.base_url
staticfiles_storage.base_url = '/test/'
try:
with warnings.catch_warnings(record=True) as recorded:
warnings.simplefilter('always')
url = static('path')
self.assertEqual(url, '/test/path')
self.assertEqual(len(recorded), 1)
self.assertIs(recorded[0].category, RemovedInDjango30Warning)
self.assertEqual(str(recorded[0].message), msg)
finally:
staticfiles_storage.base_url = old_url

View File

@ -10,7 +10,6 @@ from django.contrib.admin import AdminSite, ModelAdmin
from django.contrib.admin.helpers import ACTION_CHECKBOX_NAME
from django.contrib.admin.models import ADDITION, DELETION, LogEntry
from django.contrib.admin.options import TO_FIELD_VAR
from django.contrib.admin.templatetags.admin_static import static
from django.contrib.admin.templatetags.admin_urls import add_preserved_filters
from django.contrib.admin.tests import AdminSeleniumTestCase
from django.contrib.admin.utils import quote
@ -18,7 +17,6 @@ from django.contrib.admin.views.main import IS_POPUP_VAR
from django.contrib.auth import REDIRECT_FIELD_NAME, get_permission_codename
from django.contrib.auth.models import Group, Permission, User
from django.contrib.contenttypes.models import ContentType
from django.contrib.staticfiles.storage import staticfiles_storage
from django.core import mail
from django.core.checks import Error
from django.core.files import temp as tempfile
@ -200,18 +198,6 @@ class AdminViewBasicTest(AdminViewBasicTestCase):
response = self.client.get(add_url[:-1])
self.assertRedirects(response, add_url, status_code=301)
def test_admin_static_template_tag(self):
"""
admin_static.static points to the collectstatic version
(as django.contrib.collectstatic is in INSTALLED_APPS).
"""
old_url = staticfiles_storage.base_url
staticfiles_storage.base_url = '/test/'
try:
self.assertEqual(static('path'), '/test/path')
finally:
staticfiles_storage.base_url = old_url
def test_basic_add_GET(self):
"""
A smoke test to ensure GET on the add_view works.

View File

@ -34,8 +34,8 @@ class BaseStaticFilesMixin:
def static_template_snippet(self, path, asvar=False):
if asvar:
return "{%% load static from staticfiles %%}{%% static '%s' as var %%}{{ var }}" % path
return "{%% load static from staticfiles %%}{%% static '%s' %%}" % path
return "{%% load static from static %%}{%% static '%s' as var %%}{{ var }}" % path
return "{%% load static from static %%}{%% static '%s' %%}" % path
def assertStaticRenders(self, path, result, asvar=False, **kwargs):
template = self.static_template_snippet(path, asvar)

View File

@ -1,8 +1,8 @@
from urllib.parse import urljoin
from django.contrib.staticfiles import storage
from django.contrib.staticfiles.templatetags.staticfiles import static
from django.forms import Media
from django.templatetags.static import static
from django.test import SimpleTestCase, override_settings

View File

@ -0,0 +1,45 @@
import warnings
from urllib.parse import urljoin
from django.contrib.staticfiles import storage
from django.contrib.staticfiles.templatetags.staticfiles import static
from django.template import Context, Template
from django.test import SimpleTestCase, override_settings
from django.utils.deprecation import RemovedInDjango30Warning
class StaticTestStorage(storage.StaticFilesStorage):
def url(self, name):
return urljoin('https://example.com/assets/', name)
@override_settings(
STATIC_URL='http://media.example.com/static/',
INSTALLED_APPS=('django.contrib.staticfiles',),
STATICFILES_STORAGE='staticfiles_tests.test_forms.StaticTestStorage',
)
class StaticDeprecationTests(SimpleTestCase):
def test_templatetag_deprecated(self):
msg = '{% load staticfiles %} is deprecated in favor of {% load static %}.'
template = "{% load staticfiles %}{% static 'main.js' %}"
with warnings.catch_warnings(record=True) as recorded:
warnings.simplefilter('always')
template = Template(template)
rendered = template.render(Context())
self.assertEqual(rendered, 'https://example.com/assets/main.js')
self.assertEqual(len(recorded), 1)
self.assertIs(recorded[0].category, RemovedInDjango30Warning)
self.assertEqual(str(recorded[0].message), msg)
def test_static_deprecated(self):
msg = (
'django.contrib.staticfiles.templatetags.static() is deprecated in '
'favor of django.templatetags.static.static().'
)
with warnings.catch_warnings(record=True) as recorded:
warnings.simplefilter('always')
url = static('main.js')
self.assertEqual(url, 'https://example.com/assets/main.js')
self.assertEqual(len(recorded), 1)
self.assertIs(recorded[0].category, RemovedInDjango30Warning)
self.assertEqual(str(recorded[0].message), msg)