diff --git a/nummi/main/models.py b/nummi/main/models.py index b2bef5f..11e1349 100644 --- a/nummi/main/models.py +++ b/nummi/main/models.py @@ -6,6 +6,7 @@ from django.core.files.storage import Storage from django.core.validators import FileExtensionValidator from django.db import models from django.forms import ModelForm +from django.urls import reverse from django.utils.translation import gettext as _ @@ -27,7 +28,7 @@ class Category(models.Model): class CategoryForm(ModelForm): - template_name = "main/form.html" + template_name = "main/form/base.html" class Meta: model = Category @@ -62,6 +63,9 @@ class Transaction(models.Model): def __str__(self): return f"{self.date} – {self.name}" + def get_absolute_url(self): + return reverse("transaction", kwargs={"pk": self.pk}) + @property def invoices(self): return Invoice.objects.filter(transaction=self) @@ -77,7 +81,7 @@ class Transaction(models.Model): class TransactionForm(ModelForm): - template_name = "main/form.html" + template_name = "main/form/base.html" class Meta: model = Transaction @@ -123,7 +127,7 @@ class Invoice(models.Model): class InvoiceForm(ModelForm): - template_name = "main/form.html" + template_name = "main/form/base.html" prefix = "invoice" class Meta: @@ -263,7 +267,7 @@ class Snapshot(models.Model): class SnapshotForm(ModelForm): - template_name = "main/form.html" + template_name = "main/form/base.html" class Meta: model = Snapshot diff --git a/nummi/main/templates/main/form.html b/nummi/main/templates/main/form.html deleted file mode 100644 index 692f781..0000000 --- a/nummi/main/templates/main/form.html +++ /dev/null @@ -1,10 +0,0 @@ -{% if form.non_field_errors %} - -{% endif %} -{% for field in form %} - {{ field.errors }} - -
{{ field }}
-{% endfor %} diff --git a/nummi/main/templates/main/form/base.html b/nummi/main/templates/main/form/base.html new file mode 100644 index 0000000..b41d1c9 --- /dev/null +++ b/nummi/main/templates/main/form/base.html @@ -0,0 +1,14 @@ +{% load i18n %} +{% block fields %} + {% csrf_token %} + {% if form.non_field_errors %} + + {% endif %} + {% for field in form %} + {{ field.errors }} + +
{{ field }}
+ {% endfor %} +{% endblock %} diff --git a/nummi/main/templates/main/transaction.html b/nummi/main/templates/main/transaction.html deleted file mode 100644 index 617a4fd..0000000 --- a/nummi/main/templates/main/transaction.html +++ /dev/null @@ -1,64 +0,0 @@ -{% extends "main/base.html" %} -{% load static %} -{% load main_extras %} -{% load i18n %} -{% block link %} - {{ block.super }} - - -{% endblock %} -{% block body %} -

{{ transaction }}

- {% spaceless %} -
- {% csrf_token %} - - {{ form }} - {% form_buttons transaction %} -
- {% endspaceless %} -
- {% if transaction.has_invoice %} -
-

{% translate "Invoices" %}

-
-
- - {% translate "Name" %} - -
- {% for inv in transaction.invoices %} - - {% endfor %} -
-
- {% endif %} -
-

{% translate "Add invoice" %}

-
- {% csrf_token %} - - {{ invoice_form }} -
- -
-
-
-
- {% endblock %} diff --git a/nummi/main/templates/main/transaction_form.html b/nummi/main/templates/main/transaction_form.html new file mode 100644 index 0000000..92a8616 --- /dev/null +++ b/nummi/main/templates/main/transaction_form.html @@ -0,0 +1,22 @@ +{% extends "main/base.html" %} +{% load static %} +{% load main_extras %} +{% load i18n %} +{% block link %} + {{ block.super }} + + +{% endblock %} +{% block body %} +

{{ transaction }}

+ {% spaceless %} +
+ {{ form }} + {% form_buttons form.instance %} +
+ {% endspaceless %} + {% endblock %} diff --git a/nummi/main/urls.py b/nummi/main/urls.py index a72733f..bd15b5a 100644 --- a/nummi/main/urls.py +++ b/nummi/main/urls.py @@ -7,8 +7,8 @@ urlpatterns = [ path("login", views.LoginView.as_view(), name="login"), path("logout", views.LogoutView.as_view(), name="logout"), path("transactions", views.TransactionListView.as_view(), name="transactions"), - path("transaction", views.transaction, name="transaction"), - path("transaction/", views.transaction, name="transaction"), + path("transaction", views.TransactionCreateView.as_view(), name="transaction"), + path("transaction/", views.TransactionUpdateView.as_view(), name="transaction"), path("transaction//del", views.del_transaction, name="del_transaction"), path("del_invoice//", views.del_invoice, name="del_invoice"), path("invoice/", views.invoice, name="invoice"), diff --git a/nummi/main/views.py b/nummi/main/views.py index f4cc9da..1191b15 100644 --- a/nummi/main/views.py +++ b/nummi/main/views.py @@ -11,7 +11,7 @@ from django.core.paginator import Paginator from django.db import models from django.http import HttpResponse from django.shortcuts import get_object_or_404, redirect, render -from django.views.generic import ListView +from django.views.generic import CreateView, ListView, UpdateView from .models import ( Category, @@ -92,6 +92,16 @@ def transaction(request, uuid=None): ) +class TransactionCreateView(LoginRequiredMixin, CreateView): + model = Transaction + form_class = TransactionForm + + +class TransactionUpdateView(LoginRequiredMixin, UpdateView): + model = Transaction + form_class = TransactionForm + + @login_required def del_transaction(request, uuid): _transaction = get_object_or_404(Transaction, id=uuid)