Added the account deletion.

This commit is contained in:
依瑪貓 2020-08-05 08:23:20 +08:00
parent b6f5bbaf9e
commit 97825dfe8d
2 changed files with 34 additions and 2 deletions

View File

@ -85,9 +85,8 @@ urlpatterns = [
views.txn_edit, name="transactions.edit"), views.txn_edit, name="transactions.edit"),
path("transactions/<txn-type:txn_type>/<txn:txn>/update", path("transactions/<txn-type:txn_type>/<txn:txn>/update",
views.txn_store, name="transactions.update"), views.txn_store, name="transactions.update"),
# TODO: To be done
path("transactions/<txn:txn>/delete", path("transactions/<txn:txn>/delete",
mia_core_views.todo, name="transactions.delete"), views.txn_delete, name="transactions.delete"),
# TODO: To be done # TODO: To be done
path("transactions/sort/<date:date>", path("transactions/sort/<date:date>",
mia_core_views.todo, name="transactions.sort"), mia_core_views.todo, name="transactions.sort"),

View File

@ -921,6 +921,39 @@ def txn_store(request, txn_type, txn=None):
return success_redirect(request, url, message) 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 @require_GET
@login_required @login_required
def account_options(request): def account_options(request):