diff --git a/app/apps/transactions/tests/test_transfers.py b/app/apps/transactions/tests/test_transfers.py index cdaf7a16..adf31b52 100644 --- a/app/apps/transactions/tests/test_transfers.py +++ b/app/apps/transactions/tests/test_transfers.py @@ -439,3 +439,62 @@ class TransferReviewFixTests(TransferTestBase): partner.refresh_from_db() self.assertIsNone(partner.linked_transaction) + + +class BulkActionTests(TransferTestBase): + def _get(self, name, *ids, **kwargs): + query = "&".join(f"transactions={pk}" for pk in ids) + return self.client.get( + reverse(name, kwargs=kwargs) + f"?{query}", HTTP_HX_REQUEST="true" + ) + + def _expense(self, **fields): + return Transaction.objects.create( + account=self.checking, + type=Transaction.Type.EXPENSE, + amount=Decimal("10"), + date=date(2026, 3, 10), + description="Coffee", + **fields, + ) + + def test_change_month_updates_a_fully_selected_transfer_once_per_side(self): + from_t, to_t = self._transfer() + updated = [] + receiver = lambda sender, **kwargs: updated.append(sender.pk) # noqa: E731 + transaction_updated.connect(receiver, weak=False) + self.addCleanup(transaction_updated.disconnect, receiver) + + response = self._get( + "transactions_bulk_change_month", from_t.id, to_t.id, change_type="next" + ) + + self.assertEqual(response.status_code, 204) + from_t.refresh_from_db() + to_t.refresh_from_db() + self.assertEqual(from_t.reference_date, date(2026, 4, 1)) + self.assertEqual(to_t.reference_date, date(2026, 4, 1)) + self.assertEqual(sorted(updated), sorted([from_t.pk, to_t.pk])) # once per side + + def test_change_month_rejects_unknown_direction(self): + t = self._expense() + response = self._get("transactions_bulk_change_month", t.id, change_type="sideways") + self.assertEqual(response.status_code, 400) + + def test_move_to_today(self): + t = self._expense() + self._get("transactions_bulk_move_to_today", t.id) + t.refresh_from_db() + self.assertEqual(t.date, timezone.localdate(timezone.now())) + + def test_mute_skips_transactions_controlled_elsewhere(self): + muted_category = TransactionCategory.objects.create(name="Hidden", mute=True) + plain = self._expense() + by_category = self._expense(category=muted_category) + + self._get("transactions_bulk_mute", plain.id, by_category.id) + + plain.refresh_from_db() + by_category.refresh_from_db() + self.assertTrue(plain.mute) + self.assertFalse(by_category.mute) diff --git a/app/apps/transactions/urls.py b/app/apps/transactions/urls.py index fa54b3a6..8c430ba2 100644 --- a/app/apps/transactions/urls.py +++ b/app/apps/transactions/urls.py @@ -81,6 +81,21 @@ urlpatterns = [ views.bulk_clone_transactions, name="transactions_bulk_clone", ), + path( + "transactions/actions/change-month//", + views.bulk_change_month, + name="transactions_bulk_change_month", + ), + path( + "transactions/actions/move-to-today/", + views.bulk_move_to_today, + name="transactions_bulk_move_to_today", + ), + path( + "transactions/actions/mute/", + views.bulk_mute, + name="transactions_bulk_mute", + ), path( "transaction//pay/", views.transaction_pay, diff --git a/app/apps/transactions/views/actions.py b/app/apps/transactions/views/actions.py index eec3af02..fdaf3a0f 100644 --- a/app/apps/transactions/views/actions.py +++ b/app/apps/transactions/views/actions.py @@ -1,10 +1,12 @@ import json from copy import deepcopy +from dateutil.relativedelta import relativedelta from django.contrib import messages from django.contrib.auth.decorators import login_required from django.db.models import Q from django.http import HttpResponse +from django.utils import timezone from django.utils.translation import gettext_lazy as _, ngettext_lazy from apps.common.decorators.htmx import only_htmx @@ -149,6 +151,80 @@ def bulk_undelete_transactions(request): ) +def _bulk_update(request, transactions, change): + """Applies change to each selected transaction like its single action does. + + A transfer moves as a pair through save(), so a selected partner is skipped. + """ + done = set() + for transaction in transactions: + if transaction.linked_transaction_id in done: + continue + old_data = deepcopy(transaction) + change(transaction) + transaction.save() + transaction_updated.send(sender=transaction, old_data=old_data) + done.add(transaction.id) + + count = len(done) + messages.success( + request, + ngettext_lazy( + "%(count)s transaction updated successfully", + "%(count)s transactions updated successfully", + count, + ) + % {"count": count}, + ) + return HttpResponse(status=204, headers={"HX-Trigger": "updated"}) + + +@only_htmx +@login_required +def bulk_change_month(request, change_type): + months = {"previous": -1, "next": 1}.get(change_type) + if months is None: + return HttpResponse(status=400) + + def change(transaction): + transaction.reference_date += relativedelta(months=months) + + return _bulk_update( + request, + Transaction.objects.filter(id__in=request.GET.getlist("transactions")), + change, + ) + + +@only_htmx +@login_required +def bulk_move_to_today(request): + today = timezone.localdate(timezone.now()) + + def change(transaction): + transaction.date = today + + return _bulk_update( + request, + Transaction.objects.filter(id__in=request.GET.getlist("transactions")), + change, + ) + + +@only_htmx +@login_required +def bulk_mute(request): + # Visibility controlled by the account or category can't be changed here. + transactions = Transaction.objects.filter( + id__in=request.GET.getlist("transactions"), mute=False + ).exclude(Q(account__untracked_by=request.user) | Q(category__mute=True)) + + def change(transaction): + transaction.mute = True + + return _bulk_update(request, transactions, change) + + @only_htmx @login_required def bulk_clone_transactions(request): diff --git a/app/templates/cotton/transaction/item.html b/app/templates/cotton/transaction/item.html index 9da2e221..9751fc63 100644 --- a/app/templates/cotton/transaction/item.html +++ b/app/templates/cotton/transaction/item.html @@ -15,7 +15,7 @@
+ w-full min-w-0 transaction-item bg-base-100 rounded">
{% if not transaction.deleted %} diff --git a/app/templates/cotton/ui/deleted_transactions_action_bar.html b/app/templates/cotton/ui/deleted_transactions_action_bar.html index 6b6d5864..918ab801 100644 --- a/app/templates/cotton/ui/deleted_transactions_action_bar.html +++ b/app/templates/cotton/ui/deleted_transactions_action_bar.html @@ -17,38 +17,38 @@ end end">
-
+
{% spaceless %}
0
-
- -
+
-
+
- - + + -
+
- - -
+
+ {# Below md every action folds into a single menu so the bar doesn't overflow #} + +
+ + +
+