From 55e77addc593819d604531ae1f36313d69a56d8e Mon Sep 17 00:00:00 2001 From: "Edgar P. Burkhart" Date: Wed, 28 Dec 2022 12:03:02 +0100 Subject: [PATCH] Updated category views as class-based views --- nummi/main/models.py | 14 +++++++ nummi/main/templates/main/category.html | 29 --------------- nummi/main/templates/main/category_form.html | 28 ++++++++++++++ nummi/main/urls.py | 8 ++-- nummi/main/views.py | 39 +++++++------------- 5 files changed, 61 insertions(+), 57 deletions(-) delete mode 100644 nummi/main/templates/main/category.html create mode 100644 nummi/main/templates/main/category_form.html diff --git a/nummi/main/models.py b/nummi/main/models.py index b450963..6bec146 100644 --- a/nummi/main/models.py +++ b/nummi/main/models.py @@ -21,6 +21,20 @@ class Category(models.Model): def __str__(self): return self.name + def get_absolute_url(self): + return reverse("category", kwargs={"pk": self.pk}) + + def get_delete_url(self): + return reverse("del_category", kwargs={"pk": self.pk}) + + @property + def adding(self): + return self._state.adding + + @property + def transactions(self): + return Transaction.objects.filter(category=self) + class Meta: ordering = ["name"] verbose_name = _("Category") diff --git a/nummi/main/templates/main/category.html b/nummi/main/templates/main/category.html deleted file mode 100644 index 17c71fd..0000000 --- a/nummi/main/templates/main/category.html +++ /dev/null @@ -1,29 +0,0 @@ -{% extends "main/base.html" %} -{% load static %} -{% load main_extras %} -{% load i18n %} -{% block link %} - {{ block.super }} - - -{% endblock %} -{% block body %} -

- {{ category }} -

-
- {% csrf_token %} - {{ form }} - {% form_buttons category %} -
- {% if transactions %} - Graph representing value over time -

{% translate "Transactions" %}

- {% transaction_table transactions %} - {% endif %} - {% endblock %} diff --git a/nummi/main/templates/main/category_form.html b/nummi/main/templates/main/category_form.html new file mode 100644 index 0000000..b6422fd --- /dev/null +++ b/nummi/main/templates/main/category_form.html @@ -0,0 +1,28 @@ +{% extends "main/base.html" %} +{% load static %} +{% load main_extras %} +{% load i18n %} +{% block link %} + {{ block.super }} + + +{% endblock %} +{% block body %} +

+ {{ form.instance }} +

+
+ {% csrf_token %} + {{ form }} +
+ {% if form.instance.transactions %} + Graph representing value over time +

{% translate "Transactions" %}

+ {% transaction_table form.instance.transactions %} + {% endif %} +{% endblock %} diff --git a/nummi/main/urls.py b/nummi/main/urls.py index 9a38c96..a028581 100644 --- a/nummi/main/urls.py +++ b/nummi/main/urls.py @@ -13,12 +13,14 @@ urlpatterns = [ views.InvoiceCreateView.as_view(), name="invoice", ), + path("category", views.CategoryCreateView.as_view(), name="category"), path("transaction/", views.TransactionUpdateView.as_view(), name="transaction"), path( "transaction//invoice/", views.InvoiceUpdateView.as_view(), name="invoice", ), + path("category/", views.CategoryUpdateView.as_view(), name="category"), path( "transaction//delete", views.TransactionDeleteView.as_view(), @@ -29,9 +31,9 @@ urlpatterns = [ views.InvoiceDeleteView.as_view(), name="del_invoice", ), - path("category", views.category, name="category"), - path("category/", views.category, name="category"), - path("category//del", views.del_category, name="del_category"), + path( + "category//delete", views.CategoryDeleteView.as_view(), name="del_category" + ), path("snapshot", views.snapshot, name="snapshot"), path("snapshot/", views.snapshot, name="snapshot"), path("snapshot//del", views.del_snapshot, name="del_snapshot"), diff --git a/nummi/main/views.py b/nummi/main/views.py index 57e0c58..c297dca 100644 --- a/nummi/main/views.py +++ b/nummi/main/views.py @@ -75,6 +75,11 @@ class InvoiceCreateView(LoginRequiredMixin, CreateView): return reverse_lazy("transaction", kwargs={"pk": self.object.transaction.pk}) +class CategoryCreateView(LoginRequiredMixin, CreateView): + model = Category + form_class = CategoryForm + + class TransactionUpdateView(LoginRequiredMixin, UpdateView): model = Transaction form_class = TransactionForm @@ -93,6 +98,11 @@ class InvoiceUpdateView(LoginRequiredMixin, UpdateView): ) +class CategoryUpdateView(LoginRequiredMixin, UpdateView): + model = Category + form_class = CategoryForm + + class TransactionDeleteView(LoginRequiredMixin, DeleteView): model = Transaction template_name = "main/confirm_delete.html" @@ -112,31 +122,10 @@ class InvoiceDeleteView(LoginRequiredMixin, DeleteView): ) -@login_required -def category(request, uuid=None): - if request.method == "GET": - if uuid is None: - _category = Category() - else: - _category = get_object_or_404(Category, id=uuid) - _form = CategoryForm(instance=_category) - elif request.method == "POST": - _category, _ = Category.objects.get_or_create(id=uuid) - _form = CategoryForm(request.POST, instance=_category) - if _form.is_valid(): - _form.save() - return redirect(category, uuid=uuid) - - return render( - request, - "main/category.html", - { - "category": _category, - "form": _form, - "transactions": Transaction.objects.filter(category=_category), - "adding": _category._state.adding, - }, - ) +class CategoryDeleteView(LoginRequiredMixin, DeleteView): + model = Category + template_name = "main/confirm_delete.html" + success_url = reverse_lazy("index") @login_required