
--- app.py | 15 +++++++++------ templates/user.html | 4 ++-- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/app.py b/app.py index 9c6f6d1..ee95747 100644 --- a/app.py +++ b/app.py @@ -25,11 +25,14 @@ 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 @@ -58,7 +61,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" diff --git a/templates/user.html b/templates/user.html index 0d382bf..a757cb8 100644 --- a/templates/user.html +++ b/templates/user.html @@ -109,13 +109,13 @@ <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 -%} -- 2.28.0.rc2