From 97825dfe8dade77744d3b79a518521ad3f52ee3d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BE=9D=E7=91=AA=E8=B2=93?= Date: Wed, 5 Aug 2020 08:23:20 +0800 Subject: [PATCH] Added the account deletion. --- accounting/urls.py | 3 +-- accounting/views.py | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/accounting/urls.py b/accounting/urls.py index 8c5151b..dd22e09 100644 --- a/accounting/urls.py +++ b/accounting/urls.py @@ -85,9 +85,8 @@ urlpatterns = [ views.txn_edit, name="transactions.edit"), path("transactions///update", views.txn_store, name="transactions.update"), - # TODO: To be done path("transactions//delete", - mia_core_views.todo, name="transactions.delete"), + views.txn_delete, name="transactions.delete"), # TODO: To be done path("transactions/sort/", mia_core_views.todo, name="transactions.sort"), diff --git a/accounting/views.py b/accounting/views.py index e8a8939..8169535 100644 --- a/accounting/views.py +++ b/accounting/views.py @@ -921,6 +921,39 @@ def txn_store(request, txn_type, txn=None): return success_redirect(request, url, message) +@require_POST +@login_required +def txn_delete(request, txn): + """The view to delete an accounting transaction. + + Args: + request (HttpRequest): The request. + txn (Transaction): The transaction. + + Returns: + HttpResponse: The response. + """ + txn_same_day = list(Transaction.objects\ + .filter(Q(date=txn.date), ~Q(pk=txn.pk))\ + .order_by("ord")) + txn_to_sort = [] + for i in range(len(txn_same_day)): + txn_same_day[i].ord = i + 1 + if txn_same_day[i].is_dirty(): + txn_to_sort.append(txn_same_day[i]) + with transaction.atomic(): + for record in txn.records: + record.delete() + txn.delete() + for x in txn_to_sort: + x.save() + if "r" in request.GET: + url = request.GET.get("r") + else: + url = reverse("accounting:home") + message = gettext_noop("This transaction was deleted successfully.") + return success_redirect(request, url, message) + @require_GET @login_required def account_options(request):