Skip to content

Commit

Permalink
Apply the patch (patch.15426.diff) of the ticket #15053
Browse files Browse the repository at this point in the history
  • Loading branch information
goinnn committed Jul 16, 2012
1 parent 35ddeee commit ddb9fc1
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 13 deletions.
3 changes: 3 additions & 0 deletions django/template/base.py
Expand Up @@ -227,9 +227,11 @@ def create_token(self, token_string, in_tag):
else: else:
token = Token(TOKEN_TEXT, token_string) token = Token(TOKEN_TEXT, token_string)
token.lineno = self.lineno token.lineno = self.lineno
token.source = self.origin, (-1, -1)
self.lineno += token_string.count('\n') self.lineno += token_string.count('\n')
return token return token



class Parser(object): class Parser(object):
def __init__(self, tokens): def __init__(self, tokens):
self.tokens = tokens self.tokens = tokens
Expand Down Expand Up @@ -304,6 +306,7 @@ def extend_nodelist(self, nodelist, node, token):
"in the template." % node) "in the template." % node)
if isinstance(nodelist, NodeList) and not isinstance(node, TextNode): if isinstance(nodelist, NodeList) and not isinstance(node, TextNode):
nodelist.contains_nontext = True nodelist.contains_nontext = True
node.source = token.source
nodelist.append(node) nodelist.append(node)


def enter_command(self, command, token): def enter_command(self, command, token):
Expand Down
4 changes: 0 additions & 4 deletions django/template/debug.py
Expand Up @@ -55,10 +55,6 @@ def create_nodelist(self):
def create_variable_node(self, contents): def create_variable_node(self, contents):
return DebugVariableNode(contents) return DebugVariableNode(contents)


def extend_nodelist(self, nodelist, node, token):
node.source = token.source
super(DebugParser, self).extend_nodelist(nodelist, node, token)

def unclosed_block_tag(self, parse_until): def unclosed_block_tag(self, parse_until):
command, source = self.command_stack.pop() command, source = self.command_stack.pop()
msg = "Unclosed tag '%s'. Looking for one of: %s " % (command, ', '.join(parse_until)) msg = "Unclosed tag '%s'. Looking for one of: %s " % (command, ', '.join(parse_until))
Expand Down
31 changes: 25 additions & 6 deletions django/template/loader.py
Expand Up @@ -78,11 +78,17 @@ def __init__(self, display_name, loader, name, dirs):
def reload(self): def reload(self):
return self.loader(self.loadname, self.dirs)[0] return self.loader(self.loadname, self.dirs)[0]


class LoaderOriginLite(object):

def __init__(self, display_name, loader, name, dirs):
self.name = display_name
self.loader, self.loadname, self.dirs = loader, name, dirs

def make_origin(display_name, loader, name, dirs): def make_origin(display_name, loader, name, dirs):
if settings.TEMPLATE_DEBUG and display_name: if settings.TEMPLATE_DEBUG and display_name:
return LoaderOrigin(display_name, loader, name, dirs) return LoaderOrigin(display_name, loader, name, dirs)
else: else:
return None return LoaderOriginLite(display_name, loader, name, dirs)


def find_template_loader(loader): def find_template_loader(loader):
if isinstance(loader, (tuple, list)): if isinstance(loader, (tuple, list)):
Expand Down Expand Up @@ -117,7 +123,7 @@ def find_template_loader(loader):
else: else:
raise ImproperlyConfigured('Loader does not define a "load_template" callable template source loader') raise ImproperlyConfigured('Loader does not define a "load_template" callable template source loader')


def find_template(name, dirs=None): def find_template(name, dirs=None, skip_template=None):
# Calculate template_source_loaders the first time the function is executed # Calculate template_source_loaders the first time the function is executed
# because putting this logic in the module-level namespace may cause # because putting this logic in the module-level namespace may cause
# circular import errors. See Django ticket #1292. # circular import errors. See Django ticket #1292.
Expand All @@ -129,20 +135,32 @@ def find_template(name, dirs=None):
if loader is not None: if loader is not None:
loaders.append(loader) loaders.append(loader)
template_source_loaders = tuple(loaders) template_source_loaders = tuple(loaders)
template_candidate = None
for loader in template_source_loaders: for loader in template_source_loaders:
try: try:
source, display_name = loader(name, dirs) source, display_name = loader(name, dirs)
return (source, make_origin(display_name, loader, name, dirs)) if skip_template:
extends_tags = source.nodelist[0]
extends_tags_origin, extends_tags_source = extends_tags.source
if extends_tags_origin.name == skip_template:
template_candidate = None
continue
if not template_candidate:
template_candidate = (source, make_origin(display_name, loader, name, dirs))
else:
return (source, make_origin(display_name, loader, name, dirs))
except TemplateDoesNotExist: except TemplateDoesNotExist:
pass pass
raise TemplateDoesNotExist(name) if not template_candidate:
raise TemplateDoesNotExist(name)
return template_candidate


def get_template(template_name): def get_template(template_name, skip_template=None):
""" """
Returns a compiled Template object for the given template name, Returns a compiled Template object for the given template name,
handling template inheritance recursively. handling template inheritance recursively.
""" """
template, origin = find_template(template_name) template, origin = find_template(template_name, skip_template=skip_template)
if not hasattr(template, 'render'): if not hasattr(template, 'render'):
# template needs to be compiled # template needs to be compiled
template = get_template_from_string(template, origin, template_name) template = get_template_from_string(template, origin, template_name)
Expand Down Expand Up @@ -184,6 +202,7 @@ def select_template(template_name_list):
not_found = [] not_found = []
for template_name in template_name_list: for template_name in template_name_list:
try: try:
import ipdb; ipdb.set_trace()
return get_template(template_name) return get_template(template_name)
except TemplateDoesNotExist as e: except TemplateDoesNotExist as e:
if e.args[0] not in not_found: if e.args[0] not in not_found:
Expand Down
14 changes: 11 additions & 3 deletions django/template/loader_tags.py
@@ -1,7 +1,7 @@
from django.conf import settings from django.conf import settings
from django.template.base import TemplateSyntaxError, Library, Node, TextNode,\ from django.template.base import TemplateSyntaxError, Library, Node, TextNode,\
token_kwargs, Variable token_kwargs, Variable
from django.template.loader import get_template from django.template.loader import find_template, get_template
from django.utils.safestring import mark_safe from django.utils.safestring import mark_safe


register = Library() register = Library()
Expand Down Expand Up @@ -94,10 +94,18 @@ def get_parent(self, context):
self.parent_name.token self.parent_name.token
raise TemplateSyntaxError(error_msg) raise TemplateSyntaxError(error_msg)
if hasattr(parent, 'render'): if hasattr(parent, 'render'):
return parent # parent is a Template object return parent # parent is a Template object
return get_template(parent) origin, source = self.source
return get_template(parent, skip_template=origin.name)


def render(self, context): def render(self, context):
origin, source = self.source
if origin and origin.loadname == self.parent_name:
template = find_template(self.parent_name, skip_template=origin.name)
template_source = template[0]
extends_tags = template_source.nodelist[0]
parent_origin, parent_source = extends_tags.source
self.parent_name = getattr(parent_origin, 'name', None)
compiled_parent = self.get_parent(context) compiled_parent = self.get_parent(context)


if BLOCK_CONTEXT_KEY not in context.render_context: if BLOCK_CONTEXT_KEY not in context.render_context:
Expand Down

0 comments on commit ddb9fc1

Please sign in to comment.