paginacja i poprawki uxowe
This commit is contained in:
51
app.py
51
app.py
@@ -60,6 +60,8 @@ import pytesseract
|
||||
from pytesseract import Output
|
||||
import logging
|
||||
|
||||
from types import SimpleNamespace
|
||||
|
||||
app = Flask(__name__)
|
||||
app.config.from_object(Config)
|
||||
|
||||
@@ -2837,31 +2839,41 @@ def list_products():
|
||||
per_page = request.args.get("per_page", 125, type=int)
|
||||
per_page = max(1, min(per_page, 300))
|
||||
|
||||
items_query = Item.query.options(
|
||||
# Pobierz wszystkie itemy – tylko name, id i relacje potrzebne do odtworzenia unikalnych
|
||||
all_items = Item.query.options(
|
||||
joinedload(Item.shopping_list),
|
||||
joinedload(Item.added_by_user),
|
||||
).order_by(Item.id.desc())
|
||||
joinedload(Item.added_by_user)
|
||||
).order_by(Item.id.desc()).all()
|
||||
|
||||
pagination = items_query.paginate(page=page, per_page=per_page, error_out=False)
|
||||
items = pagination.items
|
||||
seen_names = set()
|
||||
unique_items = []
|
||||
for item in all_items:
|
||||
key = (item.name or "").strip().lower()
|
||||
if key not in seen_names:
|
||||
unique_items.append(item)
|
||||
seen_names.add(key)
|
||||
|
||||
item_names_subquery = db.session.query(
|
||||
func.lower(func.trim(Item.name)).label("normalized_name")
|
||||
).distinct().subquery()
|
||||
# Paginacja ręczna na unikalnych
|
||||
total_items = len(unique_items)
|
||||
total_pages = (total_items + per_page - 1) // per_page
|
||||
start = (page - 1) * per_page
|
||||
end = start + per_page
|
||||
items = unique_items[start:end]
|
||||
|
||||
suggestions = SuggestedProduct.query.order_by(SuggestedProduct.name.asc()).all()
|
||||
# Słownik użytkowników (ograniczony do używanych)
|
||||
user_ids = {item.added_by for item in items if item.added_by}
|
||||
users = User.query.filter(User.id.in_(user_ids)).all()
|
||||
users_dict = {u.id: u.username for u in users}
|
||||
|
||||
# Wszystkie sugestie – do dopasowań
|
||||
suggestions = SuggestedProduct.query.all()
|
||||
all_suggestions_dict = {
|
||||
(s.name or "").strip().lower(): s
|
||||
for s in suggestions
|
||||
if s.name and s.name.strip()
|
||||
(s.name or "").strip().lower(): s for s in suggestions if s.name and s.name.strip()
|
||||
}
|
||||
|
||||
used_suggestion_names = {
|
||||
row.normalized_name for row in db.session.query(item_names_subquery)
|
||||
if row.normalized_name
|
||||
}
|
||||
used_suggestion_names = {(item.name or "").strip().lower() for item in unique_items}
|
||||
|
||||
# Powiązane i osierocone
|
||||
suggestions_dict = {
|
||||
name: all_suggestions_dict[name]
|
||||
for name in used_suggestion_names
|
||||
@@ -2873,9 +2885,6 @@ def list_products():
|
||||
if name not in used_suggestion_names
|
||||
]
|
||||
|
||||
users = User.query.options(load_only(User.id, User.username)).all()
|
||||
users_dict = {user.id: user.username for user in users}
|
||||
|
||||
query_string = urlencode({k: v for k, v in request.args.items() if k != "page"})
|
||||
|
||||
return render_template(
|
||||
@@ -2886,13 +2895,11 @@ def list_products():
|
||||
orphan_suggestions=orphan_suggestions,
|
||||
page=page,
|
||||
per_page=per_page,
|
||||
total_pages=pagination.pages,
|
||||
total_pages=total_pages,
|
||||
query_string=query_string,
|
||||
)
|
||||
|
||||
|
||||
|
||||
|
||||
@app.route("/admin/sync_suggestion/<int:item_id>", methods=["POST"])
|
||||
@login_required
|
||||
def sync_suggestion_ajax(item_id):
|
||||
|
Reference in New Issue
Block a user