diff --git a/app/apps/transactions/tests/test_transfers.py b/app/apps/transactions/tests/test_transfers.py index 75444e99..7f696820 100644 --- a/app/apps/transactions/tests/test_transfers.py +++ b/app/apps/transactions/tests/test_transfers.py @@ -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() diff --git a/app/apps/transactions/views/actions.py b/app/apps/transactions/views/actions.py index 287e9ca8..73c763f4 100644 --- a/app/apps/transactions/views/actions.py +++ b/app/apps/transactions/views/actions.py @@ -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( diff --git a/app/apps/transactions/views/transactions.py b/app/apps/transactions/views/transactions.py index 8c6dfa04..ef834e3f 100644 --- a/app/apps/transactions/views/transactions.py +++ b/app/apps/transactions/views/transactions.py @@ -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