app.jinja_env.filters['humanize'] = humanize_ts
+def days_filter(time):
+ """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:
<style>
+
+.warning {
+ background-color: navajowhite;
+}
+
+.bad {
+ background-color: orangered;
+ color: white;
+}
+
.events {
margin: 0.8em;
margin-bottom: 1.5em;
{##########}
<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 = {
</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 -%}
<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>