annotate libervia/pages/_browser/template.py @ 1306:c07112ef01cd

browser (template): adapted filters/global/extensions to manage SàT templates: `template` module has been update so most SàT template can be run from browser: - `profile` and `csrf_token` are set as globals - an implementation of `xmlattr` filter has been added - `date_fmt` filter has been implemented using `moment.js` - i18n method `_` has been added to globals, and `{% trans %}` statement has been implemented using an extension. For now they are not actually translating but just returning the unmodified string. - new `get_args` helper method to handle `nunjucks` convention for arguments. - fixed `get_elt` to only return the first child element (avoiding any text child) + added a defaut value for `context`
author Goffi <goffi@goffi.org>
date Thu, 16 Jul 2020 09:08:50 +0200
parents 8aba2a2078ca
children 3411ec23c389
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1298
8aba2a2078ca browser: new template module:
Goffi <goffi@goffi.org>
parents:
diff changeset
1 """Integrate templating system using nunjucks"""
8aba2a2078ca browser: new template module:
Goffi <goffi@goffi.org>
parents:
diff changeset
2
8aba2a2078ca browser: new template module:
Goffi <goffi@goffi.org>
parents:
diff changeset
3 from js_modules.nunjucks import nunjucks
8aba2a2078ca browser: new template module:
Goffi <goffi@goffi.org>
parents:
diff changeset
4 from browser import window, document
8aba2a2078ca browser: new template module:
Goffi <goffi@goffi.org>
parents:
diff changeset
5 import javascript
8aba2a2078ca browser: new template module:
Goffi <goffi@goffi.org>
parents:
diff changeset
6
8aba2a2078ca browser: new template module:
Goffi <goffi@goffi.org>
parents:
diff changeset
7
8aba2a2078ca browser: new template module:
Goffi <goffi@goffi.org>
parents:
diff changeset
8 safe = nunjucks.runtime.SafeString.new
8aba2a2078ca browser: new template module:
Goffi <goffi@goffi.org>
parents:
diff changeset
9 env = nunjucks.configure(
8aba2a2078ca browser: new template module:
Goffi <goffi@goffi.org>
parents:
diff changeset
10 window.templates_root_url,
8aba2a2078ca browser: new template module:
Goffi <goffi@goffi.org>
parents:
diff changeset
11 {
8aba2a2078ca browser: new template module:
Goffi <goffi@goffi.org>
parents:
diff changeset
12 'autoescape': True,
8aba2a2078ca browser: new template module:
Goffi <goffi@goffi.org>
parents:
diff changeset
13 'trimBlocks': True,
8aba2a2078ca browser: new template module:
Goffi <goffi@goffi.org>
parents:
diff changeset
14 'lstripBlocks': True,
8aba2a2078ca browser: new template module:
Goffi <goffi@goffi.org>
parents:
diff changeset
15 })
8aba2a2078ca browser: new template module:
Goffi <goffi@goffi.org>
parents:
diff changeset
16
8aba2a2078ca browser: new template module:
Goffi <goffi@goffi.org>
parents:
diff changeset
17 nunjucks.installJinjaCompat()
1306
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
18 env.addGlobal("profile", window.profile)
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
19 env.addGlobal("csrf_token", window.csrf_token)
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
20 # FIXME: integrate gettext or equivalent here
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
21 env.addGlobal("_", lambda txt: txt)
1298
8aba2a2078ca browser: new template module:
Goffi <goffi@goffi.org>
parents:
diff changeset
22
8aba2a2078ca browser: new template module:
Goffi <goffi@goffi.org>
parents:
diff changeset
23
8aba2a2078ca browser: new template module:
Goffi <goffi@goffi.org>
parents:
diff changeset
24 class Indexer:
8aba2a2078ca browser: new template module:
Goffi <goffi@goffi.org>
parents:
diff changeset
25 """Index global to a page"""
8aba2a2078ca browser: new template module:
Goffi <goffi@goffi.org>
parents:
diff changeset
26
8aba2a2078ca browser: new template module:
Goffi <goffi@goffi.org>
parents:
diff changeset
27 def __init__(self):
8aba2a2078ca browser: new template module:
Goffi <goffi@goffi.org>
parents:
diff changeset
28 self._indexes = {}
8aba2a2078ca browser: new template module:
Goffi <goffi@goffi.org>
parents:
diff changeset
29
8aba2a2078ca browser: new template module:
Goffi <goffi@goffi.org>
parents:
diff changeset
30 def next(self, value):
8aba2a2078ca browser: new template module:
Goffi <goffi@goffi.org>
parents:
diff changeset
31 if value not in self._indexes:
8aba2a2078ca browser: new template module:
Goffi <goffi@goffi.org>
parents:
diff changeset
32 self._indexes[value] = 0
8aba2a2078ca browser: new template module:
Goffi <goffi@goffi.org>
parents:
diff changeset
33 return 0
8aba2a2078ca browser: new template module:
Goffi <goffi@goffi.org>
parents:
diff changeset
34 self._indexes[value] += 1
8aba2a2078ca browser: new template module:
Goffi <goffi@goffi.org>
parents:
diff changeset
35 return self._indexes[value]
8aba2a2078ca browser: new template module:
Goffi <goffi@goffi.org>
parents:
diff changeset
36
8aba2a2078ca browser: new template module:
Goffi <goffi@goffi.org>
parents:
diff changeset
37 def current(self, value):
8aba2a2078ca browser: new template module:
Goffi <goffi@goffi.org>
parents:
diff changeset
38 return self._indexes.get(value)
8aba2a2078ca browser: new template module:
Goffi <goffi@goffi.org>
parents:
diff changeset
39
8aba2a2078ca browser: new template module:
Goffi <goffi@goffi.org>
parents:
diff changeset
40
8aba2a2078ca browser: new template module:
Goffi <goffi@goffi.org>
parents:
diff changeset
41 gidx = Indexer()
8aba2a2078ca browser: new template module:
Goffi <goffi@goffi.org>
parents:
diff changeset
42 # suffix use to avoid collision with IDs generated in static page
8aba2a2078ca browser: new template module:
Goffi <goffi@goffi.org>
parents:
diff changeset
43 SCRIPT_SUFF = "__script__"
8aba2a2078ca browser: new template module:
Goffi <goffi@goffi.org>
parents:
diff changeset
44
1306
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
45 def escape_html(txt):
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
46 return (
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
47 txt
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
48 .replace('&', '&amp;')
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
49 .replace('<', '&lt;')
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
50 .replace('>', '&gt;')
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
51 .replace('"', '&quot;')
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
52 )
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
53
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
54
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
55 def get_args(n_args, *sig_args, **sig_kwargs):
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
56 """Retrieve function args when they are transmitted using nunjucks convention
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
57
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
58 cf. https://mozilla.github.io/nunjucks/templating.html#keyword-arguments
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
59 @param n_args: argument from nunjucks call
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
60 @param sig_args: expected positional arguments
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
61 @param sig_kwargs: expected keyword arguments
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
62 @return: all expected arguments, with default value if not specified in nunjucks
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
63 """
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
64 # nunjucks set kwargs in last argument
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
65 given_args = list(n_args)
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
66 try:
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
67 given_kwargs = given_args.pop().to_dict()
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
68 except (AttributeError, IndexError):
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
69 # we don't have a dict as last argument
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
70 # that happens when there is no keyword argument
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
71 given_args = list(n_args)
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
72 given_kwargs = {}
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
73 ret = given_args[:len(sig_args)]
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
74 # we check if we have remaining positional arguments
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
75 # in which case they may be specified in keyword arguments
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
76 for name in sig_args[len(given_args):]:
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
77 try:
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
78 value = given_kwargs.pop(name)
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
79 except KeyError:
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
80 raise ValueError(f"missing positional arguments {name!r}")
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
81 ret.append(value)
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
82
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
83 extra_pos_args = given_args[len(sig_args):]
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
84 # and now the keyword arguments
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
85 for name, default in sig_kwargs.items():
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
86 if extra_pos_args:
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
87 # kw args has been specified with a positional argument
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
88 ret.append(extra_pos_args.pop(0))
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
89 continue
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
90 value = given_kwargs.get(name, default)
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
91 ret.append(value)
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
92
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
93 return ret
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
94
1298
8aba2a2078ca browser: new template module:
Goffi <goffi@goffi.org>
parents:
diff changeset
95
8aba2a2078ca browser: new template module:
Goffi <goffi@goffi.org>
parents:
diff changeset
96 def _next_gidx(value):
8aba2a2078ca browser: new template module:
Goffi <goffi@goffi.org>
parents:
diff changeset
97 """Use next current global index as suffix"""
8aba2a2078ca browser: new template module:
Goffi <goffi@goffi.org>
parents:
diff changeset
98 next_ = gidx.next(value)
8aba2a2078ca browser: new template module:
Goffi <goffi@goffi.org>
parents:
diff changeset
99 return f"{value}{SCRIPT_SUFF}" if next_ == 0 else f"{value}_{SCRIPT_SUFF}{next_}"
8aba2a2078ca browser: new template module:
Goffi <goffi@goffi.org>
parents:
diff changeset
100
8aba2a2078ca browser: new template module:
Goffi <goffi@goffi.org>
parents:
diff changeset
101 env.addFilter("next_gidx", _next_gidx)
8aba2a2078ca browser: new template module:
Goffi <goffi@goffi.org>
parents:
diff changeset
102
8aba2a2078ca browser: new template module:
Goffi <goffi@goffi.org>
parents:
diff changeset
103
8aba2a2078ca browser: new template module:
Goffi <goffi@goffi.org>
parents:
diff changeset
104 def _cur_gidx(value):
8aba2a2078ca browser: new template module:
Goffi <goffi@goffi.org>
parents:
diff changeset
105 """Use current current global index as suffix"""
8aba2a2078ca browser: new template module:
Goffi <goffi@goffi.org>
parents:
diff changeset
106 current = gidx.current(value)
8aba2a2078ca browser: new template module:
Goffi <goffi@goffi.org>
parents:
diff changeset
107 return f"{value}{SCRIPT_SUFF}" if not current else f"{value}_{SCRIPT_SUFF}{current}"
8aba2a2078ca browser: new template module:
Goffi <goffi@goffi.org>
parents:
diff changeset
108
8aba2a2078ca browser: new template module:
Goffi <goffi@goffi.org>
parents:
diff changeset
109 env.addFilter("cur_gidx", _cur_gidx)
8aba2a2078ca browser: new template module:
Goffi <goffi@goffi.org>
parents:
diff changeset
110
8aba2a2078ca browser: new template module:
Goffi <goffi@goffi.org>
parents:
diff changeset
111
1306
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
112 def _xmlattr(d, autospace=True):
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
113 if not d:
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
114 return
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
115 d = d.to_dict()
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
116 ret = [''] if autospace else []
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
117 for key, value in d.items():
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
118 if value is not None:
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
119 ret.append(f'{escape_html(key)}="{escape_html(str(value))}"')
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
120
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
121 return safe(' '.join(ret))
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
122
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
123 env.addFilter("xmlattr", _xmlattr)
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
124
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
125
1298
8aba2a2078ca browser: new template module:
Goffi <goffi@goffi.org>
parents:
diff changeset
126 def _tojson(value):
1306
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
127 return safe(escape_html(window.JSON.stringify(value)))
1298
8aba2a2078ca browser: new template module:
Goffi <goffi@goffi.org>
parents:
diff changeset
128
8aba2a2078ca browser: new template module:
Goffi <goffi@goffi.org>
parents:
diff changeset
129 env.addFilter("tojson", _tojson)
8aba2a2078ca browser: new template module:
Goffi <goffi@goffi.org>
parents:
diff changeset
130
8aba2a2078ca browser: new template module:
Goffi <goffi@goffi.org>
parents:
diff changeset
131
8aba2a2078ca browser: new template module:
Goffi <goffi@goffi.org>
parents:
diff changeset
132 def _icon_use(name, cls=""):
8aba2a2078ca browser: new template module:
Goffi <goffi@goffi.org>
parents:
diff changeset
133 kwargs = cls.to_dict()
8aba2a2078ca browser: new template module:
Goffi <goffi@goffi.org>
parents:
diff changeset
134 cls = kwargs.get('cls')
8aba2a2078ca browser: new template module:
Goffi <goffi@goffi.org>
parents:
diff changeset
135 return safe(
8aba2a2078ca browser: new template module:
Goffi <goffi@goffi.org>
parents:
diff changeset
136 '<svg class="svg-icon{cls}" xmlns="http://www.w3.org/2000/svg" '
8aba2a2078ca browser: new template module:
Goffi <goffi@goffi.org>
parents:
diff changeset
137 'viewBox="0 0 100 100">\n'
8aba2a2078ca browser: new template module:
Goffi <goffi@goffi.org>
parents:
diff changeset
138 ' <use href="#{name}"/>'
8aba2a2078ca browser: new template module:
Goffi <goffi@goffi.org>
parents:
diff changeset
139 '</svg>\n'.format(name=name, cls=(" " + cls) if cls else "")
8aba2a2078ca browser: new template module:
Goffi <goffi@goffi.org>
parents:
diff changeset
140 )
8aba2a2078ca browser: new template module:
Goffi <goffi@goffi.org>
parents:
diff changeset
141
8aba2a2078ca browser: new template module:
Goffi <goffi@goffi.org>
parents:
diff changeset
142 env.addGlobal("icon", _icon_use)
8aba2a2078ca browser: new template module:
Goffi <goffi@goffi.org>
parents:
diff changeset
143
8aba2a2078ca browser: new template module:
Goffi <goffi@goffi.org>
parents:
diff changeset
144
1306
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
145 def _date_fmt(
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
146 timestamp, *args
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
147 ):
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
148 """Date formatting
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
149
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
150 cf. sat.tools.common.date_utils for arguments details
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
151 """
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
152 fmt, date_only, auto_limit, auto_old_fmt, auto_new_fmt = get_args(
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
153 args, fmt="short", date_only=False, auto_limit=7, auto_old_fmt="short",
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
154 auto_new_fmt="relative",
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
155 )
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
156 from js_modules.moment import moment
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
157 date = moment.unix(timestamp)
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
158
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
159 if fmt == "auto_day":
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
160 fmt, auto_limit, auto_old_fmt, auto_new_fmt = "auto", 0, "short", "HH:mm"
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
161 if fmt == "auto":
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
162 limit = moment().startOf('day').subtract(auto_limit, 'days')
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
163 m_fmt = auto_old_fmt if date < limit else auto_new_fmt
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
164
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
165 if fmt == "short":
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
166 m_fmt = "DD/MM/YY" if date_only else "DD/MM/YY HH:mm"
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
167 elif fmt == "medium":
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
168 m_fmt = "ll" if date_only else "lll"
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
169 elif fmt == "long":
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
170 m_fmt = "LL" if date_only else "LLL"
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
171 elif fmt == "full":
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
172 m_fmt = "dddd, LL" if date_only else "LLLL"
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
173 elif fmt == "relative":
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
174 return date.fromNow()
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
175 elif fmt == "iso":
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
176 if date_only:
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
177 m_fmt == "YYYY-MM-DD"
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
178 else:
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
179 return date.toISOString()
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
180 else:
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
181 raise NotImplementedError("free format is not implemented yet")
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
182
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
183 return date.format(m_fmt)
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
184
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
185 env.addFilter("date_fmt", _date_fmt)
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
186
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
187
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
188 class I18nExtension:
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
189 """Extension to handle the {% trans %}{% endtrans %} statement"""
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
190 # FIXME: for now there is no translation, this extension only returns the string
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
191 # unmodified
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
192 tags = ['trans']
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
193
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
194 def parse(self, parser, nodes, lexer):
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
195 tok = parser.nextToken()
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
196 args = parser.parseSignature(None, True)
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
197 parser.advanceAfterBlockEnd(tok.value)
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
198 body = parser.parseUntilBlocks('endtrans')
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
199 parser.advanceAfterBlockEnd()
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
200 return nodes.CallExtension.new(self._js_ext, 'run', args, [body])
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
201
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
202 def run(self, context, *args):
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
203 body = args[-1]
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
204 return body()
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
205
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
206 @classmethod
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
207 def install(cls, env):
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
208 ext = cls()
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
209 ext_dict = {
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
210 "tags": ext.tags,
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
211 "parse": ext.parse,
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
212 "run": ext.run
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
213 }
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
214 ext._js_ext = javascript.pyobj2jsobj(ext_dict)
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
215 env.addExtension(cls.__name__, ext._js_ext)
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
216
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
217 I18nExtension.install(env)
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
218
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
219
1298
8aba2a2078ca browser: new template module:
Goffi <goffi@goffi.org>
parents:
diff changeset
220 class Template:
8aba2a2078ca browser: new template module:
Goffi <goffi@goffi.org>
parents:
diff changeset
221
8aba2a2078ca browser: new template module:
Goffi <goffi@goffi.org>
parents:
diff changeset
222 def __init__(self, tpl_name):
8aba2a2078ca browser: new template module:
Goffi <goffi@goffi.org>
parents:
diff changeset
223 self._tpl = env.getTemplate(tpl_name, True)
8aba2a2078ca browser: new template module:
Goffi <goffi@goffi.org>
parents:
diff changeset
224
8aba2a2078ca browser: new template module:
Goffi <goffi@goffi.org>
parents:
diff changeset
225 def render(self, context):
8aba2a2078ca browser: new template module:
Goffi <goffi@goffi.org>
parents:
diff changeset
226 return self._tpl.render(context)
8aba2a2078ca browser: new template module:
Goffi <goffi@goffi.org>
parents:
diff changeset
227
1306
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
228 def get_elt(self, context=None):
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
229 if context is None:
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
230 context = {}
1298
8aba2a2078ca browser: new template module:
Goffi <goffi@goffi.org>
parents:
diff changeset
231 raw_html = self.render(context)
8aba2a2078ca browser: new template module:
Goffi <goffi@goffi.org>
parents:
diff changeset
232 template_elt = document.createElement('template')
8aba2a2078ca browser: new template module:
Goffi <goffi@goffi.org>
parents:
diff changeset
233 template_elt.innerHTML = raw_html
1306
c07112ef01cd browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents: 1298
diff changeset
234 return template_elt.content.firstElementChild