Replaced the function-based txn_delete() view with the class-based TransactionDeleteView view in the accounting application.

This commit is contained in:
依瑪貓 2020-08-09 13:19:20 +08:00
parent b84c9306cb
commit 836151d9d5
3 changed files with 39 additions and 33 deletions

View File

@ -20,7 +20,8 @@
""" """
from dirtyfields import DirtyFieldsMixin from dirtyfields import DirtyFieldsMixin
from django.conf import settings 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 django.urls import reverse
from mia_core.utils import get_multi_lingual_attr, set_multi_lingual_attr 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 True
return False 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: class Meta:
db_table = "accounting_transactions" db_table = "accounting_transactions"

View File

@ -87,7 +87,7 @@ urlpatterns = [
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"),
path("transactions/<txn:txn>/delete", path("transactions/<txn:txn>/delete",
views.txn_delete, name="transactions.delete"), views.TransactionDeleteView.as_view(), name="transactions.delete"),
path("transactions/sort/<date:date>", path("transactions/sort/<date:date>",
views.txn_sort, name="transactions.sort"), views.txn_sort, name="transactions.sort"),
path("accounts", path("accounts",

View File

@ -26,7 +26,6 @@ from django.contrib import messages
from django.db import transaction from django.db import transaction
from django.db.models import Sum, Case, When, F, Q, Max, Count, BooleanField from django.db.models import Sum, Case, When, F, Q, Max, Count, BooleanField
from django.db.models.functions import TruncMonth, Coalesce, Now 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.http import JsonResponse, HttpResponseRedirect, Http404
from django.shortcuts import render from django.shortcuts import render
from django.template.loader import render_to_string 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.decorators import method_decorator
from django.utils.translation import gettext as _, gettext_noop from django.utils.translation import gettext as _, gettext_noop
from django.views.decorators.http import require_GET, require_POST 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.digest_auth import login_required
from mia_core.period import Period from mia_core.period import Period
@ -943,37 +942,25 @@ def txn_store(request, txn_type, txn=None):
return HttpResponseRedirect(url) return HttpResponseRedirect(url)
@require_POST @method_decorator(require_POST, name="dispatch")
@login_required @method_decorator(login_required, name="dispatch")
def txn_delete(request, txn): class TransactionDeleteView(DeleteView):
"""The view to delete an accounting transaction. """The view to delete an accounting transaction."""
Args: def get_object(self, queryset=None):
request (HttpRequest): The request. txn = self.request.resolver_match.kwargs["txn"]
txn (Transaction): The transaction. txn.request = self.request
return txn
Returns: def delete(self, request, *args, **kwargs):
HttpResponse: The response. response = super(TransactionDeleteView, self)\
""" .delete(request, *args, **kwargs)
txn_same_day = list( messages.success(request, gettext_noop(
Transaction.objects "This transaction was deleted successfully."))
.filter(Q(date=txn.date), ~Q(pk=txn.pk)) return response
.order_by("ord"))
txn_to_sort = [] def get_success_url(self):
for i in range(len(txn_same_day)): return self.request.GET.get("r") or reverse("accounting:home")
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)
@login_required @login_required