1 # -*- coding: utf-8 -*-
2 # This file is part of django-ssify, licensed under GNU Affero GPLv3 or later.
3 # Copyright © Fundacja Nowoczesna Polska. See README.md for more information.
6 Utilities for defining SSI variables.
8 SSI variables are a way of providing values that need to be computed
9 at request time to the prerendered templates.
12 from __future__ import unicode_literals
13 from hashlib import md5
14 from django import template
15 from django.utils.encoding import force_text, python_2_unicode_compatible
16 from django.utils.functional import Promise
17 from django.utils.safestring import mark_safe
18 from .exceptions import SsiVarsDependencyCycleError
21 @python_2_unicode_compatible
22 class SsiVariable(object):
24 Represents a variable computed by a template tag with given arguments.
26 Instance of this class is returned from any template tag created
27 with `decorators.ssi_variable` decorator. If renders as SSI echo
28 statement, but you can also use it as an argument to {% ssi_include %},
29 to other ssi_variable, or create SSI if statements by using
30 its `if`, `else`, `endif` properties.
32 Variable's name, as used in SSI statements, is a hash of its definition,
33 so the user never has to deal with it directly.
36 def __init__(self, tagpath=None, args=None, kwargs=None, name=None):
37 self.tagpath = tagpath
38 self.args = list(args or [])
39 self.kwargs = kwargs or {}
44 """Variable name is a hash of its definition."""
45 if self._name is None:
46 self._name = 'v' + md5(json_encode(self.definition).encode('ascii')).hexdigest()
51 Sometimes there's a need to reset the variable name.
53 Typically, this is the case after finding real values for
54 variables passed as arguments to {% ssi_include %}.
61 """Variable is defined by path to template tag and its arguments."""
63 return self.tagpath, self.args, self.kwargs
65 return self.tagpath, self.args
70 return "SsiVariable(%s: %s)" % (self.name, repr(self.definition))
72 def get_value(self, request):
73 """Computes the real value of the variable, using the request."""
74 taglib, tagname = self.tagpath.rsplit('.', 1)
75 return template.get_library(taglib).tags[tagname].get_value(
76 request, *self.args, **self.kwargs)
79 return mark_safe("<!--#echo var='%s' encoding='none'-->" % self.name)
82 """Returns the form that can be used in SSI include's URL."""
83 return '${%s}' % self.name
85 # If-else-endif properties for use in templates.
86 setattr(SsiVariable, 'if',
87 lambda self: mark_safe("<!--#if expr='${%s}'-->" % self.name))
88 setattr(SsiVariable, 'else',
89 staticmethod(lambda: mark_safe("<!--#else-->")))
90 setattr(SsiVariable, 'endif',
91 staticmethod(lambda: mark_safe('<!--#endif-->')))
94 class SsiExpect(object):
95 """This class says: I want the real value of this variable here."""
96 def __init__(self, name):
99 return "SsiExpect(%s)" % (self.name,)
102 def ssi_expect(var, type_):
104 Helper function for defining get_ssi_vars on ssi_included views.
106 The view needs a way of calculating all the needed variables from
107 the view args. But the args are probably the wrong type
108 (typically, str instead of int) or even are SsiVariables, not
109 resolved until request time.
111 This function provides a way to expect a real value of the needed type.
114 if isinstance(var, SsiVariable):
115 return SsiExpect(var.name)
120 class SsiVariableNode(template.Node):
121 """ Node for the SsiVariable tags. """
122 def __init__(self, tagpath, args, kwargs, vary=None, asvar=None):
123 self.tagpath = tagpath
130 return "<SsiVariableNode>"
132 def render(self, context):
133 """Renders the tag as SSI echo or sets the context variable."""
134 resolved_args = [var.resolve(context) for var in self.args]
135 resolved_kwargs = dict((k, v.resolve(context))
136 for k, v in self.kwargs.items())
137 var = SsiVariable(self.tagpath, resolved_args, resolved_kwargs)
139 request = context['request']
140 request.ssi_vars_needed[var.name] = var
142 request.ssi_vary.update(self.vary)
145 context.dicts[0][self.asvar] = var
151 def ssi_set_statement(var, value):
152 """Generates an SSI set statement for a variable."""
153 if isinstance(value, Promise):
154 # Yes, this is quite brutal. But we need to know
155 # the real value now, we don't know the type,
156 # and we only want to evaluate the lazy function once.
157 value = value._proxy____cast()
158 if value is False or value is None:
160 return "<!--#set var='%s' value='%s'-->" % (
162 force_text(value).replace('\\', '\\\\').replace("'", "\\'"))
165 def provide_vars(request, ssi_vars):
167 Provides all the SSI set statements for ssi_vars variables.
169 The main purpose of this function is to by called by SsifyMiddleware.
171 def resolve_expects(var):
172 if not hasattr(var, 'hash_dirty'):
173 var.hash_dirty = False
175 for i, arg in enumerate(var.args):
176 if isinstance(arg, SsiExpect):
177 var.args[i] = resolved[arg.name]
178 var.hash_dirty = True
179 for k, arg in var.kwargs.items():
180 if isinstance(arg, SsiExpect):
181 var.kwargs[k] = resolved[arg.name]
182 var.hash_dirty = True
184 for arg in var.args + list(var.kwargs.values()):
185 if isinstance(arg, SsiVariable):
186 var.hash_dirty = resolve_expects(arg) or var.hash_dirty
188 hash_dirty = var.hash_dirty
190 # Rehash after calculating the SsiExpects with real
191 # values, because that's what the included views expect.
193 var.hash_dirty = False
197 def resolve_args(var):
199 for k, arg in var.kwargs.items():
200 kwargs[k] = resolved[arg.name] if isinstance(arg, SsiVariable) else arg
201 new_var = SsiVariable(var.tagpath,
202 [resolved[arg.name] if isinstance(arg, SsiVariable) else arg for arg in var.args],
207 queue = list(ssi_vars.values())
209 unresolved_streak = 0
214 rv = resolve_args(var)
215 except KeyError as e:
217 unresolved_streak += 1
218 if unresolved_streak > len(queue):
219 raise SsiVarsDependencyCycleError(request, queue, resolved)
222 resolved[var.name] = rv.get_value(request)
223 unresolved_streak = 0
225 output = "".join(ssi_set_statement(var, value)
226 for (var, value) in resolved.items()
231 from .serializers import json_encode