From f6e8b583ccd0c7ebe047da5023cd84fd1bd4e76d Mon Sep 17 00:00:00 2001 From: "Edgar P. Burkhart" Date: Sat, 22 Apr 2023 12:15:48 +0200 Subject: [PATCH] Implemented frontend for category --- nummi/category/models.py | 4 ++-- .../templates/category/category_form.html} | 4 +++- nummi/category/urls.py | 12 +++++------- nummi/category/views.py | 16 +++++++++------- 4 files changed, 19 insertions(+), 17 deletions(-) rename nummi/{main/templates/main/form/category.html => category/templates/category/category_form.html} (88%) diff --git a/nummi/category/models.py b/nummi/category/models.py index 845600b..116ba4f 100644 --- a/nummi/category/models.py +++ b/nummi/category/models.py @@ -22,10 +22,10 @@ class Category(UserModel): return str(self.name) def get_absolute_url(self): - return reverse("category", kwargs={"pk": self.pk}) + return reverse("category", args=(self.pk,)) def get_delete_url(self): - return reverse("del_category", kwargs={"pk": self.pk}) + return reverse("del_category", args=(self.pk,)) class Meta: ordering = ["name"] diff --git a/nummi/main/templates/main/form/category.html b/nummi/category/templates/category/category_form.html similarity index 88% rename from nummi/main/templates/main/form/category.html rename to nummi/category/templates/category/category_form.html index 4a7cc62..6223526 100644 --- a/nummi/main/templates/main/form/category.html +++ b/nummi/category/templates/category/category_form.html @@ -9,9 +9,11 @@ {% endblock %} {% block h2 %}{{ form.instance.icon|remix }}{{ form.instance }}{% endblock %} {% block tables %} - {% if form.instance.transactions %} + {% if transactions %}

{% translate "Transactions" %}

{% include "main/table/transaction.html" %} + {% endif %} + {% if history.data %}

{% translate "History" %}

{% include "main/plot/history.html" %} {% endif %} diff --git a/nummi/category/urls.py b/nummi/category/urls.py index 601e925..3d72bca 100644 --- a/nummi/category/urls.py +++ b/nummi/category/urls.py @@ -4,18 +4,16 @@ from transaction.views import TransactionMonthView from . import views urlpatterns = [ - path("category", views.CategoryCreateView.as_view(), name="new_category"), - path("category/", views.CategoryUpdateView.as_view(), name="category"), + path("", views.CategoryCreateView.as_view(), name="new_category"), + path("", views.CategoryUpdateView.as_view(), name="category"), path( - "category//transactions", + "/transactions", views.CategoryTListView.as_view(), name="category_transactions", ), + path("/delete", views.CategoryDeleteView.as_view(), name="del_category"), path( - "category//delete", views.CategoryDeleteView.as_view(), name="del_category" - ), - path( - "category//history//", + "/history//", TransactionMonthView.as_view(), name="transaction_month", ), diff --git a/nummi/category/views.py b/nummi/category/views.py index e84ac96..53d8609 100644 --- a/nummi/category/views.py +++ b/nummi/category/views.py @@ -1,3 +1,4 @@ +from django.shortcuts import get_object_or_404 from django.urls import reverse_lazy from main.views import NummiCreateView, NummiDeleteView, NummiUpdateView from transaction.utils import history @@ -10,13 +11,12 @@ from .models import Category class CategoryCreateView(NummiCreateView): model = Category form_class = CategoryForm - template_name = "main/form/category.html" class CategoryUpdateView(NummiUpdateView): model = Category form_class = CategoryForm - template_name = "main/form/category.html" + pk_url_kwarg = "category" def get_context_data(self, **kwargs): data = super().get_context_data(**kwargs) @@ -33,17 +33,19 @@ class CategoryUpdateView(NummiUpdateView): class CategoryDeleteView(NummiDeleteView): model = Category + pk_url_kwarg = "category" class CategoryMixin: def get_queryset(self): - return super().get_queryset().filter(category=self.kwargs.get("pk")) + self.category = get_object_or_404( + Category.objects.filter(user=self.request.user), + pk=self.kwargs.get("category"), + ) + return super().get_queryset().filter(category=self.category) def get_context_data(self, **kwargs): - return super().get_context_data(**kwargs) | { - "object": Category.objects.get(pk=self.kwargs.get("pk")), - "category": True, - } + return super().get_context_data(**kwargs) | {"category": self.category} class CategoryTListView(CategoryMixin, TransactionListView):