From eddfa4708f32db6cecc4bb76f8844879c6b94978 Mon Sep 17 00:00:00 2001 From: "Edgar P. Burkhart" Date: Thu, 19 May 2022 21:30:24 +0200 Subject: [PATCH] Add transaction creation --- nummi/main/urls.py | 1 + nummi/main/views.py | 17 ++++++++++++----- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/nummi/main/urls.py b/nummi/main/urls.py index 0663757..1c114be 100644 --- a/nummi/main/urls.py +++ b/nummi/main/urls.py @@ -4,6 +4,7 @@ from . import views urlpatterns = [ path("", views.index, name="index"), + path("transaction", views.transaction, name="transaction"), path("transaction/", views.transaction, name="transaction"), path( "transaction//update", views.update_transaction, name="update_transaction" diff --git a/nummi/main/views.py b/nummi/main/views.py index 84f2f91..a864949 100644 --- a/nummi/main/views.py +++ b/nummi/main/views.py @@ -6,16 +6,20 @@ from .models import Transaction, TransactionForm, Invoice, InvoiceForm def index(request): - _transactions = Transaction.objects.order_by("date")[:5] + _transactions = Transaction.objects.order_by("-date")[:5] context = { "transactions": _transactions, } return render(request, "main/index.html", context) -def transaction(request, uuid): - _transaction = get_object_or_404(Transaction, id=uuid) - _invoices = Invoice.objects.filter(transaction=_transaction) +def transaction(request, uuid=None): + if uuid is None: + _transaction = Transaction() + _invoices = [] + else: + _transaction = get_object_or_404(Transaction, id=uuid) + _invoices = Invoice.objects.filter(transaction=_transaction) return render( request, "main/transaction.html", @@ -29,7 +33,10 @@ def transaction(request, uuid): def update_transaction(request, uuid): - _transaction = get_object_or_404(Transaction, id=uuid) + try: + _transaction = Transaction.objects.get(id=uuid) + except Transaction.DoesNotExist: + _transaction = Transaction(id=uuid) _form = TransactionForm(request.POST, instance=_transaction) _form.save() return redirect(transaction, uuid=uuid)