from http.server import BaseHTTPRequestHandler, HTTPServer
import os
import json
+import datetime
def build_page(eatable_rows, consumption_rows, eatables_selection, day_rows):
return """<html>
<form action="/" method="POST">
<td><input name="update" type="submit" value="update" /></td>
<table>
-<tr><th>title</th><th>calories</th><th>sugar (g)</th><th>standard weight (g)</th><th>comments</th><th>delete</th></tr>
-{eatable_rows}
+<tr><th>eatable</th><th>unit count</th><th>unit weight (g)</th><th>calories</th><th>sugar (g)</th></tr>
+{consumption_rows}
<tr>
-<th>add:</th>
+<th>add from DB:</th>
</tr>
<tr>
-<td><input name="title" type="text" value="" /></td>
-<td><input name="cals" type="number" min="0" step="0.1" value="0" /></td>
-<td><input name="sugar_g" type="number" min="0" step="0.1" value="0" /></td>
-<td><input name="standard_g" type="number" min="1" step="0.1" value="1" /></td>
-<td><input name="comments" type="text" value="" /></td>
+<td><select name="eatable_key">{eatables_selection}</select></td>
+<td><input class="unit_count" name="unit_count" type="number" step="1" min="0" value="0" /></td>
+<td></td>
</tr>
</table>
<table>
{day_rows}
</table>
<table>
-<tr><th>eatable</th><th>unit count</th><th>unit weight (g)</th><th>calories</th><th>sugar (g)</th></tr>
-{consumption_rows}
+<tr><th>title</th><th>calories</th><th>sugar (g)</th><th>standard weight (g)</th><th>comments</th><th>delete</th></tr>
+{eatable_rows}
<tr>
-<th>add from DB:</th>
+<th>add:</th>
</tr>
<tr>
-<td><select name="eatable_key">{eatables_selection}</select></td>
-<td><input name="unit_count" type="number" step="1" min="0" value="0" /></td>
-<td></td>
+<td><input name="title" type="text" value="" /></td>
+<td><input name="cals" type="number" min="0" step="0.1" value="0" /></td>
+<td><input name="sugar_g" type="number" min="0" step="0.1" value="0" /></td>
+<td><input name="standard_g" type="number" min="1" step="0.1" value="1" /></td>
+<td><input name="comments" type="text" value="" /></td>
</tr>
</table>
</form>
</body>
-</html>"""
+<script>
+""" + """
+var unit_count_inputs = document.getElementsByClassName("unit_count");
+for (let i = 0; i < unit_count_inputs.length; i++) {
+ let input = unit_count_inputs[i];
+ let button = document.createElement('button');
+ button.innerHTML = '+1';
+ button.onclick = function(event) {
+ event.preventDefault();
+ input.value = parseInt(input.value) + 1;
+ };
+ input.insertAdjacentElement('afterend', button);
+}
+
+</script>
+</html>
+"""
class LockFileDetected(Exception):
pass
return {"cals": calories, "sugar": sugar_g }
def eatables_selection(self, selection=None):
- html = '' # if selection else '<option value="" />'
- for k,v in self.eatables.items():
+ html = ''
+ for k, v in sorted(self.eatables.items(), key=lambda item: item[1].title):
+ v = self.eatables[k]
selected = ' selected' if k==selection else ''
html += '<option value="%s"%s>%s</option>' % (k, selected, v.title)
return html
def add_consumption(self, consumption):
self.consumptions += [consumption]
- def add_day(self, date, day):
+ def add_day(self, date, day, archives_today=False):
+ if archives_today:
+ date = date + str(datetime.datetime.now())[10:]
self.days[date] = day
def set_today_date(self, today_date):
def do_POST(self):
from uuid import uuid4
from urllib.parse import parse_qs
- import datetime
length = int(self.headers['content-length'])
postvars = parse_qs(self.rfile.read(length).decode(), keep_blank_values=1)
db = Database(False)
if 'archive_day' in postvars.keys():
new_cals = postvars["new_day_cals"][0]
new_sugar = postvars["new_day_sugar"][0]
- db.add_day(db.today_date, Day(float(new_cals), float(new_sugar)))
- db.set_today_date(str(datetime.datetime.now()))#[:10])
+ db.add_day(db.today_date, Day(float(new_cals), float(new_sugar)), archives_today=True)
+ db.set_today_date(str(datetime.datetime.now())[:10])
db.consumptions = []
try:
db.write()
r = db.calc_consumption(c)
consumptions += "<tr />"\
"<td><select name=\"eatable_key\">%s</select></td>"\
- "<td><input name=\"unit_count\" type=\"number\" min=\"0\" value=\"%d\" /></td>"\
+ "<td><input class=\"unit_count\" name=\"unit_count\" type=\"number\" min=\"0\" value=\"%d\" /></td>"\
"<td></td>"\
"<td>%.1f</td>"\
"<td>%.1f</td>"\
hostName = "localhost"
-serverPort = 8080
+serverPort = 8081
if __name__ == "__main__":
webServer = HTTPServer((hostName, serverPort), MyServer)
print("Server started http://%s:%s" % (hostName, serverPort))