Updated category views as class-based views
This commit is contained in:
parent
033698b38a
commit
55e77addc5
5 changed files with 61 additions and 57 deletions
|
@ -21,6 +21,20 @@ class Category(models.Model):
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return self.name
|
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:
|
class Meta:
|
||||||
ordering = ["name"]
|
ordering = ["name"]
|
||||||
verbose_name = _("Category")
|
verbose_name = _("Category")
|
||||||
|
|
|
@ -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 %}
|
|
28
nummi/main/templates/main/category_form.html
Normal file
28
nummi/main/templates/main/category_form.html
Normal 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 %}
|
|
@ -13,12 +13,14 @@ urlpatterns = [
|
||||||
views.InvoiceCreateView.as_view(),
|
views.InvoiceCreateView.as_view(),
|
||||||
name="invoice",
|
name="invoice",
|
||||||
),
|
),
|
||||||
|
path("category", views.CategoryCreateView.as_view(), name="category"),
|
||||||
path("transaction/<pk>", views.TransactionUpdateView.as_view(), name="transaction"),
|
path("transaction/<pk>", views.TransactionUpdateView.as_view(), name="transaction"),
|
||||||
path(
|
path(
|
||||||
"transaction/<transaction_pk>/invoice/<pk>",
|
"transaction/<transaction_pk>/invoice/<pk>",
|
||||||
views.InvoiceUpdateView.as_view(),
|
views.InvoiceUpdateView.as_view(),
|
||||||
name="invoice",
|
name="invoice",
|
||||||
),
|
),
|
||||||
|
path("category/<pk>", views.CategoryUpdateView.as_view(), name="category"),
|
||||||
path(
|
path(
|
||||||
"transaction/<pk>/delete",
|
"transaction/<pk>/delete",
|
||||||
views.TransactionDeleteView.as_view(),
|
views.TransactionDeleteView.as_view(),
|
||||||
|
@ -29,9 +31,9 @@ urlpatterns = [
|
||||||
views.InvoiceDeleteView.as_view(),
|
views.InvoiceDeleteView.as_view(),
|
||||||
name="del_invoice",
|
name="del_invoice",
|
||||||
),
|
),
|
||||||
path("category", views.category, name="category"),
|
path(
|
||||||
path("category/<uuid>", views.category, name="category"),
|
"category/<pk>/delete", views.CategoryDeleteView.as_view(), name="del_category"
|
||||||
path("category/<uuid>/del", views.del_category, name="del_category"),
|
),
|
||||||
path("snapshot", views.snapshot, name="snapshot"),
|
path("snapshot", views.snapshot, name="snapshot"),
|
||||||
path("snapshot/<uuid>", views.snapshot, name="snapshot"),
|
path("snapshot/<uuid>", views.snapshot, name="snapshot"),
|
||||||
path("snapshot/<uuid>/del", views.del_snapshot, name="del_snapshot"),
|
path("snapshot/<uuid>/del", views.del_snapshot, name="del_snapshot"),
|
||||||
|
|
|
@ -75,6 +75,11 @@ class InvoiceCreateView(LoginRequiredMixin, CreateView):
|
||||||
return reverse_lazy("transaction", kwargs={"pk": self.object.transaction.pk})
|
return reverse_lazy("transaction", kwargs={"pk": self.object.transaction.pk})
|
||||||
|
|
||||||
|
|
||||||
|
class CategoryCreateView(LoginRequiredMixin, CreateView):
|
||||||
|
model = Category
|
||||||
|
form_class = CategoryForm
|
||||||
|
|
||||||
|
|
||||||
class TransactionUpdateView(LoginRequiredMixin, UpdateView):
|
class TransactionUpdateView(LoginRequiredMixin, UpdateView):
|
||||||
model = Transaction
|
model = Transaction
|
||||||
form_class = TransactionForm
|
form_class = TransactionForm
|
||||||
|
@ -93,6 +98,11 @@ class InvoiceUpdateView(LoginRequiredMixin, UpdateView):
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class CategoryUpdateView(LoginRequiredMixin, UpdateView):
|
||||||
|
model = Category
|
||||||
|
form_class = CategoryForm
|
||||||
|
|
||||||
|
|
||||||
class TransactionDeleteView(LoginRequiredMixin, DeleteView):
|
class TransactionDeleteView(LoginRequiredMixin, DeleteView):
|
||||||
model = Transaction
|
model = Transaction
|
||||||
template_name = "main/confirm_delete.html"
|
template_name = "main/confirm_delete.html"
|
||||||
|
@ -112,31 +122,10 @@ class InvoiceDeleteView(LoginRequiredMixin, DeleteView):
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@login_required
|
class CategoryDeleteView(LoginRequiredMixin, DeleteView):
|
||||||
def category(request, uuid=None):
|
model = Category
|
||||||
if request.method == "GET":
|
template_name = "main/confirm_delete.html"
|
||||||
if uuid is None:
|
success_url = reverse_lazy("index")
|
||||||
_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,
|
|
||||||
},
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
@login_required
|
@login_required
|
||||||
|
|
Loading…
Reference in a new issue