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):

View File

@@ -9,8 +9,8 @@ document.addEventListener('DOMContentLoaded', function () {
return str ? str.normalize("NFD").replace(/[\u0300-\u036f]/g, "").toLowerCase() : '';
}
let sortMode = 'popularity'; // 'popularity' lub 'alphabetical'
let limit = 50;
let sortMode = 'popularity';
let limit = 150;
let offset = 0;
let loading = false;
let reachedEnd = false;
@@ -20,7 +20,7 @@ document.addEventListener('DOMContentLoaded', function () {
function renderSortBar() {
if (!sortBar) return;
sortBar.innerHTML = `
<a href="#" id="sort-popularity" ${sortMode === "popularity" ? 'style="font-weight:bold"' : ''}>Sortuj: Popularność</a> |
Sortuj: <a href="#" id="sort-popularity" ${sortMode === "popularity" ? 'style="font-weight:bold"' : ''}>Popularność</a> |
<a href="#" id="sort-alphabetical" ${sortMode === "alphabetical" ? 'style="font-weight:bold"' : ''}>Alfabetycznie</a>
`;
document.getElementById('sort-popularity').onclick = (e) => {
@@ -152,8 +152,10 @@ document.addEventListener('DOMContentLoaded', function () {
}
// Infinite scroll
modal.addEventListener('scroll', function () {
if (!loading && !reachedEnd && (modal.scrollTop + modal.offsetHeight > modal.scrollHeight - 80)) {
const modalBody = modal.querySelector('.modal-body');
modalBody.addEventListener('scroll', function () {
if (!loading && !reachedEnd && (modalBody.scrollTop + modalBody.clientHeight > modalBody.scrollHeight - 80)) {
fetchProducts(false);
}
});