From 6ef5a441e119083969acac88a00f5421d140ed23 Mon Sep 17 00:00:00 2001 From: "Edgar P. Burkhart" Date: Thu, 19 May 2022 21:21:55 +0200 Subject: [PATCH] Add invoice management --- nummi/main/models.py | 10 ++++++++++ nummi/main/templates/main/transaction.html | 17 ++++++++++++----- nummi/main/urls.py | 6 ++++++ nummi/main/views.py | 17 ++++++++++++++++- 4 files changed, 44 insertions(+), 6 deletions(-) diff --git a/nummi/main/models.py b/nummi/main/models.py index e15691b..a5f30f4 100644 --- a/nummi/main/models.py +++ b/nummi/main/models.py @@ -28,3 +28,13 @@ class Invoice(models.Model): def __str__(self): return f"{self.name}: {self.transaction}" + + def delete(self, *args, **kwargs): + self.file.delete() + super().delete(*args, **kwargs) + + +class InvoiceForm(ModelForm): + class Meta: + model = Invoice + fields = ["name", "file"] diff --git a/nummi/main/templates/main/transaction.html b/nummi/main/templates/main/transaction.html index 89a6225..ea8e9cf 100644 --- a/nummi/main/templates/main/transaction.html +++ b/nummi/main/templates/main/transaction.html @@ -4,8 +4,15 @@ - +
+ {% csrf_token %} + +
diff --git a/nummi/main/urls.py b/nummi/main/urls.py index 36b814d..0663757 100644 --- a/nummi/main/urls.py +++ b/nummi/main/urls.py @@ -8,5 +8,11 @@ urlpatterns = [ path( "transaction//update", views.update_transaction, name="update_transaction" ), + path("transaction//add_invoice", views.add_invoice, name="add_invoice"), + path( + "transaction//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 473b05d..84f2f91 100644 --- a/nummi/main/views.py +++ b/nummi/main/views.py @@ -2,7 +2,7 @@ from django.shortcuts import render, get_object_or_404, redirect from django.http import HttpResponse -from .models import Transaction, TransactionForm, Invoice +from .models import Transaction, TransactionForm, Invoice, InvoiceForm def index(request): @@ -23,6 +23,7 @@ def transaction(request, uuid): "transaction": _transaction, "form": TransactionForm(instance=_transaction), "invoices": _invoices, + "invoice_form": InvoiceForm(instance=Invoice(transaction=_transaction)), }, ) @@ -38,3 +39,17 @@ def invoice(request, uuid): _invoice = get_object_or_404(Invoice, id=uuid) with _invoice.file.open() as _file: return HttpResponse(_file.read(), content_type="application/pdf") + + +def add_invoice(request, uuid): + _transaction = get_object_or_404(Transaction, id=uuid) + _invoice = Invoice(transaction=_transaction) + _form = InvoiceForm(request.POST, request.FILES, instance=_invoice) + _form.save() + return redirect(transaction, uuid=uuid) + + +def del_invoice(request, uuid, invoice_id): + _invoice = get_object_or_404(Invoice, id=invoice_id) + _invoice.delete() + return redirect(transaction, uuid=uuid)