Skip to content

Commit

Permalink
Django 1.11 compatibility improvements (see #232).
Browse files Browse the repository at this point in the history
  • Loading branch information
idlesign committed Apr 6, 2017
1 parent 9a90022 commit d0d095c
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ django-sitetree changelog
Unreleased
----------
+ IMPORTANT: i18n trees now support lang variations (e.g. de-ch, pt-br), update your i18n trees aliases.
+ Django 1.11 compatibility improvements.


v1.7.0
Expand Down
3 changes: 2 additions & 1 deletion sitetree/sitetreeapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
_THREAD_SITETREE = 'sitetree'

_URL_TAG_NEW_STYLE = VERSION >= (1, 5, 0)
_CONTEXT_FLATTEN = VERSION >= (1, 11)

_UNSET = set() # Sentinel

Expand Down Expand Up @@ -958,7 +959,7 @@ def children(self, parent_item, navigation_type, use_template, context):

context.push()
context['sitetree_items'] = tree_items
rendered = my_template.render(context)
rendered = my_template.render(context.flatten() if _CONTEXT_FLATTEN else context)
context.pop()

return rendered
Expand Down
4 changes: 2 additions & 2 deletions sitetree/templatetags/sitetree.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from django.template.loader import get_template
from django.template.base import FilterExpression

from ..sitetreeapp import get_sitetree
from ..sitetreeapp import get_sitetree, _CONTEXT_FLATTEN


register = template.Library()
Expand Down Expand Up @@ -301,7 +301,7 @@ def render(context, tree_items, use_template):
if isinstance(use_template, FilterExpression):
use_template = use_template.resolve(context)

content = get_template(use_template).render(context)
content = get_template(use_template).render(context.flatten() if _CONTEXT_FLATTEN else context)
context.pop()

return content
24 changes: 19 additions & 5 deletions sitetree/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from django.http import HttpRequest
from django.conf import settings, global_settings
from django import VERSION
from django.template.context import Context
from django.template.context import Context, RenderContext
from django.template.base import Template


Expand Down Expand Up @@ -75,8 +75,13 @@ def __init__(self, path='/', user=None, meta=None):


def contribute_to_context(context, current_app=''):
context.template = mock.MagicMock()
context.template.engine.string_if_invalid = ''
template = mock.MagicMock()
template.engine.string_if_invalid = ''

context.template = template

if VERSION >= (1, 11):
context.render_context = RenderContext()

if VERSION >= (1, 10):
match = mock.MagicMock()
Expand Down Expand Up @@ -178,10 +183,19 @@ def render_template_tag():
"""
def render(tag_library, tag_str, context=None):
context = context or {}
context = Context(context)

if not isinstance(context, Context):
context = Context(context)

contribute_to_context(context)
string = '{%% load %s %%}{%% %s %%}' % (tag_library, tag_str)
return Template(string).render(context)
template = Template(string)

if VERSION >= (1, 11):
# Prevent "TypeError: 'NoneType' object is not iterable" in get_exception_info
template.nodelist[1].token.position = (0, 0)

return template.render(context)
return render


Expand Down

0 comments on commit d0d095c

Please sign in to comment.