better handling of updating units and quantities
This commit is contained in:
@@ -30,6 +30,88 @@ def item_update(itemid, itemchecked):
|
|||||||
db.session.commit()
|
db.session.commit()
|
||||||
return '', 204
|
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
|
# user adds items to list
|
||||||
@items_bp.route("/items_append/<listid>", methods=["GET", "POST"])
|
@items_bp.route("/items_append/<listid>", methods=["GET", "POST"])
|
||||||
@login_required
|
@login_required
|
||||||
@@ -37,68 +119,7 @@ def items_append(listid):
|
|||||||
if request.method == "POST":
|
if request.method == "POST":
|
||||||
# a new item has been added
|
# a new item has been added
|
||||||
newlabel = request.form["newItem"]
|
newlabel = request.form["newItem"]
|
||||||
# could be
|
interpret_and_add_item(listid, newlabel)
|
||||||
# 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()
|
|
||||||
|
|
||||||
# read icons
|
# read icons
|
||||||
icon_names = []
|
icon_names = []
|
||||||
|
|||||||
@@ -62,7 +62,7 @@ $(document).ready(function(){
|
|||||||
<a class="btn btn-{% if item.is_suggestion %}primary{% else %}info{% endif %} btn-sm" name="{{ item.id }}" onclick='handleAddOne(this);'> + </a>
|
<a class="btn btn-{% if item.is_suggestion %}primary{% else %}info{% endif %} btn-sm" name="{{ item.id }}" onclick='handleAddOne(this);'> + </a>
|
||||||
{{ item.label }}</td>
|
{{ item.label }}</td>
|
||||||
<td style="text-align: right">
|
<td style="text-align: right">
|
||||||
{% if item.quantity %}
|
{% if item.quantity > 1 or item.unit != 'x' %}
|
||||||
<a class="btn btn-outline-primary btn-sm" name="{{ item.id }}" onclick='handleAddWithQuantity(this, {{ item.quantity }} );'>{{ item.quantity }}{{ item.unit }}</a>
|
<a class="btn btn-outline-primary btn-sm" name="{{ item.id }}" onclick='handleAddWithQuantity(this, {{ item.quantity }} );'>{{ item.quantity }}{{ item.unit }}</a>
|
||||||
|
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|||||||
@@ -49,7 +49,7 @@
|
|||||||
{{ item.label }}
|
{{ item.label }}
|
||||||
</div>
|
</div>
|
||||||
<div>
|
<div>
|
||||||
{% if item.quantity > 1 %}
|
{% if item.quantity > 1 or item.unit != 'x' %}
|
||||||
{{ item.quantity }}{{ item.unit }}
|
{{ item.quantity }}{{ item.unit }}
|
||||||
{% endif %}
|
{% endif %}
|
||||||
<img src="/static/categories/{{ item.category }}.svg" style="height: 2em;" alt="...">
|
<img src="/static/categories/{{ item.category }}.svg" style="height: 2em;" alt="...">
|
||||||
|
|||||||
Reference in New Issue
Block a user