Modified filters for expenses lit
This commit is contained in:
parent
89b9a6b016
commit
c4ec35e14d
@ -7,15 +7,27 @@
|
||||
<h1>Mis gastos</h1>
|
||||
|
||||
<form method="get">
|
||||
<label>
|
||||
Año:
|
||||
<input type="number" name="year" value="{{ selected_year }}">
|
||||
</label>
|
||||
<select name="year">
|
||||
<option value="">Año</option>
|
||||
{% for y in year_list %}
|
||||
<option value="{{ y }}"
|
||||
{% if selected_year == y %}selected{% endif %}
|
||||
>
|
||||
{{ y }}
|
||||
</option>
|
||||
{% endfor %}
|
||||
</select>
|
||||
|
||||
<label>
|
||||
Mes:
|
||||
<input type="number" name="month" value="{{ selected_month }}">
|
||||
</label>
|
||||
<select name="month">
|
||||
<option value="">Mes</option>
|
||||
{% for m in months %}
|
||||
<option value="{{ m }}"
|
||||
{% if selected_month == m %}selected{% endif %}
|
||||
>
|
||||
{{ m }}
|
||||
</option>
|
||||
{% endfor %}
|
||||
</select>
|
||||
|
||||
<label>
|
||||
Categoría:
|
||||
@ -23,7 +35,7 @@
|
||||
<option value="">Todas</option>
|
||||
{% for cat in categories %}
|
||||
<option value="{{ cat.id }}"
|
||||
{% if selected_category == cat.id|stringformat:"s" %}selected{% endif%}
|
||||
{% if selected_category == cat.id %}selected{% endif%}
|
||||
>
|
||||
{{ cat.name }}
|
||||
</option>
|
||||
|
||||
@ -99,17 +99,21 @@ def home(request):
|
||||
|
||||
@login_required
|
||||
def expense_list(request):
|
||||
expenses = (
|
||||
Expense.objects
|
||||
.filter(owner=request.user)
|
||||
.select_related('category')
|
||||
.order_by('-date')
|
||||
expenses = Expense.objects.filter(owner=request.user)
|
||||
|
||||
categories = Category.objects.filter(owner=request.user)
|
||||
|
||||
year_list = (
|
||||
Expense.objects.filter(owner=request.user)
|
||||
.dates('date', 'year')
|
||||
)
|
||||
|
||||
months = list(range(1, 13))
|
||||
|
||||
# Filters
|
||||
year = request.GET.get('year')
|
||||
month = request.GET.get('month')
|
||||
category = request.GET.get('category')
|
||||
year = _get_int(request.GET.get('year'))
|
||||
month = _get_int(request.GET.get('month'))
|
||||
category = _get_int(request.GET.get('category'))
|
||||
|
||||
if year:
|
||||
expenses = expenses.filter(date__year=year)
|
||||
@ -120,7 +124,7 @@ def expense_list(request):
|
||||
if category:
|
||||
expenses = expenses.filter(category_id=category)
|
||||
|
||||
categories = Category.objects.filter(owner=request.user).order_by('name')
|
||||
expenses = expenses.order_by('-date')
|
||||
|
||||
# Pagination
|
||||
paginator = Paginator(expenses, 10)
|
||||
@ -131,12 +135,14 @@ def expense_list(request):
|
||||
request,
|
||||
'expenses/expense_list.html',
|
||||
{
|
||||
'expenses': expenses,
|
||||
'categories': categories,
|
||||
'expenses': page_obj,
|
||||
'page_obj': page_obj,
|
||||
'selected_year': year,
|
||||
'selected_month': month,
|
||||
'selected_category': category,
|
||||
'page_obj': page_obj,
|
||||
'categories': categories,
|
||||
'year_list': [y.year for y in year_list],
|
||||
'months': months,
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user