From 836151d9d5c048e8d119d04ef79d16c9aa0f0ab0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BE=9D=E7=91=AA=E8=B2=93?= Date: Sun, 9 Aug 2020 13:19:20 +0800 Subject: [PATCH] Replaced the function-based txn_delete() view with the class-based TransactionDeleteView view in the accounting application. --- accounting/models.py | 21 ++++++++++++++++++- accounting/urls.py | 2 +- accounting/views.py | 49 ++++++++++++++++---------------------------- 3 files changed, 39 insertions(+), 33 deletions(-) diff --git a/accounting/models.py b/accounting/models.py index 7abd62a..af43c01 100644 --- a/accounting/models.py +++ b/accounting/models.py @@ -20,7 +20,8 @@ """ from dirtyfields import DirtyFieldsMixin from django.conf import settings -from django.db import models +from django.db import models, transaction +from django.db.models import Q from django.urls import reverse from mia_core.utils import get_multi_lingual_attr, set_multi_lingual_attr @@ -180,6 +181,24 @@ class Transaction(DirtyFieldsMixin, models.Model): return True return False + def delete(self, using=None, keep_parents=False): + txn_same_day = list( + Transaction.objects + .filter(Q(date=self.date), ~Q(pk=self.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 self.record_set.all(): + record.delete() + super(Transaction, self).delete( + using=using, keep_parents=keep_parents) + for txn in txn_to_sort: + txn.save() + class Meta: db_table = "accounting_transactions" diff --git a/accounting/urls.py b/accounting/urls.py index f315f39..862b19b 100644 --- a/accounting/urls.py +++ b/accounting/urls.py @@ -87,7 +87,7 @@ urlpatterns = [ path("transactions///update", views.txn_store, name="transactions.update"), path("transactions//delete", - views.txn_delete, name="transactions.delete"), + views.TransactionDeleteView.as_view(), name="transactions.delete"), path("transactions/sort/", views.txn_sort, name="transactions.sort"), path("accounts", diff --git a/accounting/views.py b/accounting/views.py index cba4044..6164165 100644 --- a/accounting/views.py +++ b/accounting/views.py @@ -26,7 +26,6 @@ from django.contrib import messages from django.db import transaction from django.db.models import Sum, Case, When, F, Q, Max, Count, BooleanField from django.db.models.functions import TruncMonth, Coalesce, Now -from django.forms import model_to_dict from django.http import JsonResponse, HttpResponseRedirect, Http404 from django.shortcuts import render from django.template.loader import render_to_string @@ -35,7 +34,7 @@ from django.utils import timezone from django.utils.decorators import method_decorator from django.utils.translation import gettext as _, gettext_noop from django.views.decorators.http import require_GET, require_POST -from django.views.generic import RedirectView, ListView, DetailView +from django.views.generic import RedirectView, ListView, DetailView, DeleteView from mia_core.digest_auth import login_required from mia_core.period import Period @@ -943,37 +942,25 @@ def txn_store(request, txn_type, txn=None): return HttpResponseRedirect(url) -@require_POST -@login_required -def txn_delete(request, txn): - """The view to delete an accounting transaction. +@method_decorator(require_POST, name="dispatch") +@method_decorator(login_required, name="dispatch") +class TransactionDeleteView(DeleteView): + """The view to delete an accounting transaction.""" - Args: - request (HttpRequest): The request. - txn (Transaction): The transaction. + def get_object(self, queryset=None): + txn = self.request.resolver_match.kwargs["txn"] + txn.request = self.request + return txn - 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() - messages.success(request, gettext_noop( - "This transaction was deleted successfully.")) - url = request.GET.get("r") or reverse("accounting:home") - return HttpResponseRedirect(url) + def delete(self, request, *args, **kwargs): + response = super(TransactionDeleteView, self)\ + .delete(request, *args, **kwargs) + messages.success(request, gettext_noop( + "This transaction was deleted successfully.")) + return response + + def get_success_url(self): + return self.request.GET.get("r") or reverse("accounting:home") @login_required