kopia lustrzana https://github.com/wagtail/wagtail
Allow a target variable to be specified for get_settings template tag. (#6891)
rodzic
4b6b1f23bd
commit
0daae4a2b8
|
@ -14,6 +14,7 @@ Changelog
|
|||
* Utilize `PageQuerySet.defer_streamfields()` to improve efficiency in a few key places (Andy Babic)
|
||||
* Switch ``register_setting``, ``register_settings_menu_item`` to use SVG icons (Thibaud Colas)
|
||||
* Add support to SVG icons for ``SearchArea`` subclasses in ``register_admin_search_area`` (Thibaud Colas)
|
||||
* `get_settings` template tag now supports specifying the variable name with `{% get_settings as var %}` (Samir Shah)
|
||||
* Fix: StreamField required status is now consistently handled by the `blank` keyword argument (Matt Westcott)
|
||||
* Fix: Show 'required' asterisks for blocks inside required StreamFields (Matt Westcott)
|
||||
* Fix: Make image chooser "Select format" fields translatable (Helen Chapman, Thibaud Colas)
|
||||
|
|
|
@ -170,6 +170,15 @@ If there is no ``request`` available in the template at all, you can use the set
|
|||
|
||||
.. note:: You can not reliably get the correct settings instance for the current site from this template tag if the request object is not available. This is only relevant for multisite instances of Wagtail.
|
||||
|
||||
By default, the tag will create or update a ``settings`` variable in the context. If you want to
|
||||
assign to a different context variable instead, use ``{% get_settings as other_variable_name %}``:
|
||||
|
||||
.. code-block:: html+django
|
||||
|
||||
{% load wagtailsettings_tags %}
|
||||
{% get_settings as wagtail_settings %}
|
||||
{{ wagtail_settings.app_label.SocialMediaSettings.instagram }}
|
||||
|
||||
.. _settings_tag_jinja2:
|
||||
|
||||
Using in Jinja2 templates
|
||||
|
|
|
@ -31,6 +31,7 @@ Other features
|
|||
* Update ``PageQueryset.specific(defer=True)`` to only perform a single database query (Andy Babic)
|
||||
* Switched ``register_setting``, ``register_settings_menu_item`` to use SVG icons (Thibaud Colas)
|
||||
* Add support to SVG icons for ``SearchArea`` subclasses in ``register_admin_search_area`` (Thibaud Colas)
|
||||
* ``get_settings`` template tag now supports specifying the variable name with ``{% get_settings as var %}`` (Samir Shah)
|
||||
|
||||
Bug fixes
|
||||
~~~~~~~~~
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
from django.template import Library
|
||||
from django.template import Library, Node
|
||||
from django.template.defaulttags import token_kwargs
|
||||
|
||||
from wagtail.core.models import Site
|
||||
|
||||
|
@ -8,14 +9,33 @@ from ..context_processors import SettingsProxy
|
|||
register = Library()
|
||||
|
||||
|
||||
@register.simple_tag(takes_context=True)
|
||||
def get_settings(context, use_default_site=False):
|
||||
if use_default_site:
|
||||
site = Site.objects.get(is_default_site=True)
|
||||
context['settings'] = SettingsProxy(site)
|
||||
elif 'request' in context:
|
||||
context['settings'] = SettingsProxy(context['request'])
|
||||
else:
|
||||
raise RuntimeError('No request found in context, and use_default_site '
|
||||
'flag not set')
|
||||
return ''
|
||||
class GetSettingsNode(Node):
|
||||
def __init__(self, kwargs, target_var):
|
||||
self.kwargs = kwargs
|
||||
self.target_var = target_var
|
||||
|
||||
@staticmethod
|
||||
def get_settings_object(context, use_default_site=False):
|
||||
if use_default_site:
|
||||
site = Site.objects.get(is_default_site=True)
|
||||
return SettingsProxy(site)
|
||||
if 'request' in context:
|
||||
return SettingsProxy(context['request'])
|
||||
|
||||
raise RuntimeError('No request found in context, and use_default_site flag not set')
|
||||
|
||||
def render(self, context):
|
||||
resolved_kwargs = {k: v.resolve(context) for k, v in self.kwargs.items()}
|
||||
context[self.target_var] = self.get_settings_object(context, **resolved_kwargs)
|
||||
return ''
|
||||
|
||||
|
||||
@register.tag
|
||||
def get_settings(parser, token):
|
||||
bits = token.split_contents()[1:]
|
||||
target_var = 'settings'
|
||||
if len(bits) >= 2 and bits[-2] == 'as':
|
||||
target_var = bits[-1]
|
||||
bits = bits[:-2]
|
||||
kwargs = token_kwargs(bits, parser) if bits else {}
|
||||
return GetSettingsNode(kwargs, target_var)
|
||||
|
|
|
@ -138,6 +138,37 @@ class TestTemplateTag(TemplateTestCase):
|
|||
with self.assertRaises(RuntimeError):
|
||||
template.render(context)
|
||||
|
||||
def test_get_settings_variable_assignment_request_context(self):
|
||||
"""
|
||||
Check that assigning the setting to a context variable with
|
||||
{% get_settings as wagtail_settings %} works.
|
||||
"""
|
||||
request = self.get_request(site=self.other_site)
|
||||
context = Context({'request': request})
|
||||
template = Template('{% load wagtailsettings_tags %}'
|
||||
'{% get_settings as wagtail_settings %}'
|
||||
'{{ wagtail_settings.tests.testsetting.title}}')
|
||||
|
||||
self.assertEqual(template.render(context), self.other_site_settings.title)
|
||||
# Also check that the default 'settings' variable hasn't been set
|
||||
template = Template('{% load wagtailsettings_tags %}'
|
||||
'{% get_settings as wagtail_settings %}'
|
||||
'{{ settings.tests.testsetting.title}}')
|
||||
|
||||
self.assertEqual(template.render(context), '')
|
||||
|
||||
def test_get_settings_variable_assigment_use_default(self):
|
||||
"""
|
||||
Check that assigning the setting to a context variable with
|
||||
{% get_settings use_default_site=True as wagtail_settings %} works.
|
||||
"""
|
||||
context = Context()
|
||||
template = Template('{% load wagtailsettings_tags %}'
|
||||
'{% get_settings use_default_site=True as wagtail_settings %}'
|
||||
'{{ wagtail_settings.tests.testsetting.title }}')
|
||||
|
||||
self.assertEqual(template.render(context), self.default_site_settings.title)
|
||||
|
||||
|
||||
class TestSettingsJinja(TemplateTestCase):
|
||||
|
||||
|
|
Ładowanie…
Reference in New Issue