Clear a little the code
This commit is contained in:
parent
1fe1311ca1
commit
8b8e2d0622
@ -25,6 +25,12 @@ MONTHS = {
|
||||
12:'DICIEMBRE'
|
||||
}
|
||||
|
||||
def get_int(value):
|
||||
try:
|
||||
return int(value)
|
||||
except (TypeError, ValueError):
|
||||
return None
|
||||
|
||||
@login_required
|
||||
def expense_list(request):
|
||||
expenses = Expense.objects.filter(owner=request.user)
|
||||
@ -102,40 +108,48 @@ def dashboard(request):
|
||||
|
||||
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'))
|
||||
|
||||
current_year = date.today()
|
||||
|
||||
selected_year = year or current_year
|
||||
selected_month = month
|
||||
|
||||
expenses_filtered = expenses.filter(date__year=selected_year)
|
||||
|
||||
if selected_month:
|
||||
expenses_filtered = expenses_filtered.filter(date__month=selected_month)
|
||||
|
||||
# Querysets
|
||||
by_category = (expenses.values('category__name').annotate(total=Sum('amount')))
|
||||
# By Category
|
||||
by_category = (
|
||||
expenses_filtered
|
||||
.values('category__name')
|
||||
.annotate(total=Sum('amount'))
|
||||
.order_by('category__name')
|
||||
)
|
||||
|
||||
# Get year, if is empty, use current year
|
||||
year = request.GET.get('year')
|
||||
|
||||
expenses_by_yearmonth = expenses
|
||||
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 = (
|
||||
expenses_by_yearmonth
|
||||
by_month_qs = (
|
||||
expenses_filtered
|
||||
.annotate(month=ExtractMonth('date'))
|
||||
.values('month')
|
||||
.annotate(total=Sum('amount'))
|
||||
.order_by('month')
|
||||
)
|
||||
|
||||
by_month_display = [
|
||||
by_month = [
|
||||
{
|
||||
'month': MONTHS[row['month']],
|
||||
'total': row['total'],
|
||||
}
|
||||
for row in by_month
|
||||
for row in by_month_qs
|
||||
]
|
||||
|
||||
year_list = (
|
||||
expenses
|
||||
.annotate(year=ExtractYear('date'))
|
||||
.values('year')
|
||||
.values_list('year', float=True)
|
||||
.distinct()
|
||||
.order_by('year')
|
||||
)
|
||||
@ -145,18 +159,18 @@ def dashboard(request):
|
||||
# Prepare data fot ploting in chart
|
||||
chart_labels = []
|
||||
chart_totals = []
|
||||
for month_data in by_month_display:
|
||||
for month_data in by_month:
|
||||
chart_labels.append(month_data['month'])
|
||||
chart_totals.append(float(month_data['total']))
|
||||
|
||||
# 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,
|
||||
'chart_labels': chart_labels,
|
||||
'chart_data': chart_totals,
|
||||
'year_list': year_list,
|
||||
'months': months,
|
||||
'selected_year': year,
|
||||
'selected_month': month,
|
||||
'chart_labels': chart_labels,
|
||||
'chart_data': chart_totals
|
||||
'months': months,
|
||||
})
|
||||
Loading…
Reference in New Issue
Block a user