diff --git a/expenses_manager/expenses/templates/expenses/dashboard.html b/expenses_manager/expenses/templates/expenses/dashboard.html index 6bf2365..360bb7b 100644 --- a/expenses_manager/expenses/templates/expenses/dashboard.html +++ b/expenses_manager/expenses/templates/expenses/dashboard.html @@ -1,4 +1,5 @@ +

Dashboard

Gastos por categoría

@@ -9,7 +10,38 @@
  • No hay datos
  • {% endfor %} + +
    + + + + + + + +
    + + {% for row in by_month %}
  • {{ row.month }} → {{ row.total }}
  • {% endfor %} + \ No newline at end of file diff --git a/expenses_manager/expenses/views.py b/expenses_manager/expenses/views.py index 5ba8855..75f40f7 100644 --- a/expenses_manager/expenses/views.py +++ b/expenses_manager/expenses/views.py @@ -5,7 +5,7 @@ from .forms import ExpenseForm from django.db.models import Sum from django.contrib.auth import login from django.utils.ipv6 import is_valid_ipv6_address -from django.db.models.functions import ExtractMonth +from django.db.models.functions import ExtractMonth, ExtractYear from django.contrib.auth.decorators import login_required from django.shortcuts import get_object_or_404, render, redirect @@ -108,15 +108,16 @@ def dashboard(request): # Get year, if is empty, use current year year = request.GET.get('year') - year_filter = year if year else date.today().year - expenses = expenses.filter(date__year=year_filter) + expenses_by_yearmonth = expenses + if year: + expenses_by_yearmonth = expenses_by_yearmonth.filter(date__year=year) month = request.GET.get('month') if month: - expenses = expenses.filter(date__month=month) + expenses_by_yearmonth = expenses_by_yearmonth.filter(date__month=month) by_month = ( - expenses + expenses_by_yearmonth .annotate(month=ExtractMonth('date')) .values('month') .annotate(total=Sum('amount')) @@ -131,7 +132,22 @@ def dashboard(request): for row in by_month ] + year_list = ( + expenses + .annotate(year=ExtractYear('date')) + .values('year') + .distinct() + .order_by('year') + ) + months = list(range(1, 13)) + + + # Send the data to the dashboard return render(request, 'expenses/dashboard.html', { 'by_category': by_category, - 'by_month': by_month_display + 'by_month': by_month_display, + 'year_list': year_list, + 'months': months, + 'selected_year': year, + 'selected_month': month, }) \ No newline at end of file