better handling of updating units and quantities
This commit is contained in:
@@ -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/<listid>", 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 = []
|
||||
|
||||
Reference in New Issue
Block a user