0
0
mirror of https://github.com/wagtail/wagtail.git synced 2024-12-01 11:41:20 +01:00

Merge branch 'refactor/jinja-support' of https://github.com/takeflight/wagtail

This commit is contained in:
Matt Westcott 2015-09-30 16:48:21 +01:00
commit 5a67971d1d
2 changed files with 30 additions and 5 deletions

View File

@ -7,6 +7,19 @@ from wagtail.wagtailcore.models import Page, PAGE_TEMPLATE_VAR
register = template.Library()
def get_page_instance(context):
"""
Given a template context, try and find a Page variable in the common
places. Returns None if a page can not be found.
"""
possible_names = [PAGE_TEMPLATE_VAR, 'self']
for name in possible_names:
if name in context:
page = context[name]
if isinstance(page, Page):
return page
@register.simple_tag(takes_context=True)
def wagtailuserbar(context):
# Find request object
@ -16,13 +29,13 @@ def wagtailuserbar(context):
if not request.user.has_perm('wagtailadmin.access_admin'):
return ''
# Only render if the context contains a 'PAGE_TEMPLATE_VAR' variable
# referencing a saved page
if PAGE_TEMPLATE_VAR not in context:
# Only render if the context contains a variable referencing a saved page
page = get_page_instance(context)
if page is None:
return ''
page = context[PAGE_TEMPLATE_VAR]
if not isinstance(page, Page) or page.id is None:
# Dont render anything if the page has not been saved - i.e. a preview
if page.pk is None:
return ''
try:

View File

@ -29,6 +29,18 @@ class TestUserbarTag(TestCase):
self.assertIn("<!-- Wagtail user bar embed code -->", content)
def test_userbar_tag_self(self):
"""
Ensure the userbar renders with `self` instead of `PAGE_TEMPLATE_VAR`
"""
template = Template("{% load wagtailuserbar %}{% wagtailuserbar %}")
content = template.render(Context({
'self': self.homepage,
'request': self.dummy_request(self.user),
}))
self.assertIn("<!-- Wagtail user bar embed code -->", content)
def test_userbar_tag_anonymous_user(self):
template = Template("{% load wagtailuserbar %}{% wagtailuserbar %}")
content = template.render(Context({