from django import template from doorgen models import Site Page impor

 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
28
29
30
31
32
33
34
35
36
37
from django import template
from doorgen.models import Site, Page
import random
from inspect import getargspec
from django.template import resolve_variable, Node, generic_tag_compiler
from django.utils.functional import curry
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 takes_context=True 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
#template.Library.simple_tag_with_context = simple_tag_with_context
register = template.Library()
@simple_tag_with_context(register)
def text(context):
return context['manager'].get_text()