Clear a little the code

This commit is contained in:
JKuijperM 2026-01-21 14:59:00 +01:00
parent 1fe1311ca1
commit 8b8e2d0622

View File

@ -25,6 +25,12 @@ MONTHS = {
12:'DICIEMBRE' 12:'DICIEMBRE'
} }
def get_int(value):
try:
return int(value)
except (TypeError, ValueError):
return None
@login_required @login_required
def expense_list(request): def expense_list(request):
expenses = Expense.objects.filter(owner=request.user) expenses = Expense.objects.filter(owner=request.user)
@ -102,40 +108,48 @@ def dashboard(request):
expenses = Expense.objects.filter(owner=request.user) expenses = Expense.objects.filter(owner=request.user)
# Apply filters by year and month
year = get_int(request.GET.get('year'))
month = get_int(request.GET.get('month'))
# Querysets current_year = date.today()
by_category = (expenses.values('category__name').annotate(total=Sum('amount')))
# Get year, if is empty, use current year selected_year = year or current_year
year = request.GET.get('year') selected_month = month
expenses_by_yearmonth = expenses expenses_filtered = expenses.filter(date__year=selected_year)
if year:
expenses_by_yearmonth = expenses_by_yearmonth.filter(date__year=year)
month = request.GET.get('month')
if month:
expenses_by_yearmonth = expenses_by_yearmonth.filter(date__month=month)
by_month = ( if selected_month:
expenses_by_yearmonth expenses_filtered = expenses_filtered.filter(date__month=selected_month)
# Querysets
# By Category
by_category = (
expenses_filtered
.values('category__name')
.annotate(total=Sum('amount'))
.order_by('category__name')
)
by_month_qs = (
expenses_filtered
.annotate(month=ExtractMonth('date')) .annotate(month=ExtractMonth('date'))
.values('month') .values('month')
.annotate(total=Sum('amount')) .annotate(total=Sum('amount'))
.order_by('month')
) )
by_month_display = [ by_month = [
{ {
'month': MONTHS[row['month']], 'month': MONTHS[row['month']],
'total': row['total'], 'total': row['total'],
} }
for row in by_month for row in by_month_qs
] ]
year_list = ( year_list = (
expenses expenses
.annotate(year=ExtractYear('date')) .annotate(year=ExtractYear('date'))
.values('year') .values_list('year', float=True)
.distinct() .distinct()
.order_by('year') .order_by('year')
) )
@ -145,18 +159,18 @@ def dashboard(request):
# Prepare data fot ploting in chart # Prepare data fot ploting in chart
chart_labels = [] chart_labels = []
chart_totals = [] chart_totals = []
for month_data in by_month_display: for month_data in by_month:
chart_labels.append(month_data['month']) chart_labels.append(month_data['month'])
chart_totals.append(float(month_data['total'])) chart_totals.append(float(month_data['total']))
# Send the data to the dashboard # Send the data to the dashboard
return render(request, 'expenses/dashboard.html', { return render(request, 'expenses/dashboard.html', {
'by_category': by_category, 'by_category': by_category,
'by_month': by_month_display, 'by_month': by_month,
'chart_labels': chart_labels,
'chart_data': chart_totals,
'year_list': year_list, 'year_list': year_list,
'months': months,
'selected_year': year, 'selected_year': year,
'selected_month': month, 'selected_month': month,
'chart_labels': chart_labels, 'months': months,
'chart_data': chart_totals
}) })