Show form errors: view rewriting with methods
This commit is contained in:
parent
11c215e4d8
commit
c3c328fdd6
8 changed files with 89 additions and 88 deletions
|
@ -50,32 +50,41 @@ class TransactionListView(LoginRequiredMixin, ListView):
|
|||
|
||||
@login_required
|
||||
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)
|
||||
_form = None
|
||||
_inv_form = None
|
||||
_invoices = []
|
||||
if request.method == "GET":
|
||||
if uuid is None:
|
||||
_transaction = Transaction()
|
||||
else:
|
||||
_transaction = get_object_or_404(Transaction, id=uuid)
|
||||
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(
|
||||
request,
|
||||
"main/transaction.html",
|
||||
{
|
||||
"transaction": _transaction,
|
||||
"form": TransactionForm(instance=_transaction),
|
||||
"invoices": _invoices,
|
||||
"invoice_form": InvoiceForm(instance=Invoice(transaction=_transaction)),
|
||||
"form": _form or TransactionForm(instance=_transaction),
|
||||
"invoices": _invoices or Invoice.objects.filter(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
|
||||
def del_transaction(request, 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")
|
||||
|
||||
|
||||
@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
|
||||
def del_invoice(request, uuid, invoice_id):
|
||||
_invoice = get_object_or_404(Invoice, id=invoice_id)
|
||||
|
@ -108,31 +108,29 @@ def del_invoice(request, uuid, invoice_id):
|
|||
|
||||
@login_required
|
||||
def category(request, uuid=None):
|
||||
if uuid is None:
|
||||
_category = Category()
|
||||
_transactions = None
|
||||
else:
|
||||
_category = get_object_or_404(Category, id=uuid)
|
||||
_transactions = Transaction.objects.filter(category=_category)
|
||||
if request.method == "GET":
|
||||
if uuid is None:
|
||||
_category = Category()
|
||||
else:
|
||||
_category = get_object_or_404(Category, id=uuid)
|
||||
_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(
|
||||
request,
|
||||
"main/category.html",
|
||||
{
|
||||
"category": _category,
|
||||
"form": CategoryForm(instance=_category),
|
||||
"transactions": _transactions,
|
||||
"form": _form,
|
||||
"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
|
||||
def del_category(request, uuid):
|
||||
_category = get_object_or_404(Category, id=uuid)
|
||||
|
@ -141,35 +139,38 @@ def del_category(request, uuid):
|
|||
|
||||
|
||||
@login_required
|
||||
def snapshot(request, date=None):
|
||||
if date is None:
|
||||
_snapshot = Snapshot()
|
||||
else:
|
||||
_snapshot = get_object_or_404(Snapshot, date=date)
|
||||
def snapshot(request, uuid=None):
|
||||
if request.method == "GET":
|
||||
if uuid is None:
|
||||
_snapshot = Snapshot()
|
||||
else:
|
||||
_snapshot = get_object_or_404(Snapshot, id=uuid)
|
||||
context = {
|
||||
"snapshot": _snapshot,
|
||||
"form": SnapshotForm(instance=_snapshot),
|
||||
}
|
||||
elif request.method == "POST":
|
||||
try:
|
||||
_snapshot = Snapshot.objects.get(id=uuid)
|
||||
except Snapshot.DoesNotExist:
|
||||
_snapshot = Snapshot(id=uuid)
|
||||
_form = SnapshotForm(request.POST, instance=_snapshot)
|
||||
if _form.is_valid():
|
||||
_form.save()
|
||||
context = {
|
||||
"snapshot": _snapshot,
|
||||
"form": _form,
|
||||
}
|
||||
|
||||
return render(
|
||||
request,
|
||||
"main/snapshot.html",
|
||||
{
|
||||
"snapshot": _snapshot,
|
||||
"form": SnapshotForm(instance=_snapshot),
|
||||
},
|
||||
context,
|
||||
)
|
||||
|
||||
|
||||
@login_required
|
||||
def update_snapshot(request, uuid):
|
||||
try:
|
||||
_snapshot = Snapshot.objects.get(id=uuid)
|
||||
except Snapshot.DoesNotExist:
|
||||
_snapshot = Snapshot(id=uuid)
|
||||
_form = SnapshotForm(request.POST, instance=_snapshot)
|
||||
if _form.is_valid():
|
||||
_form.save()
|
||||
return redirect(snapshot, date=_snapshot.date)
|
||||
|
||||
|
||||
@login_required
|
||||
def del_snapshot(request, date):
|
||||
_snapshot = get_object_or_404(Snapshot, date=date)
|
||||
def del_snapshot(request, uuid):
|
||||
_snapshot = get_object_or_404(Snapshot, id=uuid)
|
||||
_snapshot.delete()
|
||||
return redirect(index)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue