from inspect import getargspec from django template import resolve_var

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
from inspect import getargspec
from django.template import resolve_variable, Node, generic_tag_compiler, TemplateSyntaxError
from django.utils.functional import curry
from django import template
def simple_tag_with_context(self):
def dec(func):
params, xx, xxx, defaults = getargspec(func)
if params[0] == 'context':
params = params[1:]
else:
raise TemplateSyntaxError, "Any tag function decorated with simple_tag_with_context must have a first argument of 'context'"
class SimpleNode(Node):
def __init__(self, vars_to_resolve):
self.vars_to_resolve = vars_to_resolve
def render(self, context):
resolved_vars = [resolve_variable(var, context) for var in self.vars_to_resolve]
resolved_vars.insert(0,context)
return func(*resolved_vars)
compile_func = curry(generic_tag_compiler, params, defaults, getattr(func, "_decorated_function", func).__name__, SimpleNode)
compile_func.__doc__ = func.__doc__
self.tag(getattr(func, "_decorated_function", func).__name__, compile_func)
return func
return dec