Ticket #2414: 2414.diff
File 2414.diff, 7.4 KB (added by , 15 years ago) |
---|
-
django/template/loader_tags.py
### Eclipse Workspace Patch 1.0 #P Django trunk
6 6 7 7 register = Library() 8 8 9 class ExtendsError(Exception): 9 class ExtendsError(TemplateSyntaxError): 10 pass 11 12 class ExtendsRecursionError(ExtendsError): 13 pass 14 15 class ExtendsNotFoundError(ExtendsError): 10 16 pass 11 17 12 18 class BlockNode(Node): … … 43 49 self.nodelist = nodelist 44 50 self.parent_name, self.parent_name_expr = parent_name, parent_name_expr 45 51 self.template_dirs = template_dirs 52 self.seen_parent_names = [] 46 53 47 54 def __repr__(self): 48 55 if self.parent_name_expr: … … 57 64 error_msg = "Invalid template name in 'extends' tag: %r." % parent 58 65 if self.parent_name_expr: 59 66 error_msg += " Got this from the '%s' variable." % self.parent_name_expr.token 60 raise TemplateSyntaxError, error_msg67 raise ExtendsError(error_msg) 61 68 if hasattr(parent, 'render'): 62 69 return parent # parent is a Template object 70 if parent in self.seen_parent_names: 71 raise ExtendsRecursionError('Template %r cannot be extended, ' 72 'because it would cause a circular recursion' % parent) 63 73 try: 64 74 source, origin = find_template_source(parent, self.template_dirs) 65 75 except TemplateDoesNotExist: 66 raise TemplateSyntaxError, "Template %r cannot be extended, because it doesn't exist" % parent 67 else: 68 return get_template_from_string(source, origin, parent) 76 raise ExtendsNotFoundError("Template %r cannot be extended, " 77 "because it doesn't exist" % parent) 78 compiled_parent = get_template_from_string(source, origin, parent) 79 # Let the parent extends node know about seen names (to avoid circular 80 # recursion). 81 parent_extends_node = self.get_extends_node(compiled_parent) 82 if parent_extends_node: 83 seen_parent_names = self.seen_parent_names[:] 84 seen_parent_names.append(parent) 85 parent_extends_node.seen_parent_names.extend(seen_parent_names) 86 return compiled_parent 69 87 70 88 def render(self, context): 71 89 compiled_parent = self.get_parent(context) … … 81 99 # it'll be checked when the parent node's render() is called. 82 100 83 101 # Find out if the parent template has a parent itself 84 for node in compiled_parent.nodelist: 85 if not isinstance(node, TextNode): 86 # If the first non-text node is an extends, handle it. 87 if isinstance(node, ExtendsNode): 88 node.nodelist.append(block_node) 89 # Extends must be the first non-text node, so once you find 90 # the first non-text node you can stop looking. 91 break 102 node = self.get_extends_node(compiled_parent) 103 if node: 104 node.nodelist.append(block_node) 92 105 else: 93 106 # Keep any existing parents and add a new one. Used by BlockNode. 94 107 parent_block.parent = block_node.parent … … 96 109 parent_block.nodelist = block_node.nodelist 97 110 return compiled_parent.render(context) 98 111 112 def get_extends_node(self, template): 113 for node in template.nodelist: 114 if not isinstance(node, TextNode): 115 if isinstance(node, ExtendsNode): 116 return node 117 # Since extends must be the first non-text node, stop looking 118 # after the first one. 119 break 120 99 121 class ConstantIncludeNode(Node): 100 122 def __init__(self, template_path): 101 123 try: -
tests/regressiontests/templates/tests.py
15 15 from django import template 16 16 from django.core import urlresolvers 17 17 from django.template import loader 18 from django.template import loader_tags 18 19 from django.template.loaders import app_directories, filesystem 19 20 from django.utils.translation import activate, deactivate, ugettext as _ 20 21 from django.utils.safestring import mark_safe … … 215 216 if isinstance(vals[2], tuple): 216 217 normal_string_result = vals[2][0] 217 218 invalid_string_result = vals[2][1] 218 if '%s' in invalid_string_result: 219 if (isinstance(invalid_string_result, basestring) and 220 '%s' in invalid_string_result): 219 221 expected_invalid_str = 'INVALID %s' 220 222 invalid_string_result = invalid_string_result % vals[2][2] 221 223 template.invalid_var_format_string = True … … 474 476 ### EXCEPTIONS ############################################################ 475 477 476 478 # Raise exception for invalid template name 477 'exception01': ("{% extends 'nonexistent' %}", {}, template.TemplateSyntaxError),479 'exception01': ("{% extends 'nonexistent' %}", {}, loader_tags.ExtendsNotFoundError), 478 480 479 481 # Raise exception for invalid template name (in variable) 480 'exception02': ("{% extends nonexistent %}", {}, template.TemplateSyntaxError),482 'exception02': ("{% extends nonexistent %}", {}, (loader_tags.ExtendsError, loader_tags.ExtendsNotFoundError)), 481 483 482 484 # Raise exception for extra {% extends %} tags 483 485 'exception03': ("{% extends 'inheritance01' %}{% block first %}2{% endblock %}{% extends 'inheritance16' %}", {}, template.TemplateSyntaxError), … … 802 804 # Inheritance from a template with a space in its name should work. 803 805 'inheritance29': ("{% extends 'inheritance 28' %}", {}, '!'), 804 806 807 ### CIRCULAR INHERITANCE EXCEPTIONS ####################################### 808 809 # Self-extending template 810 'circularinheritance01': ("{% extends 'circularinheritance01' %}", {}, loader_tags.ExtendsRecursionError), 811 812 # Circular extending template 813 'circularinheritance02': ("{% extends 'circularinheritance04' %}", {}, loader_tags.ExtendsRecursionError), 814 'circularinheritance03': ("{% extends 'circularinheritance02' %}", {}, loader_tags.ExtendsRecursionError), 815 'circularinheritance04': ("{% extends 'circularinheritance03' %}", {}, loader_tags.ExtendsRecursionError), 816 817 # Self-extending template (by variable) 818 'circularinheritance05': ("{% extends var %}", {'var': 'circularinheritance05'}, loader_tags.ExtendsRecursionError), 819 820 # Circular template (by variable) 821 'circularinheritance06': ("{% extends var %}", {'var': 'circularinheritance08'}, loader_tags.ExtendsRecursionError), 822 'circularinheritance07': ("{% extends var %}", {'var': 'circularinheritance06'}, loader_tags.ExtendsRecursionError), 823 'circularinheritance08': ("{% extends var %}", {'var': 'circularinheritance07'}, loader_tags.ExtendsRecursionError), 824 805 825 ### I18N ################################################################## 806 826 807 827 # {% spaceless %} tag