Merge pull request #713 from officialasishkumar/fix/duplicate-transaction-rules

fix(transactions): run creation rules on duplicates, not on the originals
This commit is contained in:
Herculino Trotta authored and GitHub committed 2026-12-05 02:39:11 -03:00
commit 3b70746a3c
3 files changed
+78 -4

No files matched your search

@@ -622,6 +622,73 @@ class TransferRuleSignalTests(TransferTestBase):
self.assertEqual(old_amounts, {Decimal("100"), Decimal("98")})
class DuplicateRuleSignalTests(TransferTestBase):
"""Duplicates are new transactions: creation rules run on them, not on the originals."""
def setUp(self):
super().setUp()
self.created = []
def receiver(sender, **kwargs):
self.created.append(sender.pk)
transaction_created.connect(receiver, weak=False)
self.addCleanup(transaction_created.disconnect, receiver)
def _new_pks(self, *originals):
return set(
Transaction.objects.exclude(pk__in=[t.pk for t in originals]).values_list(
"pk", flat=True
)
)
def test_duplicate_runs_rules_on_the_copy(self):
coffee = Transaction.objects.create(
account=self.checking, type="EX", date=date(2026, 3, 10), amount=4
)
response = self.client.get(
reverse("transaction_clone", kwargs={"transaction_id": coffee.id}),
HTTP_HX_REQUEST="true",
)
self.assertEqual(response.status_code, 204)
copy_pks = self._new_pks(coffee)
self.assertEqual(len(copy_pks), 1)
self.assertEqual(self.created, list(copy_pks))
def test_duplicating_a_transfer_runs_rules_on_both_new_sides(self):
from_t, to_t = self._transfer()
self.created.clear()
self.client.get(
reverse("transaction_clone", kwargs={"transaction_id": to_t.id}),
HTTP_HX_REQUEST="true",
)
new_pks = self._new_pks(from_t, to_t)
self.assertEqual(len(new_pks), 2)
self.assertEqual(sorted(self.created), sorted(new_pks))
def test_bulk_duplicate_runs_rules_on_each_copy(self):
coffee = Transaction.objects.create(
account=self.checking, type="EX", date=date(2026, 3, 10), amount=4
)
from_t, to_t = self._transfer()
self.created.clear()
response = self.client.get(
reverse("transactions_bulk_clone")
+ f"?transactions={coffee.id}&transactions={from_t.id}&transactions={to_t.id}",
HTTP_HX_REQUEST="true",
)
self.assertEqual(response.status_code, 204)
new_pks = self._new_pks(coffee, from_t, to_t)
self.assertEqual(len(new_pks), 3) # the transfer is copied once, as a pair
self.assertEqual(sorted(self.created), sorted(new_pks))
class TransferReviewFixTests(TransferTestBase):
def test_pair_of_two_incomes_stays_listed(self):
from_t, to_t = self._transfer()
+6 -2
View File
@@ -11,7 +11,7 @@ from django.utils.translation import gettext_lazy as _, ngettext_lazy
from apps.common.decorators.htmx import only_htmx
from apps.transactions.models import Transaction
from apps.rules.signals import transaction_updated
from apps.rules.signals import transaction_created, transaction_updated
def _selected(manager, request):
@@ -238,7 +238,11 @@ def bulk_clone_transactions(request):
# Both sides of a transfer selected: clone the pair once
if transaction.linked_transaction_id in cloned_ids:
continue
clone_transaction(transaction)
new_transaction = clone_transaction(transaction)
# Rules run on each copy, like on any other new transaction
for duplicate in (new_transaction, new_transaction.linked_transaction):
if duplicate:
transaction_created.send(sender=duplicate)
cloned_ids.add(transaction.id)
messages.success(
+5 -2
View File
@@ -446,11 +446,14 @@ def transactions_bulk_edit(request):
@require_http_methods(["GET", "POST"])
def transaction_clone(request, transaction_id, **kwargs):
transaction = get_object_or_404(Transaction, id=transaction_id)
clone_transaction(transaction)
new_transaction = clone_transaction(transaction)
messages.success(request, _("Transaction duplicated successfully"))
transaction_created.send(sender=transaction)
# Rules run on each copy, like on any other new transaction
for duplicate in (new_transaction, new_transaction.linked_transaction):
if duplicate:
transaction_created.send(sender=duplicate)
# THIS HAS BEEN DISABLE DUE TO HTMX INCOMPATIBILITY
# SEE https://github.com/bigskysoftware/htmx/issues/3115 and https://github.com/bigskysoftware/htmx/issues/2706