From fa1654964dcab17e5cb7849da504a33c348343e3 Mon Sep 17 00:00:00 2001 From: Goffi Date: Wed, 3 Jan 2018 00:29:18 +0100 Subject: [PATCH] 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. --- src/tools/common/template.py | 33 ++++++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/src/tools/common/template.py b/src/tools/common/template.py index 12b1a99b..e67b0946 100644 --- a/src/tools/common/template.py +++ b/src/tools/common/template.py @@ -26,6 +26,7 @@ from sat.core.log import getLogger log = getLogger(__name__) import os.path from xml.sax.saxutils import quoteattr +import datetime import time import re from babel import support @@ -369,7 +370,7 @@ class Renderer(object): log.warning(_(u"Can't parse date: {msg}").format(msg=e)) return timestamp - def date_fmt(self, timestamp, fmt='short', date_only=False, auto_limit=None, auto_old_fmt=None): + def date_fmt(self, timestamp, fmt='short', date_only=False, auto_limit=7, auto_old_fmt='short', auto_new_fmt='relative'): """format date according to locale @param timestamp(basestring, int): unix time @@ -383,12 +384,17 @@ class Renderer(object): note that this format is not precise - iso: ISO 8601 format e.g.: u'2007-04-01T19:53:23Z' - - auto_limit (int, None): limit in days before using auto_old_fmt - None: use default(7 days) - - auto_old_fmt(unicode, None): format to use when date is olded than limit - None: use default(short) + - auto: use auto_old_fmt if date is older than auto_limit + else use auto_new_fmt + - auto_day: shorcut to set auto format with change on day + old format will be short, and new format will be time only or a free value which is passed to babel.dates.format_datetime @param date_only(bool): if True, only display date (not datetime) + @param auto_limit (int): limit in days before using auto_old_fmt + use 0 to have a limit at last midnight (day change) + @param auto_old_fmt(unicode): format to use when date is older than limit + @param auto_new_fmt(unicode): format to use when date is equal to or more recent + than limit """ if is_undefined(fmt): @@ -396,12 +402,21 @@ class Renderer(object): if (auto_limit is not None or auto_old_fmt is not None) and fmt != 'auto': raise ValueError(u'auto argument can only be used with auto fmt') + if fmt == 'auto_day': + fmt, auto_limit, auto_old_fmt, auto_new_fmt = 'auto', 0, 'short', 'HH:mm' if fmt == 'auto': - days_delta = (time.time() - int(timestamp)) / 3600 - if days_delta > (auto_limit or 7): - fmt = auto_old_fmt or 'short' + if auto_limit == 0: + today = time.mktime(datetime.date.today().timetuple()) + if int(timestamp) < today: + fmt = auto_old_fmt + else: + fmt = auto_new_fmt else: - fmt = 'relative' + days_delta = (time.time() - int(timestamp)) / 3600 + if days_delta > (auto_limit or 7): + fmt = auto_old_fmt + else: + fmt = auto_new_fmt if fmt == 'relative': delta = int(timestamp) - time.time()