Mercurial > libervia-web
comparison libervia/web/pages/_browser/template.py @ 1583:9865013da86c
browser(template): add `bare_jid` and fix `date_fmt`
author | Goffi <goffi@goffi.org> |
---|---|
date | Tue, 28 Nov 2023 17:54:30 +0100 |
parents | 8d61654cb34f |
children |
comparison
equal
deleted
inserted
replaced
1582:f52b89365002 | 1583:9865013da86c |
---|---|
1 """Integrate templating system using nunjucks""" | 1 """Integrate templating system using nunjucks""" |
2 | 2 |
3 from js_modules.nunjucks import nunjucks | 3 from js_modules.nunjucks import nunjucks |
4 from browser import window, document | 4 from browser import window, document |
5 import javascript | 5 import javascript |
6 import jid | |
6 | 7 |
7 | 8 |
8 safe = nunjucks.runtime.SafeString.new | 9 safe = nunjucks.runtime.SafeString.new |
9 env = nunjucks.configure( | 10 env = nunjucks.configure( |
10 window.templates_root_url, | 11 window.templates_root_url, |
92 ret.append(value) | 93 ret.append(value) |
93 | 94 |
94 return ret | 95 return ret |
95 | 96 |
96 | 97 |
98 def _bare_jid(full_jid: str|jid.JID) -> str: | |
99 """Return the bare JID""" | |
100 return str(jid.JID(str(full_jid)).bare) | |
101 | |
102 env.addFilter("bare_jid", _bare_jid) | |
103 | |
104 | |
97 def _next_gidx(value): | 105 def _next_gidx(value): |
98 """Use next current global index as suffix""" | 106 """Use next current global index as suffix""" |
99 next_ = gidx.next(value) | 107 next_ = gidx.next(value) |
100 return f"{value}{SCRIPT_SUFF}" if next_ == 0 else f"{value}_{SCRIPT_SUFF}{next_}" | 108 return f"{value}{SCRIPT_SUFF}" if next_ == 0 else f"{value}_{SCRIPT_SUFF}{next_}" |
101 | 109 |
160 ) | 168 ) |
161 | 169 |
162 env.addGlobal("icon", _icon_use) | 170 env.addGlobal("icon", _icon_use) |
163 | 171 |
164 | 172 |
165 def _date_fmt( | 173 def _date_fmt(timestamp, *args): |
166 timestamp, *args | |
167 ): | |
168 """Date formatting | 174 """Date formatting |
169 | 175 |
170 cf. libervia.backend.tools.common.date_utils for arguments details | 176 cf. libervia.backend.tools.common.date_utils for arguments details |
171 """ | 177 """ |
172 fmt, date_only, auto_limit, auto_old_fmt, auto_new_fmt = get_args( | 178 fmt, date_only, auto_limit, auto_old_fmt, auto_new_fmt = get_args( |
176 from js_modules.moment import moment | 182 from js_modules.moment import moment |
177 date = moment.unix(timestamp) | 183 date = moment.unix(timestamp) |
178 | 184 |
179 if fmt == "auto_day": | 185 if fmt == "auto_day": |
180 fmt, auto_limit, auto_old_fmt, auto_new_fmt = "auto", 0, "short", "HH:mm" | 186 fmt, auto_limit, auto_old_fmt, auto_new_fmt = "auto", 0, "short", "HH:mm" |
187 | |
181 if fmt == "auto": | 188 if fmt == "auto": |
182 limit = moment().startOf('day').subtract(auto_limit, 'days') | 189 limit = moment().startOf('day').subtract(auto_limit, 'days') |
183 m_fmt = auto_old_fmt if date < limit else auto_new_fmt | 190 fmt = auto_old_fmt if date < limit else auto_new_fmt |
184 | 191 |
185 elif fmt == "short": | 192 format_mapping = { |
186 m_fmt = "DD/MM/YY" if date_only else "DD/MM/YY HH:mm" | 193 "short": "DD/MM/YY" if date_only else "DD/MM/YY HH:mm", |
187 elif fmt == "medium": | 194 "medium": "DD MMM YYYY" if date_only else "DD MMM YYYY HH:mm", |
188 m_fmt = "ll" if date_only else "lll" | 195 "long": "D MMMM YYYY" if date_only else "D MMMM YYYY HH:mm", |
189 elif fmt == "long": | 196 "full": "dddd, D MMMM YYYY" if date_only else "dddd, D MMMM YYYY HH:mm", |
190 m_fmt = "LL" if date_only else "LLL" | 197 "relative": date.fromNow(), |
191 elif fmt == "full": | 198 "iso": date.toISOString() if not date_only else "YYYY-MM-DD" |
192 m_fmt = "dddd, LL" if date_only else "LLLL" | 199 } |
193 elif fmt == "relative": | 200 |
194 return date.fromNow() | 201 # if `fmt` doesn't match any mapping, it's a free format |
195 elif fmt == "iso": | 202 m_fmt = format_mapping.get(fmt, fmt) |
196 if date_only: | 203 |
197 m_fmt == "YYYY-MM-DD" | 204 return m_fmt if fmt in ["relative", "iso"] else date.format(m_fmt) |
198 else: | |
199 return date.toISOString() | |
200 else: | |
201 raise NotImplementedError("free format is not implemented yet") | |
202 | |
203 return date.format(m_fmt) | |
204 | 205 |
205 env.addFilter("date_fmt", _date_fmt) | 206 env.addFilter("date_fmt", _date_fmt) |
206 | 207 |
207 | 208 |
208 class I18nExtension: | 209 class I18nExtension: |