Show form errors: view rewriting with methods

This commit is contained in:
Edgar P. Burkhart 2022-05-22 18:52:57 +02:00
parent 11c215e4d8
commit c3c328fdd6
Signed by: edpibu
GPG key ID: 9833D3C5A25BD227
8 changed files with 89 additions and 88 deletions

View file

@ -6,6 +6,14 @@ form {
line-height: 2rem; line-height: 2rem;
} }
form ul.errorlist {
grid-column: 1 / -1;
color: var(--red);
font-weight: 550;
list-style-type: "! ";
margin: 0;
}
form input, form input,
form select, form select,
form textarea { form textarea {

View file

@ -11,7 +11,7 @@
{% block body %} {% block body %}
<h1><i class="fa fa-{{ category.icon }}"></i> {{ category }}</h1> <h1><i class="fa fa-{{ category.icon }}"></i> {{ category }}</h1>
<form action="{% url 'update_category' category.id %}" method="post"> <form action="{% url 'category' category.id %}" method="post">
{% csrf_token %} {% csrf_token %}
{{ form }} {{ form }}
<div class="buttons"> <div class="buttons">

View file

@ -29,7 +29,7 @@
{% for snap in snapshots %} {% for snap in snapshots %}
<div class="snapshot {% cycle 'w' 'g' %}"> <div class="snapshot {% cycle 'w' 'g' %}">
<span class="date num center"> <span class="date num center">
<a href="{% url 'snapshot' snap.date %}">{{ snap.date|date:"Y-m-d" }}</a> <a href="{% url 'snapshot' snap.id %}">{{ snap.date|date:"Y-m-d" }}</a>
</span> </span>
<span class="value num right">{% value snap.value %}</span> <span class="value num right">{% value snap.value %}</span>
<span class="diff num right"> <span class="diff num right">

View file

@ -21,7 +21,7 @@
{{ snapshot }} {{ snapshot }}
</h1> </h1>
<form action="{% url 'update_snapshot' snapshot.id %}" method="post"> <form action="{% url 'snapshot' snapshot.id %}" method="post">
{% csrf_token %} {% csrf_token %}
{{ form }} {{ form }}
<div class="buttons"> <div class="buttons">

View file

@ -10,8 +10,9 @@
<h1>{{ transaction }}</h1> <h1>{{ transaction }}</h1>
{% spaceless %} {% spaceless %}
<form id="transaction" action="{% url 'update_transaction' transaction.id %}" method="post"> <form id="transaction" action="{% url 'transaction' transaction.id %}" method="post">
{% csrf_token %} {% csrf_token %}
<input type="hidden" name="form" value="transaction" />
{{ form }} {{ form }}
<div class="buttons"> <div class="buttons">
<a href="{% url 'del_transaction' transaction.id %}"><input type="button" value="Delete" class="btn del" /></a> <a href="{% url 'del_transaction' transaction.id %}"><input type="button" value="Delete" class="btn del" /></a>
@ -35,8 +36,9 @@
</div> </div>
<h3>Add Invoice</h3> <h3>Add Invoice</h3>
<form action="{% url 'add_invoice' transaction.id %}" method="post" enctype="multipart/form-data"> <form action="{% url 'transaction' transaction.id %}" method="post" enctype="multipart/form-data">
{% csrf_token %} {% csrf_token %}
<input type="hidden" name="form" value="invoice" />
{{ invoice_form }} {{ invoice_form }}
<div class="buttons"><input class="btn" type="submit" value="Add Invoice" /></div> <div class="buttons"><input class="btn" type="submit" value="Add Invoice" /></div>
</form> </form>

View file

@ -9,23 +9,13 @@ urlpatterns = [
path("transactions", views.TransactionListView.as_view(), name="transactions"), path("transactions", views.TransactionListView.as_view(), name="transactions"),
path("transaction", views.transaction, name="transaction"), path("transaction", views.transaction, name="transaction"),
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("transaction/<uuid>/del", views.del_transaction, name="del_transaction"), path("transaction/<uuid>/del", views.del_transaction, name="del_transaction"),
path("transaction/<uuid>/add_invoice", views.add_invoice, name="add_invoice"), path("del_invoice/<uuid>/<invoice_id>", views.del_invoice, name="del_invoice"),
path(
"transaction/<uuid>/del_invoice/<invoice_id>",
views.del_invoice,
name="del_invoice",
),
path("invoice/<uuid>", views.invoice, name="invoice"), path("invoice/<uuid>", views.invoice, name="invoice"),
path("category", views.category, name="category"), path("category", views.category, name="category"),
path("category/<uuid>", views.category, name="category"), path("category/<uuid>", views.category, name="category"),
path("category/<uuid>/update", views.update_category, name="update_category"),
path("category/<uuid>/del", views.del_category, name="del_category"), path("category/<uuid>/del", views.del_category, name="del_category"),
path("snapshot", views.snapshot, name="snapshot"), path("snapshot", views.snapshot, name="snapshot"),
path("snapshot/<date>", views.snapshot, name="snapshot"), path("snapshot/<uuid>", views.snapshot, name="snapshot"),
path("snapshot/update/<uuid>", views.update_snapshot, name="update_snapshot"), path("snapshot/<uuid>/del", views.del_snapshot, name="del_snapshot"),
path("snapshot/<date>/del", views.del_snapshot, name="del_snapshot"),
] ]

View file

@ -50,32 +50,41 @@ class TransactionListView(LoginRequiredMixin, ListView):
@login_required @login_required
def transaction(request, uuid=None): def transaction(request, uuid=None):
_form = None
_inv_form = None
_invoices = []
if request.method == "GET":
if uuid is None: if uuid is None:
_transaction = Transaction() _transaction = Transaction()
_invoices = []
else: else:
_transaction = get_object_or_404(Transaction, id=uuid) _transaction = get_object_or_404(Transaction, id=uuid)
_invoices = Invoice.objects.filter(transaction=_transaction) elif request.method == "POST":
if request.POST["form"] == "transaction":
_transaction, _ = Transaction.objects.get_or_create(id=uuid)
_form = TransactionForm(request.POST, instance=_transaction)
if _form.is_valid():
_form.save()
_inv_form = InvoiceForm(instance=Invoice(transaction=_transaction))
elif request.POST["form"] == "invoice":
_transaction = get_object_or_404(Transaction, id=uuid)
_invoice = Invoice(transaction=_transaction)
_inv_form = InvoiceForm(request.POST, request.FILES, instance=_invoice)
if _inv_form.is_valid():
_inv_form.save()
return render( return render(
request, request,
"main/transaction.html", "main/transaction.html",
{ {
"transaction": _transaction, "transaction": _transaction,
"form": TransactionForm(instance=_transaction), "form": _form or TransactionForm(instance=_transaction),
"invoices": _invoices, "invoices": _invoices or Invoice.objects.filter(transaction=_transaction),
"invoice_form": InvoiceForm(instance=Invoice(transaction=_transaction)), "invoice_form": _inv_form
or InvoiceForm(instance=Invoice(transaction=_transaction)),
}, },
) )
@login_required
def update_transaction(request, uuid):
_transaction, _ = Transaction.objects.get_or_create(id=uuid)
_form = TransactionForm(request.POST, instance=_transaction)
_form.save()
return redirect(transaction, uuid=uuid)
@login_required @login_required
def del_transaction(request, uuid): def del_transaction(request, uuid):
_transaction = get_object_or_404(Transaction, id=uuid) _transaction = get_object_or_404(Transaction, id=uuid)
@ -90,15 +99,6 @@ def invoice(request, uuid):
return HttpResponse(_file.read(), content_type="application/pdf") return HttpResponse(_file.read(), content_type="application/pdf")
@login_required
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)
@login_required @login_required
def del_invoice(request, uuid, invoice_id): def del_invoice(request, uuid, invoice_id):
_invoice = get_object_or_404(Invoice, id=invoice_id) _invoice = get_object_or_404(Invoice, id=invoice_id)
@ -108,31 +108,29 @@ def del_invoice(request, uuid, invoice_id):
@login_required @login_required
def category(request, uuid=None): def category(request, uuid=None):
if request.method == "GET":
if uuid is None: if uuid is None:
_category = Category() _category = Category()
_transactions = None
else: else:
_category = get_object_or_404(Category, id=uuid) _category = get_object_or_404(Category, id=uuid)
_transactions = Transaction.objects.filter(category=_category) _form = CategoryForm(instance=_category)
elif request.method == "POST":
_category, _ = Category.objects.get_or_create(id=uuid)
_form = CategoryForm(request.POST, instance=_category)
if _form.is_valid():
_form.save()
return render( return render(
request, request,
"main/category.html", "main/category.html",
{ {
"category": _category, "category": _category,
"form": CategoryForm(instance=_category), "form": _form,
"transactions": _transactions, "transactions": Transaction.objects.filter(category=_category),
}, },
) )
@login_required
def update_category(request, uuid):
_category, _ = Category.objects.get_or_create(id=uuid)
_form = CategoryForm(request.POST, instance=_category)
_form.save()
return redirect(category, uuid=uuid)
@login_required @login_required
def del_category(request, uuid): def del_category(request, uuid):
_category = get_object_or_404(Category, id=uuid) _category = get_object_or_404(Category, id=uuid)
@ -141,23 +139,17 @@ def del_category(request, uuid):
@login_required @login_required
def snapshot(request, date=None): def snapshot(request, uuid=None):
if date is None: if request.method == "GET":
if uuid is None:
_snapshot = Snapshot() _snapshot = Snapshot()
else: else:
_snapshot = get_object_or_404(Snapshot, date=date) _snapshot = get_object_or_404(Snapshot, id=uuid)
return render( context = {
request,
"main/snapshot.html",
{
"snapshot": _snapshot, "snapshot": _snapshot,
"form": SnapshotForm(instance=_snapshot), "form": SnapshotForm(instance=_snapshot),
}, }
) elif request.method == "POST":
@login_required
def update_snapshot(request, uuid):
try: try:
_snapshot = Snapshot.objects.get(id=uuid) _snapshot = Snapshot.objects.get(id=uuid)
except Snapshot.DoesNotExist: except Snapshot.DoesNotExist:
@ -165,11 +157,20 @@ def update_snapshot(request, uuid):
_form = SnapshotForm(request.POST, instance=_snapshot) _form = SnapshotForm(request.POST, instance=_snapshot)
if _form.is_valid(): if _form.is_valid():
_form.save() _form.save()
return redirect(snapshot, date=_snapshot.date) context = {
"snapshot": _snapshot,
"form": _form,
}
return render(
request,
"main/snapshot.html",
context,
)
@login_required @login_required
def del_snapshot(request, date): def del_snapshot(request, uuid):
_snapshot = get_object_or_404(Snapshot, date=date) _snapshot = get_object_or_404(Snapshot, id=uuid)
_snapshot.delete() _snapshot.delete()
return redirect(index) return redirect(index)