Skip to content

Commit

Permalink
1.8 compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
ojii committed Feb 3, 2015
1 parent 6cf05f7 commit ed986d2
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 18 deletions.
6 changes: 5 additions & 1 deletion runtests.py
Expand Up @@ -33,11 +33,15 @@
def runtests():
from django import VERSION
from django.conf import settings
if VERSION[0] == 1 and VERSION[1] < 6:

This comment has been minimized.

Copy link
@jrief

jrief Apr 30, 2015

just a note: It is DRYer to write if VERSION[:2] <= (1, 6)

runner = 'django.test.simple.DjangoTestSuiteRunner'
else:
runner = 'django.test.runner.DiscoverRunner'
settings.configure(
INSTALLED_APPS=INSTALLED_APPS,
ROOT_URLCONF=ROOT_URLCONF,
DATABASES=DATABASES,
TEST_RUNNER='django.test.simple.DjangoTestSuiteRunner',
TEST_RUNNER=runner,
TEMPLATE_DIRS=TEMPLATE_DIRS,
TEMPLATE_CONTEXT_PROCESSORS=TEMPLATE_CONTEXT_PROCESSORS,
TEMPLATE_DEBUG=TEMPLATE_DEBUG,
Expand Down
49 changes: 32 additions & 17 deletions sekizai/helpers.py
@@ -1,9 +1,28 @@
# -*- coding: utf-8 -*-
from collections import namedtuple

from django.conf import settings
from django.template import VariableNode, Variable
from django.template.base import VariableNode, Variable, Template
from django.template.loader import get_template
from django.template.loader_tags import BlockNode, ExtendsNode

try:
from django.template import engines
except ImportError:
engines = None

if engines is not None:
FAKE_CONTEXT = namedtuple('Context', 'engine')(engines.all()[0])
else:
FAKE_CONTEXT = {}


def _get_nodelist(tpl):
if isinstance(tpl, Template):
return tpl.nodelist
else:
return tpl.template.nodelist


def is_variable_extend_node(node):
if hasattr(node, 'parent_name_expr') and node.parent_name_expr:
Expand All @@ -23,9 +42,9 @@ def _extend_blocks(extend_node, blocks):
# we don't support variable extensions
if is_variable_extend_node(extend_node):
return
parent = extend_node.get_parent(None)
parent = extend_node.get_parent(FAKE_CONTEXT)
# Search for new blocks
for node in parent.nodelist.get_nodes_by_type(BlockNode):
for node in _get_nodelist(parent).get_nodes_by_type(BlockNode):
if node.name not in blocks:
blocks[node.name] = node
else:
Expand All @@ -38,7 +57,7 @@ def _extend_blocks(extend_node, blocks):
block = block.super
block.super = node
# search for further ExtendsNodes
for node in parent.nodelist.get_nodes_by_type(ExtendsNode):
for node in _get_nodelist(parent).get_nodes_by_type(ExtendsNode):
_extend_blocks(node, blocks)
break

Expand All @@ -56,29 +75,25 @@ def _extend_nodelist(extend_node):
found = []

for block in blocks.values():
found += _scan_namespaces(block.nodelist, block, blocks.keys())
found += _scan_namespaces(block.nodelist, block)

parent_template = extend_node.get_parent({})
parent_template = extend_node.get_parent(FAKE_CONTEXT)
# if this is the topmost template, check for namespaces outside of blocks
if not parent_template.nodelist.get_nodes_by_type(ExtendsNode):
if not _get_nodelist(parent_template).get_nodes_by_type(ExtendsNode):
found += _scan_namespaces(
parent_template.nodelist,
None,
blocks.keys()
_get_nodelist(parent_template),
None
)
else:
found += _scan_namespaces(
parent_template.nodelist,
extend_node,
blocks.keys()
_get_nodelist(parent_template),
extend_node
)
return found


def _scan_namespaces(nodelist, current_block=None, ignore_blocks=None):
def _scan_namespaces(nodelist, current_block=None):
from sekizai.templatetags.sekizai_tags import RenderBlock
if ignore_blocks is None:
ignore_blocks = []
found = []

for node in nodelist:
Expand All @@ -103,7 +118,7 @@ def _scan_namespaces(nodelist, current_block=None, ignore_blocks=None):

def get_namespaces(template):
compiled_template = get_template(template)
return _scan_namespaces(compiled_template.nodelist)
return _scan_namespaces(_get_nodelist(compiled_template))


def validate_template(template, namespaces):
Expand Down

0 comments on commit ed986d2

Please sign in to comment.