From c765d8ec860119ebb87599b382b5c7eb51aa5a1b 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:29:30 +0800 Subject: [PATCH] Revised the algorithm to find the transactions to reorder when the date of an existing transaction is changed in a transaction form in the accounting application. --- accounting/views.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/accounting/views.py b/accounting/views.py index 8169535..1acda09 100644 --- a/accounting/views.py +++ b/accounting/views.py @@ -869,7 +869,6 @@ def txn_store(request, txn_type, txn=None): if txn is None: txn = Transaction() old_date = txn.date - old_ord = txn.ord fill_txn_from_post(txn_type, txn, post) if not txn.is_dirty(): url = reverse("accounting:transactions.show", args=(txn_type, txn)) @@ -886,9 +885,13 @@ def txn_store(request, txn_type, txn=None): txn_to_sort = [] if txn.date != old_date: if old_date is not None: - txn_to_sort = Transaction.objects\ - .filter(date=old_date, ord__gt=old_ord)\ - .order_by("ord") + txn_same_day = list(Transaction.objects\ + .filter(Q(date=old_date), ~Q(pk=txn.pk))\ + .order_by("ord")) + 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]) max_ord = Transaction.objects\ .filter(date=txn.date)\ .aggregate(max=Max("ord"))["max"] @@ -902,8 +905,6 @@ def txn_store(request, txn_type, txn=None): record.created_by = user record.updated_at = Now() record.updated_by = user - for x in txn_to_sort: - x.ord = x.ord - 1 to_keep = [x.pk for x in txn.records if x.pk is not None] to_delete = [x for x in txn.record_set.all() if x.pk not in to_keep] # Runs the update