]> rtime.felk.cvut.cz Git - coffee/coffee-flask.git/commitdiff
Report events at most in "days ago", not in weeks, months or years ago
authorMichal Sojka <michal.sojka@cvut.cz>
Thu, 20 Aug 2020 16:28:43 +0000 (18:28 +0200)
committerMichal Sojka <michal.sojka@cvut.cz>
Fri, 21 Aug 2020 10:26:31 +0000 (12:26 +0200)
app.py
templates/user.html

diff --git a/app.py b/app.py
index 9c6f6d168b0c0cacd3fe5562b19253c96c21e5de..9301ced0d64b9823e778b94fdd5ae060ceb7fc09 100644 (file)
--- a/app.py
+++ b/app.py
@@ -25,14 +25,20 @@ app.secret_key = b'_5#y2L"F4Q8z\n\xec]/'
 
 # Inspired by https://shubhamjain.co/til/how-to-render-human-readable-time-in-jinja/,
 # updated to our needs
-def humanize_ts(time):
-    """
-    Convert date in ISO format to relative, human readable string
-    like 'an hour ago', 'Yesterday', '3 months ago',
-    'just now', etc
+def humanize_ts(time, max_interval="years"):
+    """Convert date/time in ISO format to relative, human readable string.
+
+    Example return values: 'an hour ago', 'Yesterday', '3 months ago',
+    'just now', etc.
+
+    When optional max_interval is set to "days", the return value will
+    report at most the number of days ago, not week, months or years.
     """
     if jinja2.is_undefined(time):
         return time
+    if max_interval not in ["years", "days"]:
+        raise ValueError
+
     now = datetime.now(timezone.utc)
     if time[-1] == 'Z':     # Convert Zulu time zone to datetime compatible format
         time = time[0:-1] + '+00:00'
@@ -58,7 +64,7 @@ def humanize_ts(time):
             return str(int(second_diff / 3600)) + " hours ago"
     if day_diff == 1:
         return "Yesterday"
-    if day_diff < 7:
+    if day_diff < 7 or max_interval == "days":
         return str(day_diff) + " days ago"
     if day_diff < 31:
         return str(int(day_diff / 7)) + " weeks ago"
index 0d382bf651eea11ec53744a97c85eb5d60600682..a757cb809c04c3e3f9e7ce6f982a55e68b5fcbf6 100644 (file)
               <div class="btnline">
                 <input type="button" value="{{ verb[event]|first }}"
                        onclick="addEvent('{{ event }}', '{{ verb[events[0]]|last }} the {{ title }}')" />
-                       ({{ last_events[event] | humanize if event in last_events else "never" }})
+                       ({{ last_events[event] | humanize(max_interval="days") if event in last_events else "never" }})
               </div>
             {%- endfor -%}
           {%- else -%}    {# Nobody logged in - show overview with summary times #}
             {# Calculate maximum timestamp of all relevant events #}
             {%- set when = last_events.items() | selectattr(0, 'in',
-          events) | map(attribute=1) | max | humanize -%}
+          events) | map(attribute=1) | max | humanize(max_interval="days") -%}
               {%- if when -%}
                 {{ verb[events[0]]|last }} {{ when }}
               {%- else -%}