Add form for transactions

This commit is contained in:
Edgar P. Burkhart 2022-05-19 20:33:36 +02:00
parent 4ef99f4ef0
commit 5339a725f2
Signed by: edpibu
GPG key ID: 9833D3C5A25BD227
4 changed files with 26 additions and 3 deletions

View file

@ -1,5 +1,6 @@
import uuid import uuid
from django.db import models from django.db import models
from django.forms import ModelForm
class Transaction(models.Model): class Transaction(models.Model):
@ -13,6 +14,12 @@ class Transaction(models.Model):
return f"{self.date} {self.name}: {self.value}" return f"{self.date} {self.name}: {self.value}"
class TransactionForm(ModelForm):
class Meta:
model = Transaction
fields = ["name", "description", "value", "date"]
class Invoice(models.Model): class Invoice(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
name = models.CharField(max_length=256) name = models.CharField(max_length=256)

View file

@ -1,4 +1,9 @@
<h1>{{ transaction }}</h1> <form action="{% url 'update_transaction' transaction.id %}" method="post">
{% csrf_token %}
{{ form }}
<input type="submit" />
</form>
<ul> <ul>
{% for inv in invoices %} {% 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></li>

View file

@ -5,5 +5,8 @@ from . import views
urlpatterns = [ urlpatterns = [
path("", views.index, name="index"), path("", views.index, name="index"),
path("transaction/<uuid>", views.transaction, name="transaction"), path("transaction/<uuid>", views.transaction, name="transaction"),
path(
"transaction/<uuid>/update", views.update_transaction, name="update_transaction"
),
path("invoice/<uuid>", views.invoice, name="invoice"), path("invoice/<uuid>", views.invoice, name="invoice"),
] ]

View file

@ -1,8 +1,8 @@
from django.shortcuts import render, get_object_or_404 from django.shortcuts import render, get_object_or_404, redirect
from django.http import HttpResponse from django.http import HttpResponse
from .models import Transaction, Invoice from .models import Transaction, TransactionForm, Invoice
def index(request): def index(request):
@ -21,11 +21,19 @@ def transaction(request, uuid):
"main/transaction.html", "main/transaction.html",
{ {
"transaction": _transaction, "transaction": _transaction,
"form": TransactionForm(instance=_transaction),
"invoices": _invoices, "invoices": _invoices,
}, },
) )
def update_transaction(request, uuid):
_transaction = get_object_or_404(Transaction, id=uuid)
_form = TransactionForm(request.POST, instance=_transaction)
_form.save()
return redirect(transaction, uuid=uuid)
def invoice(request, uuid): def invoice(request, uuid):
_invoice = get_object_or_404(Invoice, id=uuid) _invoice = get_object_or_404(Invoice, id=uuid)
with _invoice.file.open() as _file: with _invoice.file.open() as _file: