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