This commit is contained in:
Mateusz Gruszczyński
2025-08-16 13:10:21 +02:00
parent e3b180fba7
commit b6502fedfc
2 changed files with 12 additions and 20 deletions

20
app.py
View File

@@ -1951,42 +1951,32 @@ def suggest_products():
@app.route("/all_products")
def all_products():
# Parametry
sort = request.args.get("sort", "popularity")
limit = request.args.get("limit", type=int) or 100
offset = request.args.get("offset", type=int) or 0
# Wyciągnięcie wszystkich użyć produktów z Item + agregacja (uniwersalność z polskimi znakami i wielkością liter!)
normalized_name = func.lower(func.trim(func.replace(Item.name, ' ', ' ')))
item_usage = db.session.query(
func.lower(Item.name).label("name"),
normalized_name.label("name"),
func.sum(Item.quantity).label("count")
).group_by(func.lower(Item.name)).all()
).group_by(normalized_name).all()
usage_map = {row.name: row.count for row in item_usage}
# Unikalne nazwy
all_names = sorted(usage_map.keys())
# Sortowanie
if sort == "popularity":
sorted_names = sorted(all_names, key=lambda name: (-usage_map[name], name))
else: # alphabetical
else:
sorted_names = sorted(all_names)
# Paginacja
paged_names = sorted_names[offset:offset+limit]
# Dla każdego produktu zwróć dict: nazwa i liczba użyć (możesz użyć tylko nazw jeśli chcesz jak dawniej)
products = [{"name": name, "count": usage_map[name]} for name in paged_names]
# Ilość wszystkich dostępnych produktów
product_count = len(all_names)
return jsonify({"products": products, "total_count": product_count})
@app.route("/upload_receipt/<int:list_id>", methods=["POST"])
@login_required
def upload_receipt(list_id):