Mercurial > libervia-backend
comparison src/tools/common/template.py @ 2466:d2e16a7466a0
template: date filter improvments:
new format (i.e. format after date limit has been reached) can now be specified.
0 can now be used for "auto_limit" to use last midnight as date limit.
A new "auto_day" format can be used as shortcut to use last midnight as date limit, short datetime for old format, and time only for new format.
author | Goffi <goffi@goffi.org> |
---|---|
date | Wed, 03 Jan 2018 00:29:18 +0100 |
parents | bb0bbcc80fc8 |
children | 0046283a285d |
comparison
equal
deleted
inserted
replaced
2465:bb0bbcc80fc8 | 2466:d2e16a7466a0 |
---|---|
24 from sat.core import exceptions | 24 from sat.core import exceptions |
25 from sat.core.log import getLogger | 25 from sat.core.log import getLogger |
26 log = getLogger(__name__) | 26 log = getLogger(__name__) |
27 import os.path | 27 import os.path |
28 from xml.sax.saxutils import quoteattr | 28 from xml.sax.saxutils import quoteattr |
29 import datetime | |
29 import time | 30 import time |
30 import re | 31 import re |
31 from babel import support | 32 from babel import support |
32 from babel import Locale | 33 from babel import Locale |
33 from babel.core import UnknownLocaleError | 34 from babel.core import UnknownLocaleError |
367 return self.date_fmt(timestamp, fmt, date_only, auto_limit, auto_old_fmt) | 368 return self.date_fmt(timestamp, fmt, date_only, auto_limit, auto_old_fmt) |
368 except Exception as e: | 369 except Exception as e: |
369 log.warning(_(u"Can't parse date: {msg}").format(msg=e)) | 370 log.warning(_(u"Can't parse date: {msg}").format(msg=e)) |
370 return timestamp | 371 return timestamp |
371 | 372 |
372 def date_fmt(self, timestamp, fmt='short', date_only=False, auto_limit=None, auto_old_fmt=None): | 373 def date_fmt(self, timestamp, fmt='short', date_only=False, auto_limit=7, auto_old_fmt='short', auto_new_fmt='relative'): |
373 """format date according to locale | 374 """format date according to locale |
374 | 375 |
375 @param timestamp(basestring, int): unix time | 376 @param timestamp(basestring, int): unix time |
376 @param fmt(str): one of: | 377 @param fmt(str): one of: |
377 - short: e.g. u'31/12/17' | 378 - short: e.g. u'31/12/17' |
381 - relative: format in relative time | 382 - relative: format in relative time |
382 e.g.: 3 hours | 383 e.g.: 3 hours |
383 note that this format is not precise | 384 note that this format is not precise |
384 - iso: ISO 8601 format | 385 - iso: ISO 8601 format |
385 e.g.: u'2007-04-01T19:53:23Z' | 386 e.g.: u'2007-04-01T19:53:23Z' |
386 - auto_limit (int, None): limit in days before using auto_old_fmt | 387 - auto: use auto_old_fmt if date is older than auto_limit |
387 None: use default(7 days) | 388 else use auto_new_fmt |
388 - auto_old_fmt(unicode, None): format to use when date is olded than limit | 389 - auto_day: shorcut to set auto format with change on day |
389 None: use default(short) | 390 old format will be short, and new format will be time only |
390 or a free value which is passed to babel.dates.format_datetime | 391 or a free value which is passed to babel.dates.format_datetime |
391 @param date_only(bool): if True, only display date (not datetime) | 392 @param date_only(bool): if True, only display date (not datetime) |
393 @param auto_limit (int): limit in days before using auto_old_fmt | |
394 use 0 to have a limit at last midnight (day change) | |
395 @param auto_old_fmt(unicode): format to use when date is older than limit | |
396 @param auto_new_fmt(unicode): format to use when date is equal to or more recent | |
397 than limit | |
392 | 398 |
393 """ | 399 """ |
394 if is_undefined(fmt): | 400 if is_undefined(fmt): |
395 fmt = u'short' | 401 fmt = u'short' |
396 | 402 |
397 if (auto_limit is not None or auto_old_fmt is not None) and fmt != 'auto': | 403 if (auto_limit is not None or auto_old_fmt is not None) and fmt != 'auto': |
398 raise ValueError(u'auto argument can only be used with auto fmt') | 404 raise ValueError(u'auto argument can only be used with auto fmt') |
405 if fmt == 'auto_day': | |
406 fmt, auto_limit, auto_old_fmt, auto_new_fmt = 'auto', 0, 'short', 'HH:mm' | |
399 if fmt == 'auto': | 407 if fmt == 'auto': |
400 days_delta = (time.time() - int(timestamp)) / 3600 | 408 if auto_limit == 0: |
401 if days_delta > (auto_limit or 7): | 409 today = time.mktime(datetime.date.today().timetuple()) |
402 fmt = auto_old_fmt or 'short' | 410 if int(timestamp) < today: |
411 fmt = auto_old_fmt | |
412 else: | |
413 fmt = auto_new_fmt | |
403 else: | 414 else: |
404 fmt = 'relative' | 415 days_delta = (time.time() - int(timestamp)) / 3600 |
416 if days_delta > (auto_limit or 7): | |
417 fmt = auto_old_fmt | |
418 else: | |
419 fmt = auto_new_fmt | |
405 | 420 |
406 if fmt == 'relative': | 421 if fmt == 'relative': |
407 delta = int(timestamp) - time.time() | 422 delta = int(timestamp) - time.time() |
408 return dates.format_timedelta(delta, granularity="minute", add_direction=True, locale=self._locale_str) | 423 return dates.format_timedelta(delta, granularity="minute", add_direction=True, locale=self._locale_str) |
409 elif fmt in ('short', 'long'): | 424 elif fmt in ('short', 'long'): |