poprawki w user_expenses

This commit is contained in:
Mateusz Gruszczyński
2025-07-25 10:53:50 +02:00
parent 0d5b170cac
commit bb667a2cbd
3 changed files with 44 additions and 18 deletions

44
app.py
View File

@@ -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,
)