diff --git a/app/apps/transactions/filters.py b/app/apps/transactions/filters.py index 7c0a8e34..7196a277 100644 --- a/app/apps/transactions/filters.py +++ b/app/apps/transactions/filters.py @@ -258,6 +258,9 @@ class TransactionsFilter(django_filters.FilterSet): from today, or for the current month, keeps meaning that later on. A date range of whole months, like the 1st to the 31st, moves by months instead, so it stays whole months. + + Dates are read as ISO, or as typed in the user's format by presets saved + before they were stored as ISO, and written back in the user's format. """ months = (until.year - since.year) * 12 + until.month - since.month shifts = { @@ -267,10 +270,12 @@ class TransactionsFilter(django_filters.FilterSet): dates = {} for name in self.relative_date_units: - try: - dates[name] = datetime.date.fromisoformat(self.form[name].data) - except (TypeError, ValueError): - pass + for value in (self.data.get(name), self.form[name].data): + try: + dates[name] = datetime.date.fromisoformat(value) + break + except (TypeError, ValueError): + pass start, end = dates.get("date_start"), dates.get("date_end") whole_months = ( diff --git a/app/apps/transactions/tests/test_filter_presets.py b/app/apps/transactions/tests/test_filter_presets.py index 304c1df3..2cfdd161 100644 --- a/app/apps/transactions/tests/test_filter_presets.py +++ b/app/apps/transactions/tests/test_filter_presets.py @@ -55,6 +55,8 @@ class FilterPresetViewTests(TestCase): self.assertContains(response, "Account X Unpaid") def test_create_keeps_dates_relative_to_today_when_asked(self): + self.user.settings.date_format = "d/m/Y" + self.user.settings.save() today = datetime.date(2026, 8, 20) with mock.patch("django.utils.timezone.localdate", return_value=today): @@ -70,7 +72,7 @@ class FilterPresetViewTests(TestCase): preset = FilterPreset.objects.get(owner=self.user, name="Today") self.assertEqual(preset.relative_to, today) - self.assertEqual(preset.parameters, {"date_start": ["20/08/2026"]}) + self.assertEqual(preset.parameters, {"date_start": ["2026-08-20"]}) def test_create_keeps_dates_fixed_by_default(self): self.client.post( @@ -188,6 +190,26 @@ class FilterPresetViewTests(TestCase): self.assertContains(response, 'data-value="2027-02-01"') self.assertContains(response, 'data-value="2027-02-28"') + def test_apply_shows_stored_dates_in_the_users_current_format(self): + self.user.settings.date_format = "m/d/Y" + self.user.settings.save() + preset = FilterPreset.objects.create( + owner=self.user, + name="August", + parameters={ + "date_start": ["2026-08-20"], + "reference_date_start": ["2026-08-01"], + }, + ) + + response = self.client.get( + reverse("filter_preset_apply", args=[preset.pk]), + HTTP_HX_REQUEST="true", + ) + + self.assertContains(response, 'data-value="2026-08-20"') + self.assertContains(response, 'value="August 2026"') + def test_apply_keeps_fixed_dates_unchanged(self): self.user.settings.date_format = "d/m/Y" self.user.settings.save() diff --git a/app/apps/transactions/views/transactions.py b/app/apps/transactions/views/transactions.py index 55cc7cc5..19abd33e 100644 --- a/app/apps/transactions/views/transactions.py +++ b/app/apps/transactions/views/transactions.py @@ -713,6 +713,13 @@ def filter_preset_create(request): for key in TransactionsFilter.base_filters if key in request.POST } + # Dates are stored as ISO, so a later change of date format or language + # doesn't stop the preset from reading them + date_form = TransactionsFilter(request.POST).form + for key in TransactionsFilter.relative_date_units: + if date_form[key].data: + parameters[key] = [date_form[key].data] + has_dates = any( any(parameters.get(key, [])) for key in TransactionsFilter.relative_date_units ) @@ -744,10 +751,9 @@ def filter_preset_apply(request, preset_id): data.setlist(key, values) transaction_filter = TransactionsFilter(data) - if preset.relative_to: - transaction_filter.shift_dates( - since=preset.relative_to, until=timezone.localdate(timezone.now()) - ) + # Fixed presets shift by nothing, which still puts ISO dates in the user's format + today = timezone.localdate(timezone.now()) + transaction_filter.shift_dates(since=preset.relative_to or today, until=today) response = render( request,