]> rtime.felk.cvut.cz Git - coffee/coffee-flask.git/commitdiff
Colorize milk cleaning event box after some days without cleaning
authorMichal Sojka <michal.sojka@cvut.cz>
Thu, 20 Aug 2020 20:21:30 +0000 (22:21 +0200)
committerMichal Sojka <michal.sojka@cvut.cz>
Fri, 21 Aug 2020 10:27:25 +0000 (12:27 +0200)
app.py
templates/user.html

diff --git a/app.py b/app.py
index 2a4b96b988fcee1e9bfb583ef3d07b540f00eb59..06c6be2c9cf36b99167f528ada33e1c765ffd349 100644 (file)
--- a/app.py
+++ b/app.py
@@ -73,6 +73,17 @@ def humanize_ts(time, max_interval="years"):
 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:
index a757cb809c04c3e3f9e7ce6f982a55e68b5fcbf6..cca3ef833913444be5d377cc9b62b833ed2fa70a 100644 (file)
@@ -1,4 +1,14 @@
 <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) }}
         &nbsp;&nbsp;
         {{ event_box('coffee pack', ['COFFEE_PACK_OPENED'] ) }}
       </div>