X-Git-Url: https://plomlompom.com/repos/template?a=blobdiff_plain;ds=sidebyside;f=calories.py;h=7a4846acdf40b5304fc5cc4ba088de48d16830da;hb=35fe0ad6b60640e86add740c3b49dff05c75a240;hp=0f65aebae5a09b04f90bccef302ce5f90fcdbcdb;hpb=26462d618eea05e4f4f468d34a9ff875ad1ebb4a;p=misc
diff --git a/calories.py b/calories.py
index 0f65aeb..7a4846a 100644
--- a/calories.py
+++ b/calories.py
@@ -24,6 +24,7 @@ td, th {
add from DB: |
+
|
|
|
@@ -55,7 +56,7 @@ 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.innerHTML = '+1';
button.onclick = function(event) {
event.preventDefault();
input.value = parseInt(input.value) + 1;
@@ -72,12 +73,13 @@ class LockFileDetected(Exception):
class Eatable:
- def __init__(self, title, cals, sugar_g, standard_g=100, comments=""):
+ def __init__(self, title, cals, sugar_g, standard_g=100, comments="", popularity=0):
self.title = title
self.cals = cals # per 100g
self.sugar_g = sugar_g # per 100g
self.standard_g = standard_g # common unit weight
- self.comments = comments
+ self.comments = comments
+ self.popularity = popularity
def to_dict(self):
return {
@@ -85,19 +87,22 @@ class Eatable:
"cals": self.cals,
"sugar_g": self.sugar_g,
"standard_g": self.standard_g,
- "comments": self.comments
+ "comments": self.comments,
+ "popularity": self.popularity
}
class Consumption:
- def __init__(self, eatable_key, unit_count=None):
- self.eatable_key = eatable_key
+ def __init__(self, eatable_key, unit_count=None, keep_visible=0):
+ self.eatable_key = eatable_key
self.unit_count = unit_count
+ self.keep_visible = keep_visible
def to_dict(self):
return {
"eatable_key": self.eatable_key,
"unit_count": self.unit_count,
+ "keep_visible": self.keep_visible
}
class Day:
@@ -112,7 +117,7 @@ class Day:
"sugar_g": self.sugar_g,
}
-class Database:
+class Database:
def __init__(self, load_from_file=True):
db_name = "calories_db"
@@ -168,12 +173,12 @@ class Database:
self.eatables[id_] = eatable
def add_consumption(self, consumption):
- self.consumptions += [consumption]
+ self.consumptions += [consumption]
def add_day(self, date, day, archives_today=False):
if archives_today:
date = date + str(datetime.datetime.now())[10:]
- self.days[date] = day
+ self.days[date] = day
def set_today_date(self, today_date):
self.today_date = today_date
@@ -217,23 +222,23 @@ 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)
i = 0
if 'eatable_key' in postvars.keys():
for eatable_key in postvars['eatable_key']:
- c = Consumption(decode("eatable_key", i, False), decode("unit_count", i))
- i += 1
- if c.unit_count == 0:
+ c = Consumption(decode("eatable_key", i, False), decode("unit_count", i), decode("keep_visible", i))
+ i += 1
+ if c.unit_count == 0 and c.keep_visible == 0:
continue
db.add_consumption(c)
i = 0
if 'day_date' in postvars.keys():
for date in postvars['day_date']:
db.add_day((date), Day(decode("day_cals", i), decode("day_sugar", i)))
- i += 1
+ i += 1
if 'new_date' in postvars.keys():
db.set_today_date(postvars["new_date"][0])
if 'archive_day' in postvars.keys():
@@ -241,7 +246,16 @@ class MyServer(BaseHTTPRequestHandler):
new_sugar = postvars["new_day_sugar"][0]
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])
+ for c in db.consumptions:
+ if c.unit_count > 0:
+ db.eatables[c.eatable_key].popularity += 1
db.consumptions = []
+ default_slots = 7
+ for k, v in sorted(db.eatables.items(), key=lambda item: -item[1].popularity):
+ db.add_consumption(Consumption(k, 0))
+ default_slots -= 1
+ if (default_slots <= 0):
+ break
try:
db.write()
self.send_response(302)
@@ -273,6 +287,7 @@ class MyServer(BaseHTTPRequestHandler):
for c in db.consumptions:
r = db.calc_consumption(c)
consumptions += "
"\
+ ""\
" | "\
" | "\
" | "\
@@ -281,12 +296,12 @@ class MyServer(BaseHTTPRequestHandler):
"" % (db.eatables_selection(c.eatable_key), c.unit_count, r["cals"], r["sugar"])
day_rows = ""
for date, day in db.days.items():
- day_rows += ""\
+ day_rows = "
"\
"%s | "\
"%.1f | "\
"%.1f | "\
- "
" % (date, date[:10], day.calories, day.calories, day.sugar_g, day.sugar_g)
- day_rows += ""\
+ "
" % (date, date[:10], day.calories, day.calories, day.sugar_g, day.sugar_g) + day_rows
+ day_rows = ""\
"today: | | | archive? | "\
"
"\
""\
@@ -294,14 +309,14 @@ class MyServer(BaseHTTPRequestHandler):
"%.1f | "\
"%.1f | "\
" | "\
- "
" % (db.today_date, db.today.calories, db.today.calories, db.today.sugar_g, db.today.sugar_g)
+ "" % (db.today_date, db.today.calories, db.today.calories, db.today.sugar_g, db.today.sugar_g) + day_rows
page = build_page(eatables, consumptions, db.eatables_selection(), day_rows)
self.wfile.write(bytes(page, "utf-8"))
hostName = "localhost"
serverPort = 8081
-if __name__ == "__main__":
+if __name__ == "__main__":
webServer = HTTPServer((hostName, serverPort), MyServer)
print("Server started http://%s:%s" % (hostName, serverPort))
try: