changeset 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 908be289eb49
files src/tools/common/template.py
diffstat 1 files changed, 24 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/src/tools/common/template.py	Wed Jan 03 00:23:36 2018 +0100
+++ b/src/tools/common/template.py	Wed Jan 03 00:29:18 2018 +0100
@@ -26,6 +26,7 @@
 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 @@
             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 @@
                 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 @@
 
         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()