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 %} {% if categories %}
<ul> <ul>
{% for cat in categories %} {{ categories|unordered_list }}
<li>{{ cat }}</li>
{% endfor %}
</ul> </ul>
{% endif %} {% endif %}

View file

@ -9,11 +9,22 @@ from .models import Transaction, TransactionForm, Invoice, InvoiceForm, Category
@login_required @login_required
def index(request): def index(request):
_transactions = Transaction.objects.order_by("-date")[:5] _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 = { context = {
"transactions": _transactions, "transactions": _transactions,
"categories": _categories, "categories": _cats,
} }
return render(request, "main/index.html", context) return render(request, "main/index.html", context)