
Napíšu to do jednoho, ať neposílám dva maily. :) - U funkce days_filter(time, max_interval="years") se ten druhý parameter vůbec nepoužívá? - U funkce humanize_ts(…) [z 2/4] se mi moc nelíbí ten parametr 'max_interval'. Chápu, že ses chtěl vyhnout 'only_days=0/1', ale teď je tam něco, co vlastně funguje jen "trochu" -- když je tam ='days' dělá to něco, když je tam cokoliv jiného je to jak předtím. (Ale samozřejmě mám pochopení pro to, že jiné možnosti moc nepoužijeme :)) J. Cituji Michal Sojka <michal.sojka@cvut.cz>:
--- app.py | 11 +++++++++++ templates/user.html | 24 +++++++++++++++++------- 2 files changed, 28 insertions(+), 7 deletions(-)
diff --git a/app.py b/app.py index 1a3c79f..7504ab1 100644 --- a/app.py +++ b/app.py @@ -70,6 +70,17 @@ def humanize_ts(time, max_interval="years"): app.jinja_env.filters['humanize'] = humanize_ts
+def days_filter(time, max_interval="years"): + """Return the number of days elapsed since time.""" + if jinja2.is_undefined(time): + return time + diff = datetime.now(timezone.utc) - time + return diff.days + + +app.jinja_env.filters['days'] = days_filter + + @app.route('/') def hello(): if "uid" in session: diff --git a/templates/user.html b/templates/user.html index a757cb8..cca3ef8 100644 --- a/templates/user.html +++ b/templates/user.html @@ -1,4 +1,14 @@ <style> + +.warning { + background-color: navajowhite; +} + +.bad { + background-color: orangered; + color: white; +} + .events { margin: 0.8em; margin-bottom: 1.5em; @@ -92,8 +102,11 @@ {##########} <br /> <form> - {%- macro event_box(title, events) -%} - <div class="events-box"> + {%- macro event_box(title, events, warn_days=999, bad_days=9999) -%} + {# Calculate maximum timestamp of all relevant events #} + {%- set when = last_events.items() | selectattr(0, 'in', events) | map(attribute=1) | max -%} + {%- set days = when | days | default(0) -%} + <div class="events-box{% if days >= bad_days %} bad {% elif days >= warn_days %} warning{% endif %}"> <h4>{{title | capitalize}}</h4> {#- The first item in the list is used as button label, last item in the overview -#} {%- set verb = { @@ -113,11 +126,8 @@ </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(max_interval="days") -%} {%- if when -%} - {{ verb[events[0]]|last }} {{ when }} + {{ verb[events[0]]|last }} {{ when | humanize(max_interval="days") }} {%- else -%} never {{ verb[events[0]]|last }} {%- endif -%} @@ -127,7 +137,7 @@ <div class="events"> <h3>{{ ("Record<br />event" if name else "Events") | safe }}:</h3> {{ event_box('coffee machine', ['COFFEE_MACHINE_CLEANED'] ) }} - {{ event_box('milk container', ['MILK_CONTAINER_CLEANED', 'MILK_CONTAINER_CLEANED_WITH_TABLET'] ) }} + {{ event_box('milk container', ['MILK_CONTAINER_CLEANED', 'MILK_CONTAINER_CLEANED_WITH_TABLET'], warn_days=4, bad_days=7) }} {{ event_box('coffee pack', ['COFFEE_PACK_OPENED'] ) }} </div> -- 2.28.0.rc2
_______________________________________________ Coffee mailing list Coffee@rtime.felk.cvut.cz https://rtime.felk.cvut.cz/mailman/listinfo/coffee