From 467c8002e238dfdb1bdaaeaaaaf710ae68ee254f Mon Sep 17 00:00:00 2001 From: "Edgar P. Burkhart" Date: Fri, 20 May 2022 19:17:42 +0200 Subject: [PATCH] Add category page --- nummi/main/models.py | 13 +++++- nummi/main/static/css/index.css | 7 ++-- nummi/main/templates/main/category.html | 20 +++++++++ nummi/main/templates/main/index.html | 8 +++- nummi/main/urls.py | 3 ++ nummi/main/views.py | 55 ++++++++++++++++++------- 6 files changed, 84 insertions(+), 22 deletions(-) create mode 100644 nummi/main/templates/main/category.html diff --git a/nummi/main/models.py b/nummi/main/models.py index 073fafd..5b3773a 100644 --- a/nummi/main/models.py +++ b/nummi/main/models.py @@ -12,7 +12,7 @@ class Category(models.Model): parent = models.ForeignKey("self", on_delete=models.SET_NULL, blank=True, null=True) def __str__(self): - if self.parent is None: + if self.parent is None or self.parent == self: return self.name return f"{self.parent}>{self.name}" @@ -24,7 +24,7 @@ class Category(models.Model): @property def tree(self): - if self.parent is None: + if self.parent is None or self.parent == self: return [self.name] return self.parent.tree + [self.name] @@ -32,6 +32,12 @@ class Category(models.Model): ordering = ["full_name"] +class CategoryForm(ModelForm): + class Meta: + model = Category + fields = ["name", "parent"] + + class Transaction(models.Model): id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) name = models.CharField(max_length=256, default="Transaction") @@ -49,6 +55,9 @@ class Transaction(models.Model): return f"{res} ({self.category})" return res + class Meta: + ordering = ["-date"] + class TransactionForm(ModelForm): class Meta: diff --git a/nummi/main/static/css/index.css b/nummi/main/static/css/index.css index c4c798f..90649d9 100644 --- a/nummi/main/static/css/index.css +++ b/nummi/main/static/css/index.css @@ -42,7 +42,6 @@ ul#categories { list-style-type: "– "; } -#categories ul { - list-style-type: "↳ "; - padding-left: 1.5em; -} +ul#categories li.cat2 {margin-left: 1em} +ul#categories li.cat3 {margin-left: 2em} +ul#categories li.cat4 {margin-left: 3em} diff --git a/nummi/main/templates/main/category.html b/nummi/main/templates/main/category.html new file mode 100644 index 0000000..07ed357 --- /dev/null +++ b/nummi/main/templates/main/category.html @@ -0,0 +1,20 @@ +{% extends "main/base.html" %} + +{% block body %} +Home + +

{{ category.tree|join:" → " }}

+ +
+ {% csrf_token %} + {{ form.as_p }} + +
+ +{% if transactions %} +

Transactions ({{ transactions|length }})

+ +{% endif %} +{% endblock %} diff --git a/nummi/main/templates/main/index.html b/nummi/main/templates/main/index.html index 4a394a4..262b396 100644 --- a/nummi/main/templates/main/index.html +++ b/nummi/main/templates/main/index.html @@ -37,7 +37,13 @@ {% if categories %} {% endif %} diff --git a/nummi/main/urls.py b/nummi/main/urls.py index 194ddb6..4114aaf 100644 --- a/nummi/main/urls.py +++ b/nummi/main/urls.py @@ -18,4 +18,7 @@ urlpatterns = [ name="del_invoice", ), path("invoice/", views.invoice, name="invoice"), + path("category", views.category, name="category"), + path("category/", views.category, name="category"), + path("category//update", views.update_category, name="update_category"), ] diff --git a/nummi/main/views.py b/nummi/main/views.py index 95bef5e..8afcc8c 100644 --- a/nummi/main/views.py +++ b/nummi/main/views.py @@ -3,29 +3,24 @@ from django.http import HttpResponse from django.contrib.auth.decorators import login_required from django.contrib.auth import views as auth_views -from .models import Transaction, TransactionForm, Invoice, InvoiceForm, Category +from .models import ( + Transaction, + TransactionForm, + Invoice, + InvoiceForm, + Category, + CategoryForm, +) @login_required def index(request): _transactions = Transaction.objects.order_by("-date")[:5] - _categories = Category.objects.filter(parent=None) - - def _cat_list(cat): - children = [] - for child in Category.objects.filter(parent=cat): - children += _cat_list(child) - if len(children) == 0: - return (cat.name,) - return cat.name, children - - _cats = [] - for cat in _categories: - _cats += _cat_list(cat) + _categories = Category.objects.all() context = { "transactions": _transactions, - "categories": _cats, + "categories": _categories, } return render(request, "main/index.html", context) @@ -91,3 +86,33 @@ def del_invoice(request, uuid, invoice_id): _invoice = get_object_or_404(Invoice, id=invoice_id) _invoice.delete() return redirect(transaction, uuid=uuid) + + +@login_required +def category(request, uuid=None): + if uuid is None: + _category = Category() + _transactions = None + else: + _category = get_object_or_404(Category, id=uuid) + _transactions = Transaction.objects.filter(category=_category) + return render( + request, + "main/category.html", + { + "category": _category, + "form": CategoryForm(instance=_category), + "transactions": _transactions, + }, + ) + + +@login_required +def update_category(request, uuid): + try: + _category = Category.objects.get(id=uuid) + except Category.DoesNotExist: + _category = Category(id=uuid) + _form = CategoryForm(request.POST, instance=_category) + _form.save() + return redirect(category, uuid=uuid)