Refactor view code
This commit is contained in:
parent
8b8e2d0622
commit
2b643367e7
@ -1,4 +1,5 @@
|
||||
from datetime import date
|
||||
from re import M
|
||||
from .models import Expense
|
||||
from .forms import ExpenseForm
|
||||
|
||||
@ -25,7 +26,7 @@ MONTHS = {
|
||||
12:'DICIEMBRE'
|
||||
}
|
||||
|
||||
def get_int(value):
|
||||
def _get_int(value):
|
||||
try:
|
||||
return int(value)
|
||||
except (TypeError, ValueError):
|
||||
@ -105,25 +106,30 @@ def expense_delete(request, pk):
|
||||
|
||||
@login_required
|
||||
def dashboard(request):
|
||||
# ------------------
|
||||
# Filters
|
||||
# ------------------
|
||||
year = _get_int(request.GET.get('year'))
|
||||
month = _get_int(request.GET.get('month'))
|
||||
|
||||
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()
|
||||
current_year = date.today().year
|
||||
|
||||
selected_year = year or current_year
|
||||
selected_month = month
|
||||
|
||||
# ------------------
|
||||
# Queryset base
|
||||
# -----------------
|
||||
expenses = Expense.objects.filter(owner=request.user)
|
||||
|
||||
expenses_filtered = expenses.filter(date__year=selected_year)
|
||||
|
||||
if selected_month:
|
||||
expenses_filtered = expenses_filtered.filter(date__month=selected_month)
|
||||
|
||||
# Querysets
|
||||
# By Category
|
||||
# ------------------
|
||||
# Totals by category
|
||||
# -----------------
|
||||
by_category = (
|
||||
expenses_filtered
|
||||
.values('category__name')
|
||||
@ -131,6 +137,10 @@ def dashboard(request):
|
||||
.order_by('category__name')
|
||||
)
|
||||
|
||||
# ------------------
|
||||
# Totals by month
|
||||
# -----------------
|
||||
|
||||
by_month_qs = (
|
||||
expenses_filtered
|
||||
.annotate(month=ExtractMonth('date'))
|
||||
@ -138,30 +148,36 @@ def dashboard(request):
|
||||
.annotate(total=Sum('amount'))
|
||||
)
|
||||
|
||||
month_totals = {
|
||||
row['month']: float(row['total'])
|
||||
for row in by_month_qs
|
||||
}
|
||||
|
||||
months = list(range(1, 13))
|
||||
|
||||
by_month = [
|
||||
{
|
||||
'month': MONTHS[row['month']],
|
||||
'total': row['total'],
|
||||
'month': m,
|
||||
'total': month_totals.get(m, 0),
|
||||
}
|
||||
for row in by_month_qs
|
||||
for m in months
|
||||
]
|
||||
|
||||
# ------------------
|
||||
# Availables years
|
||||
# -----------------
|
||||
|
||||
year_list = (
|
||||
expenses
|
||||
.annotate(year=ExtractYear('date'))
|
||||
.values_list('year', float=True)
|
||||
.values_list('year', flat=True)
|
||||
.distinct()
|
||||
.order_by('year')
|
||||
)
|
||||
# Month list
|
||||
months = list(range(1, 13))
|
||||
|
||||
# Prepare data fot ploting in chart
|
||||
chart_labels = []
|
||||
chart_totals = []
|
||||
for month_data in by_month:
|
||||
chart_labels.append(month_data['month'])
|
||||
chart_totals.append(float(month_data['total']))
|
||||
chart_labels = months
|
||||
chart_totals = [row['total'] for row in by_month]
|
||||
|
||||
# Send the data to the dashboard
|
||||
return render(request, 'expenses/dashboard.html', {
|
||||
@ -170,7 +186,7 @@ def dashboard(request):
|
||||
'chart_labels': chart_labels,
|
||||
'chart_data': chart_totals,
|
||||
'year_list': year_list,
|
||||
'selected_year': year,
|
||||
'selected_month': month,
|
||||
'months': months,
|
||||
'selected_year': selected_year,
|
||||
'selected_month': selected_month,
|
||||
})
|
||||
Loading…
Reference in New Issue
Block a user