Updated category views as class-based views

This commit is contained in:
Edgar P. Burkhart 2022-12-28 12:03:02 +01:00
parent 033698b38a
commit 55e77addc5
Signed by: edpibu
GPG key ID: 9833D3C5A25BD227
5 changed files with 61 additions and 57 deletions

View file

@ -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")

View file

@ -1,29 +0,0 @@
{% extends "main/base.html" %}
{% load static %}
{% load main_extras %}
{% load i18n %}
{% block link %}
{{ block.super }}
<link rel="stylesheet"
href="{% static 'main/css/form.css' %}"
type="text/css"/>
<link rel="stylesheet"
href="{% static 'main/css/table.css' %}"
type="text/css"/>
{% endblock %}
{% block body %}
<h1>
<i class="fa fa-{{ category.icon }}"></i> {{ category }}
</h1>
<form action="{% url 'category' category.id %}" method="post">
{% csrf_token %}
{{ form }}
{% form_buttons category %}
</form>
{% if transactions %}
<img src="{% url "plot-category" category.id %}"
alt="Graph representing value over time"/>
<h2>{% translate "Transactions" %}</h2>
{% transaction_table transactions %}
{% endif %}
{% endblock %}

View file

@ -0,0 +1,28 @@
{% extends "main/base.html" %}
{% load static %}
{% load main_extras %}
{% load i18n %}
{% block link %}
{{ block.super }}
<link rel="stylesheet"
href="{% static 'main/css/form.css' %}"
type="text/css"/>
<link rel="stylesheet"
href="{% static 'main/css/table.css' %}"
type="text/css"/>
{% endblock %}
{% block body %}
<h1>
<i class="fa fa-{{ form.instance.icon }}"></i> {{ form.instance }}
</h1>
<form method="post">
{% csrf_token %}
{{ form }}
</form>
{% if form.instance.transactions %}
<img src="{% url "plot-category" form.instance.id %}"
alt="Graph representing value over time"/>
<h2>{% translate "Transactions" %}</h2>
{% transaction_table form.instance.transactions %}
{% endif %}
{% endblock %}

View file

@ -13,12 +13,14 @@ urlpatterns = [
views.InvoiceCreateView.as_view(),
name="invoice",
),
path("category", views.CategoryCreateView.as_view(), name="category"),
path("transaction/<pk>", views.TransactionUpdateView.as_view(), name="transaction"),
path(
"transaction/<transaction_pk>/invoice/<pk>",
views.InvoiceUpdateView.as_view(),
name="invoice",
),
path("category/<pk>", views.CategoryUpdateView.as_view(), name="category"),
path(
"transaction/<pk>/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/<uuid>", views.category, name="category"),
path("category/<uuid>/del", views.del_category, name="del_category"),
path(
"category/<pk>/delete", views.CategoryDeleteView.as_view(), name="del_category"
),
path("snapshot", views.snapshot, name="snapshot"),
path("snapshot/<uuid>", views.snapshot, name="snapshot"),
path("snapshot/<uuid>/del", views.del_snapshot, name="del_snapshot"),

View file

@ -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