Merge pull request 'Fixed minor bugs and refactor code' (#8) from dev into main

Reviewed-on: #8
This commit is contained in:
jkuijperm 2026-05-07 13:32:22 +00:00
commit 89f0ec3c95

View File

@ -292,64 +292,45 @@ def dashboard(request):
# ------------------
year = _get_int(request.GET.get("year"))
month = _get_int(request.GET.get("month"))
period = request.GET.get("period")
current_year = date.today().year
period = request.GET.get("period")
account_id = _get_int(request.GET.get("account"))
accounts = Account.objects.filter(
owner=request.user,
active=True,
)
compare_enabled = request.GET.get("compare") == "1"
today = date.today()
current_year = today.year
accounts = Account.objects.filter(owner=request.user, active=True)
selected_year = year or current_year
selected_month = month
compare_enabled = request.GET.get("compare") == "1"
# ------------------
# Queryset base
# -----------------
expenses = Expense.objects.filter(owner=request.user)
selected_account_obj = None
kpi_balance = None
if account_id:
expenses = expenses.filter(account_id=account_id)
selected_account_obj = accounts.filter(id=account_id).first()
kpi_balance = (
selected_account_obj.current_balance() if selected_account_obj else 0
)
if period == "this_month":
selected_year, selected_month = today.year, today.month
elif period == "last_month":
selected_year, selected_month = sub_months(today.year, today.month, 1)
elif period == "this_year":
selected_year, selected_month = today.year, None
if selected_account_obj:
kpi_balance = selected_account_obj.current_balance()
else:
kpi_balance = sum(account.current_balance() for account in accounts)
today = date.today()
if period == "this_month":
selected_year = today.year
selected_month = today.month
elif period == "last_month":
if today.month == 1:
selected_year = today.year - 1
selected_month = 12
else:
selected_year = today.year
selected_month = today.month
elif period == "this_year":
selected_year = today.year
selected_month = None
expenses_filtered = expenses.filter(date__year=selected_year)
if selected_month:
expenses_filtered = expenses_filtered.filter(date__month=selected_month)
total_amount = expenses_filtered.aggregate(total=Sum("amount"))["total"] or 0
expense_count = expenses_filtered.count()
category_count = expenses_filtered.values("category").distinct().count()
# ------------------
@ -372,16 +353,8 @@ def dashboard(request):
)
month_totals = {row["month"]: float(row["total"]) for row in by_month_qs}
months = list(range(1, 13))
by_month = [
{
"month": m,
"total": month_totals.get(m, 0),
}
for m in months
]
by_month = [{"month": m, "total": month_totals.get(m, 0)}for m in months]
# ------------------
# Availables years
@ -414,17 +387,10 @@ def dashboard(request):
if compare_enabled:
previous_expenses = Expense.objects.filter(owner=request.user)
if account_id:
previous_expenses.filter(account_id=account_id)
previous_expenses = previous_expenses.filter(account_id=account_id)
if selected_month:
# Monthly compare
if selected_month == 1:
prev_year = selected_year - 1
prev_month = 12
else:
prev_year = selected_year
prev_month = selected_month - 1
prev_year, prev_month = sub_months(selected_year, selected_month, 1)
previous_expenses = previous_expenses.filter(
date__year=prev_year, date__month=prev_month
)
@ -434,13 +400,11 @@ def dashboard(request):
previous_expenses = previous_expenses.filter(date__year=prev_year)
previous_total = previous_expenses.aggregate(total=Sum("amount"))["total"] or 0
kpi_difference = total_amount - previous_total
if previous_total:
percentage = (kpi_difference / previous_total) * 100
kpi_trend = None
if kpi_difference is not None:
if kpi_difference > 0:
kpi_trend = "up"
@ -448,29 +412,24 @@ def dashboard(request):
kpi_trend = "down"
else:
kpi_trend = "equal"
kpi_difference_abs = abs(kpi_difference) if kpi_difference is not None else None
# ------------------
# Previous expenses by category
# ------------------
previous_by_category = previous_expenses.values("category__name").annotate(
total=Sum("amount")
)
previous_by_category = previous_expenses.values("category__name").annotate(total=Sum("amount"))
current_map = {row["category__name"]: row["total"] for row in by_category}
previous_map = {
row["category__name"]: row["total"] for row in previous_by_category
}
all_categories = set(current_map.keys()) | set(previous_map.keys())
previous_map = {row["category__name"]: row["total"] for row in previous_by_category}
all_categories = set(current_map) | set(previous_map)
category_comparison = []
for category in all_categories:
current_total = current_map.get(category, 0)
previous_total_cat = previous_map.get(category, 0)
difference = current_total - previous_total_cat
category_comparison.append(
@ -490,7 +449,6 @@ def dashboard(request):
for account in accounts:
monthly_data = account.monthly_balance(selected_year)
m_balance = [row["balance"] for row in monthly_data]
accounts_charts.append(
{
"id": account.id,