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 = [
path("", views.index, name="index"),
path("transaction", views.transaction, name="transaction"),
path("transaction/<uuid>", views.transaction, name="transaction"),
path(
"transaction/<uuid>/update", views.update_transaction, name="update_transaction"

View file

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