Django

Code

Ticket #625: django-template-decorators-2.diff

File django-template-decorators-2.diff, 3.0 kB (added by rjwittams, 3 years ago)

updated patch, moved to template/decorators.py to fit in with template reorganisation

  • django/core/template/decorators.py

    old new  
     1from inspect import getargspec 
     2from django.core.template import Context, Node, TemplateSyntaxError, register_tag, resolve_variable 
     3from django.core.template_loader import render_to_string, get_template 
     4from django.utils.functional import curry 
     5 
     6def gen_compile_func(params, defaults, name, node_class, parser, token): 
     7    #look in tags for 
     8    bits = token.contents.split()[1:] 
     9    bmax = len(params) 
     10    def_len = defaults and len(defaults) or 0 
     11    bmin = bmax - def_len 
     12    if( len(bits) < bmin or len(bits) > bmax  ): 
     13        if bmin == bmax: 
     14            message = "%s takes %s arguments" % (name, bmin) 
     15        else: 
     16            message = "%s takes between %s and %s arguments" % (name, bmin, bmax) 
     17        raise TemplateSyntaxError(message) 
     18    return node_class(bits) 
     19 
     20 
     21def simple_tag(func): 
     22    (params,_, _, defaults) = getargspec(func) 
     23    class SimpleNode(Node): 
     24        def __init__(self, vars_to_resolve): 
     25            #get the vars to resolve 
     26            self.vars_to_resolve = vars_to_resolve     
     27 
     28        def render(self, context): 
     29            resolved_vars = [resolve_variable(var, context)  
     30                              for var in self.vars_to_resolve] 
     31            return func(*resolved_vars) 
     32    compile_func = curry(gen_compile_func, params, defaults, func.__name__, SimpleNode) 
     33    compile_func.__doc__ = func.__doc__ 
     34    register_tag(func.__name__, compile_func) 
     35    return func 
     36 
     37 
     38def inclusion_tag(file_name, context_class=Context, takes_context=False): 
     39    def dec(func): 
     40        (params,_, _, defaults) = getargspec(func) 
     41        if takes_context: 
     42            if params[0] == 'context': 
     43                params = params[1:] 
     44            else: 
     45                raise TemplateSyntaxError("Any tag function decorated with takes_context=True must have a first argument of 'context'" ) 
     46        class InclusionNode(Node): 
     47            def __init__(self, vars_to_resolve): 
     48                self.vars_to_resolve = vars_to_resolve 
     49 
     50            def render(self, context): 
     51                resolved_vars = [resolve_variable(var, context) 
     52                                 for var in self.vars_to_resolve] 
     53                if takes_context: 
     54                    args = [context] + resolved_vars 
     55                else: 
     56                    args = resolved_vars 
     57 
     58                dict = func(*args) 
     59                 
     60                if not getattr(self, 'nodelist', False): 
     61                    t = get_template(file_name) 
     62                    self.nodelist = t.nodelist 
     63                return self.nodelist.render(context_class(dict)) 
     64 
     65        compile_func = curry(gen_compile_func, params, defaults, func.__name__, InclusionNode) 
     66        compile_func.__doc__ = func.__doc__ 
     67        register_tag(func.__name__, compile_func) 
     68        return func 
     69    return dec 
     70