Ticket #6237: django-pickling-template-nodes.patch

File django-pickling-template-nodes.patch, 4.9 KB (added by Simon Law <simon@…>, 7 years ago)
  • django/template/__init__.py

    diff --git a/django/template/__init__.py b/django/template/__init__.py
    a b class DebugVariableNode(VariableNode): 
    876876        else:
    877877            return output
    878878
    879 def generic_tag_compiler(params, defaults, name, node_class, parser, token):
     879def generic_tag_compiler(init, params, defaults, name, node_class, parser, token):
    880880    "Returns a template.Node subclass."
    881881    bits = token.split_contents()[1:]
    882882    bmax = len(params)
    def generic_tag_compiler(params, default 
    888888        else:
    889889            message = "%s takes between %s and %s arguments" % (name, bmin, bmax)
    890890        raise TemplateSyntaxError, message
    891     return node_class(bits)
     891    return node_class(bits, **init)
     892
     893class SimpleNode(Node):
     894    def __init__(self, vars_to_resolve, func):
     895        self.vars_to_resolve = map(Variable, vars_to_resolve)
     896        self.func = func
     897
     898    def render(self, context):
     899        resolved_vars = [var.resolve(context) for var in self.vars_to_resolve]
     900        return self.func(*resolved_vars)
     901
     902class InclusionNode(Node):
     903    def __init__(self, vars_to_resolve, file_name, context_class, takes_context, func):
     904        self.vars_to_resolve = map(Variable, vars_to_resolve)
     905        self.file_name = file_name
     906        self.context_class = context_class
     907        self.takes_context = takes_context
     908        self.func = func
     909
     910    def render(self, context):
     911        resolved_vars = [var.resolve(context) for var in self.vars_to_resolve]
     912        if self.takes_context:
     913            args = [context] + resolved_vars
     914        else:
     915            args = resolved_vars
     916
     917        dict = self.func(*args)
     918
     919        if not getattr(self, 'nodelist', False):
     920            from django.template.loader import get_template, select_template
     921            if not isinstance(self.file_name, basestring) and is_iterable(self.file_name):
     922                t = select_template(self.file_name)
     923            else:
     924                t = get_template(self.file_name)
     925            self.nodelist = t.nodelist
     926        return self.nodelist.render(self.context_class(dict,
     927                autoescape=context.autoescape))
    892928
    893929class Library(object):
    894930    def __init__(self):
    class Library(object): 
    946982    def simple_tag(self,func):
    947983        params, xx, xxx, defaults = getargspec(func)
    948984
    949         class SimpleNode(Node):
    950             def __init__(self, vars_to_resolve):
    951                 self.vars_to_resolve = map(Variable, vars_to_resolve)
    952 
    953             def render(self, context):
    954                 resolved_vars = [var.resolve(context) for var in self.vars_to_resolve]
    955                 return func(*resolved_vars)
    956 
    957         compile_func = curry(generic_tag_compiler, params, defaults, getattr(func, "_decorated_function", func).__name__, SimpleNode)
     985        compile_func = curry(generic_tag_compiler, {"func": func}, params, defaults, getattr(func, "_decorated_function", func).__name__, SimpleNode)
    958986        compile_func.__doc__ = func.__doc__
    959987        self.tag(getattr(func, "_decorated_function", func).__name__, compile_func)
    960988        return func
    class Library(object): 
    968996                else:
    969997                    raise TemplateSyntaxError, "Any tag function decorated with takes_context=True must have a first argument of 'context'"
    970998
    971             class InclusionNode(Node):
    972                 def __init__(self, vars_to_resolve):
    973                     self.vars_to_resolve = map(Variable, vars_to_resolve)
    974 
    975                 def render(self, context):
    976                     resolved_vars = [var.resolve(context) for var in self.vars_to_resolve]
    977                     if takes_context:
    978                         args = [context] + resolved_vars
    979                     else:
    980                         args = resolved_vars
    981 
    982                     dict = func(*args)
    983 
    984                     if not getattr(self, 'nodelist', False):
    985                         from django.template.loader import get_template, select_template
    986                         if not isinstance(file_name, basestring) and is_iterable(file_name):
    987                             t = select_template(file_name)
    988                         else:
    989                             t = get_template(file_name)
    990                         self.nodelist = t.nodelist
    991                     return self.nodelist.render(context_class(dict,
    992                             autoescape=context.autoescape))
    993 
    994             compile_func = curry(generic_tag_compiler, params, defaults, getattr(func, "_decorated_function", func).__name__, InclusionNode)
     999            init = {"file_name": file_name, "context_class": context_class, "takes_context": takes_context, "func": func}
     1000            compile_func = curry(generic_tag_compiler, init, params, defaults, getattr(func, "_decorated_function", func).__name__, InclusionNode)
    9951001            compile_func.__doc__ = func.__doc__
    9961002            self.tag(getattr(func, "_decorated_function", func).__name__, compile_func)
    9971003            return func
Back to Top