home · contact · privacy
Improve calories counter layout.
authorChristian Heller <c.heller@plomlompom.de>
Tue, 10 Oct 2023 20:11:14 +0000 (22:11 +0200)
committerChristian Heller <c.heller@plomlompom.de>
Tue, 10 Oct 2023 20:11:14 +0000 (22:11 +0200)
calories.py

index 23234b2e0924718dfbca7edb3f725ac3e79039c5..5f710878460e2cd4a11599eb9850f89f60e7d117 100644 (file)
@@ -158,18 +158,18 @@ class Database:
         eatable = self.eatables[c.eatable_key]
         calories = eatable.cals * c.unit_count
         sugar_g = eatable.sugar_g * c.unit_count
-        # calories = float(eatable.cals / eatable.standard_g) * c.unit_count * c.unit_weight
-        # sugar_g = float(eatable.sugar_g / eatable.standard_g) * c.unit_count * c.unit_weight
         self.today.calories += calories
         self.today.sugar_g += sugar_g
         return {"cals": calories, "sugar": sugar_g }
 
-    def eatables_selection(self, selection=None):
+    def eatables_selection(self):
         html = ''
+        already_selected = [c.eatable_key for c in self.consumptions]
         for k, v in sorted(self.eatables.items(), key=lambda item: item[1].title):
+            if k in already_selected:
+                continue
             v = self.eatables[k]
-            selected = ' selected' if k==selection else ''
-            html += '<option value="%s"%s>%s</option>' % (k, selected, v.title)
+            html += '<option value="%s">%s</option>' % (k, v.title)
         return html
 
     def add_eatable(self, id_, eatable):
@@ -225,7 +225,7 @@ class MyServer(BaseHTTPRequestHandler):
                 if uuid not in to_delete:
                     e = Eatable(decode("title", i, False), decode("cals", i), decode("sugar_g", i), decode("standard_g", i), decode("comments", i, False))
                     db.add_eatable(uuid, e)
-                i += 1   
+                i += 1
         if 'title' in postvars.keys() and len(postvars['title'][i]) > 0:
              e = Eatable(decode("title", i, False), decode("cals", i), decode("sugar_g", i), decode("standard_g", i), decode("comments", i, False))
              db.add_eatable(str(uuid4()), e)
@@ -253,7 +253,7 @@ class MyServer(BaseHTTPRequestHandler):
                 if c.unit_count > 0:
                     db.eatables[c.eatable_key].popularity += 1
             db.consumptions = []
-            default_slots = 10 
+            default_slots = 10
             for k, v in sorted(db.eatables.items(), key=lambda item: -item[1].popularity):
                 db.add_consumption(Consumption(k, 0))
                 default_slots -= 1
@@ -291,13 +291,13 @@ class MyServer(BaseHTTPRequestHandler):
         for c in db.consumptions:
             r = db.calc_consumption(c)
             consumptions += "<tr />"\
-                    "<input type=\"hidden\" name=\"keep_visible\" value=\"1\">"\
-                    "<td><select name=\"eatable_key\">%s</select></td>"\
+                    "<input type=\"hidden\" name=\"keep_visible\" value=\"1\"><input name=\"eatable_key\" type=\"hidden\" value=\"%s\">"\
+                    "<td>%s</td>"\
                     "<td><input class=\"unit_count\" name=\"unit_count\" type=\"number\" min=\"0\" step=\"0.1\" value=\"%.1f\" /></td>"\
                     "<td></td>"\
                     "<td>%.1f</td>"\
                     "<td>%.1f</td>"\
-                    "</tr>" % (db.eatables_selection(c.eatable_key), c.unit_count, r["cals"], r["sugar"])
+                    "</tr>" % (c.eatable_key, db.eatables[c.eatable_key].title, c.unit_count, r["cals"], r["sugar"])
         day_rows = ""
         for date in sorted(db.days.keys()):
             day = db.days[date]