Add category inheritance to view and template

This commit is contained in:
Edgar P. Burkhart 2022-05-20 15:55:37 +02:00
parent a3273c13cf
commit ea9d871e25
Signed by: edpibu
GPG Key ID: 9833D3C5A25BD227
2 changed files with 14 additions and 5 deletions

View File

@ -31,9 +31,7 @@
{% if categories %}
<ul>
{% for cat in categories %}
<li>{{ cat }}</li>
{% endfor %}
{{ categories|unordered_list }}
</ul>
{% endif %}

View File

@ -9,11 +9,22 @@ from .models import Transaction, TransactionForm, Invoice, InvoiceForm, Category
@login_required
def index(request):
_transactions = Transaction.objects.order_by("-date")[:5]
_categories = Category.objects.order_by("name")
_categories = Category.objects.filter(parent=None).order_by("name")
def _cat_list(cat):
children = []
for child in Category.objects.filter(parent=cat):
children += _cat_list(child)
if len(children) == 0: return cat,
return cat, children
_cats = []
for cat in _categories:
_cats += _cat_list(cat)
context = {
"transactions": _transactions,
"categories": _categories,
"categories": _cats,
}
return render(request, "main/index.html", context)