Add transaction creation

This commit is contained in:
Edgar P. Burkhart 2022-05-19 21:30:24 +02:00
parent 6ef5a441e1
commit eddfa4708f
Signed by: edpibu
GPG key ID: 9833D3C5A25BD227
2 changed files with 13 additions and 5 deletions

View file

@ -4,6 +4,7 @@ from . import views
urlpatterns = [ urlpatterns = [
path("", views.index, name="index"), path("", views.index, name="index"),
path("transaction", views.transaction, name="transaction"),
path("transaction/<uuid>", views.transaction, name="transaction"), path("transaction/<uuid>", views.transaction, name="transaction"),
path( path(
"transaction/<uuid>/update", views.update_transaction, name="update_transaction" "transaction/<uuid>/update", views.update_transaction, name="update_transaction"

View file

@ -6,14 +6,18 @@ from .models import Transaction, TransactionForm, Invoice, InvoiceForm
def index(request): def index(request):
_transactions = Transaction.objects.order_by("date")[:5] _transactions = Transaction.objects.order_by("-date")[:5]
context = { context = {
"transactions": _transactions, "transactions": _transactions,
} }
return render(request, "main/index.html", context) return render(request, "main/index.html", context)
def transaction(request, uuid): def transaction(request, uuid=None):
if uuid is None:
_transaction = Transaction()
_invoices = []
else:
_transaction = get_object_or_404(Transaction, id=uuid) _transaction = get_object_or_404(Transaction, id=uuid)
_invoices = Invoice.objects.filter(transaction=_transaction) _invoices = Invoice.objects.filter(transaction=_transaction)
return render( return render(
@ -29,7 +33,10 @@ def transaction(request, uuid):
def update_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 = TransactionForm(request.POST, instance=_transaction)
_form.save() _form.save()
return redirect(transaction, uuid=uuid) return redirect(transaction, uuid=uuid)