porządkowanie kodu i poprawki js
This commit is contained in:
72
app.py
72
app.py
@@ -1543,7 +1543,7 @@ def handle_disconnect(sid):
|
||||
@socketio.on("add_item")
|
||||
def handle_add_item(data):
|
||||
list_id = data["list_id"]
|
||||
name = data["name"]
|
||||
name = data["name"].strip()
|
||||
quantity = data.get("quantity", 1)
|
||||
|
||||
try:
|
||||
@@ -1553,34 +1553,56 @@ def handle_add_item(data):
|
||||
except:
|
||||
quantity = 1
|
||||
|
||||
new_item = Item(
|
||||
list_id=list_id,
|
||||
name=name,
|
||||
quantity=quantity,
|
||||
added_by=current_user.id if current_user.is_authenticated else None,
|
||||
)
|
||||
db.session.add(new_item)
|
||||
# Szukamy istniejącego itemu w tej liście (ignorując wielkość liter)
|
||||
existing_item = Item.query.filter(
|
||||
Item.list_id == list_id,
|
||||
func.lower(Item.name) == name.lower(),
|
||||
Item.not_purchased == False
|
||||
).first()
|
||||
|
||||
if not SuggestedProduct.query.filter_by(name=name).first():
|
||||
new_suggestion = SuggestedProduct(name=name)
|
||||
db.session.add(new_suggestion)
|
||||
if existing_item:
|
||||
existing_item.quantity += quantity
|
||||
db.session.commit()
|
||||
|
||||
db.session.commit()
|
||||
emit(
|
||||
"item_edited",
|
||||
{
|
||||
"item_id": existing_item.id,
|
||||
"new_name": existing_item.name,
|
||||
"new_quantity": existing_item.quantity
|
||||
},
|
||||
to=str(list_id),
|
||||
)
|
||||
else:
|
||||
new_item = Item(
|
||||
list_id=list_id,
|
||||
name=name,
|
||||
quantity=quantity,
|
||||
added_by=current_user.id if current_user.is_authenticated else None,
|
||||
)
|
||||
db.session.add(new_item)
|
||||
|
||||
emit(
|
||||
"item_added",
|
||||
{
|
||||
"id": new_item.id,
|
||||
"name": new_item.name,
|
||||
"quantity": new_item.quantity,
|
||||
"added_by": (
|
||||
current_user.username if current_user.is_authenticated else "Gość"
|
||||
),
|
||||
},
|
||||
to=str(list_id),
|
||||
include_self=True,
|
||||
)
|
||||
if not SuggestedProduct.query.filter(func.lower(SuggestedProduct.name) == name.lower()).first():
|
||||
new_suggestion = SuggestedProduct(name=name)
|
||||
db.session.add(new_suggestion)
|
||||
|
||||
db.session.commit()
|
||||
|
||||
emit(
|
||||
"item_added",
|
||||
{
|
||||
"id": new_item.id,
|
||||
"name": new_item.name,
|
||||
"quantity": new_item.quantity,
|
||||
"added_by": (
|
||||
current_user.username if current_user.is_authenticated else "Gość"
|
||||
),
|
||||
},
|
||||
to=str(list_id),
|
||||
include_self=True,
|
||||
)
|
||||
|
||||
# Aktualizacja postępu
|
||||
purchased_count, total_count, percent = get_progress(list_id)
|
||||
|
||||
emit(
|
||||
|
Reference in New Issue
Block a user