diff --git a/app.py b/app.py index 6b494e2..6014b47 100644 --- a/app.py +++ b/app.py @@ -958,17 +958,25 @@ def view_list(list_id): def user_expenses(): start_date_str = request.args.get("start_date") end_date_str = request.args.get("end_date") + show_all = request.args.get("show_all", "false").lower() == "true" + start = None end = None - # Przygotowanie podstawowego zapytania o wydatki użytkownika - expenses_query = ( - Expense.query.join(ShoppingList, Expense.list_id == ShoppingList.id) - .options(joinedload(Expense.list)) - .filter(ShoppingList.owner_id == current_user.id) - ) + expenses_query = Expense.query.join( + ShoppingList, Expense.list_id == ShoppingList.id + ).options(joinedload(Expense.list)) + + # Jeśli show_all to False, filtruj tylko po bieżącym użytkowniku + if not show_all: + expenses_query = expenses_query.filter(ShoppingList.owner_id == current_user.id) + else: + expenses_query = expenses_query.filter( + or_( + ShoppingList.owner_id == current_user.id, ShoppingList.is_public == True + ) + ) - # Filtrowanie po zakresie dat, jeśli podano if start_date_str and end_date_str: try: start = datetime.strptime(start_date_str, "%Y-%m-%d") @@ -981,7 +989,13 @@ def user_expenses(): expenses = expenses_query.order_by(Expense.added_at.desc()).all() - # Tabela wydatków + list_ids = {e.list_id for e in expenses} + lists = ( + ShoppingList.query.filter(ShoppingList.id.in_(list_ids)) + .order_by(ShoppingList.created_at.desc()) + .all() + ) + expense_table = [ { "title": e.list.title if e.list else "Nieznana", @@ -991,15 +1005,6 @@ def user_expenses(): for e in expenses ] - # Tylko listy z tych wydatków - list_ids = {e.list_id for e in expenses} - lists = ( - ShoppingList.query.filter(ShoppingList.id.in_(list_ids)) - .order_by(ShoppingList.created_at.desc()) - .all() - ) - - # Lista zsumowanych wydatków per lista (z uwzględnieniem filtra dat) lists_data = [ { "id": l.id, @@ -1016,7 +1021,10 @@ def user_expenses(): ] return render_template( - "user_expenses.html", expense_table=expense_table, lists_data=lists_data + "user_expenses.html", + expense_table=expense_table, + lists_data=lists_data, + show_all=show_all, ) diff --git a/static/js/user_expense_lists.js b/static/js/user_expense_lists.js index 2ceb887..7c7460f 100644 --- a/static/js/user_expense_lists.js +++ b/static/js/user_expense_lists.js @@ -115,6 +115,8 @@ document.addEventListener("DOMContentLoaded", function () { }); toggleBtn.textContent = allChecked ? "🚫 Odznacz wszystkie" : "✅ Zaznacz wszystkie"; + const updateTotalEvent = new Event('change'); + checkboxes.forEach(cb => cb.dispatchEvent(updateTotalEvent)); }); }); @@ -125,4 +127,15 @@ document.getElementById("applyCustomRange")?.addEventListener("click", () => { const url = `/user_expenses?start_date=${start}&end_date=${end}`; window.location.href = url; } +}); + +document.getElementById("showAllLists").addEventListener("change", function () { + const checked = this.checked; + const url = new URL(window.location.href); + if (checked) { + url.searchParams.set("show_all", "true"); + } else { + url.searchParams.delete("show_all"); + } + window.location.href = url.toString(); }); \ No newline at end of file diff --git a/templates/user_expenses.html b/templates/user_expenses.html index 7c3fd3d..17f5852 100644 --- a/templates/user_expenses.html +++ b/templates/user_expenses.html @@ -47,6 +47,11 @@ +
+ + +
Od