From 9013d488837a0296a97278428c4b8b87f96dd1d7 Mon Sep 17 00:00:00 2001 From: Ignace Date: Sat, 3 Jan 2026 15:44:42 +0100 Subject: [PATCH] better handling of updating units and quantities --- items.py | 145 +++++++++++++++++++++--------------- templates/items_append.html | 2 +- templates/items_show.html | 2 +- 3 files changed, 85 insertions(+), 64 deletions(-) diff --git a/items.py b/items.py index d953f58..07310b4 100644 --- a/items.py +++ b/items.py @@ -30,6 +30,88 @@ def item_update(itemid, itemchecked): db.session.commit() return '', 204 +def interpret_and_add_item(listid, newlabel): + # could be + # appeltjes + # appel goudr + #. appel zilver 4 + #. appel zilver 4kratten + print(newlabel) + unit = '' + quantity = '' + if ' ' in newlabel: + # could be: + # paard 5 + # paard 5 poten + # 3 paarden + # 3kilo schaap + # 3 kilo schaap + # take the last word + words = newlabel.split(' ') + # -1- if there is a digit in the last word + if any(char.isdigit() for char in words[-1]): + # ... paard 5 + # ... paard 5poten + for char in words[-1]: + if char.isdigit(): + quantity += char + else: + unit += char + newlabel = ' '.join(words[0:-1]) + elif len(words)>2 and any(char.isdigit() for char in words[-2]): + # ... paard 5 poten + # ... paard 4keer poten + print(words) + for char in words[-2]: + if char.isdigit(): + quantity += char + else: + unit += char + unit = words[-1] if unit=='' else unit+' '+words[-1] + newlabel = ' '.join(words[0:-2]) + elif any(char.isdigit() for char in words[0]): + # 5 paarden + # 5koppig paarden + for char in words[0]: + if char.isdigit(): + quantity += char + else: + unit += char + newlabel = ' '.join(words[1:]) + if unit=='': + if len(words)>2 and len(words[1])<=4: + unit = words[1] + newlabel = ' '.join(words[2:]) + if unit == '': + unit = 'x' + if quantity == '': + quantity = 1 + + newlabel=newlabel.title() + # check if the item exists already + item = Item.query.filter_by(listofitems_id=listid, label=newlabel, unit=unit).first() + if item: + print(item.unit) + print(unit) + if item.is_checked: + item.quantity += int(quantity) + if item.is_suggestion: + item.quantity = quantity + item.is_suggestion = False + item.is_checked = False + else: + # create a new one + item=Item(listofitems_id=listid, label=newlabel) + if unit: + item.unit = unit + if quantity: + item.quantity = int(quantity) + db.session.add(item) + db.session.commit() + + return item + + # user adds items to list @items_bp.route("/items_append/", methods=["GET", "POST"]) @login_required @@ -37,68 +119,7 @@ def items_append(listid): if request.method == "POST": # a new item has been added newlabel = request.form["newItem"] - # could be - # appeltjes - # appel goudr - #. appel zilver 4 - #. appel zilver 4kratten - print(newlabel) - unit = '' - quantity = '' - if ' ' in newlabel: - # could be: - # paard 5 - # paard 5 poten - # 3 paarden - # 3kilo schaap - # 3 kilo schaap - # take the last word - words = newlabel.split(' ') - # -1- if there is a digit in the last word - if any(char.isdigit() for char in words[-1]): - # ... paard 5 - # ... paard 5poten - for char in words[-1]: - if char.isdigit(): - quantity += char - else: - unit += char - newlabel = ' '.join(words[0:-1]) - elif len(words)>2 and any(char.isdigit() for char in words[-2]): - # ... paard 5 poten - # ... paard 4keer poten - print(words) - for char in words[-2]: - if char.isdigit(): - quantity += char - else: - unit += char - unit = words[-1] if unit=='' else unit+' '+words[-1] - newlabel = ' '.join(words[0:-2]) - elif any(char.isdigit() for char in words[0]): - # 5 paarden - # 5koppig paarden - for char in words[0]: - if char.isdigit(): - quantity += char - else: - unit += char - newlabel = ' '.join(words[1:]) - if unit=='': - if len(words)>2 and len(words[1])<=4: - unit = words[1] - newlabel = ' '.join(words[2:]) - if unit == '': - unit = 'x' - - newlabel=newlabel.title() - newitem=Item(listofitems_id=listid, label=newlabel) - if unit: - newitem.unit = unit - if quantity: - newitem.quantity = int(quantity) - db.session.add(newitem) - db.session.commit() + interpret_and_add_item(listid, newlabel) # read icons icon_names = [] diff --git a/templates/items_append.html b/templates/items_append.html index eb32047..497b484 100644 --- a/templates/items_append.html +++ b/templates/items_append.html @@ -62,7 +62,7 @@ $(document).ready(function(){  +  {{ item.label }} - {% if item.quantity %} + {% if item.quantity > 1 or item.unit != 'x' %} {{ item.quantity }}{{ item.unit }}     {% endif %} diff --git a/templates/items_show.html b/templates/items_show.html index 61f468f..347eace 100644 --- a/templates/items_show.html +++ b/templates/items_show.html @@ -49,7 +49,7 @@ {{ item.label }}
- {% if item.quantity > 1 %} + {% if item.quantity > 1 or item.unit != 'x' %} {{ item.quantity }}{{ item.unit }} {% endif %} ...