Add invoice management

This commit is contained in:
Edgar P. Burkhart 2022-05-19 21:21:55 +02:00
parent 5339a725f2
commit 6ef5a441e1
Signed by: edpibu
GPG key ID: 9833D3C5A25BD227
4 changed files with 44 additions and 6 deletions

View file

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

View file

@ -4,8 +4,15 @@
<input type="submit" />
</form>
<ul>
<form action="{% url 'add_invoice' transaction.id %}" method="post" enctype="multipart/form-data">
{% csrf_token %}
<ul>
{% for inv in invoices %}
<li><a href="{% url 'invoice' inv.id %}">{{ inv.name }}</a></li>
<li>
<a href="{% url 'invoice' inv.id %}">{{ inv.name }}</a>
<a href="{% url 'del_invoice' transaction.id inv.id %}">x</a>
</li>
{% endfor %}
</ul>
<li>{{ invoice_form }}<input type="submit" /></li>
</ul>
</form>

View file

@ -8,5 +8,11 @@ urlpatterns = [
path(
"transaction/<uuid>/update", views.update_transaction, name="update_transaction"
),
path("transaction/<uuid>/add_invoice", views.add_invoice, name="add_invoice"),
path(
"transaction/<uuid>/del_invoice/<invoice_id>",
views.del_invoice,
name="del_invoice",
),
path("invoice/<uuid>", views.invoice, name="invoice"),
]

View file

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