diff --git a/app/apps/transactions/filters.py b/app/apps/transactions/filters.py index 3237c120..7c0a8e34 100644 --- a/app/apps/transactions/filters.py +++ b/app/apps/transactions/filters.py @@ -256,20 +256,38 @@ class TransactionsFilter(django_filters.FilterSet): Dates move by days and reference months by months, so a preset saved 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. """ - elapsed = { - "days": (until - since).days, - "months": (until.year - since.year) * 12 + until.month - since.month, + months = (until.year - since.year) * 12 + until.month - since.month + shifts = { + "days": relativedelta(days=(until - since).days), + "months": relativedelta(months=months), } - for name, unit in self.relative_date_units.items(): + dates = {} + for name in self.relative_date_units: try: - value = datetime.date.fromisoformat(self.form[name].data) + dates[name] = datetime.date.fromisoformat(self.form[name].data) except (TypeError, ValueError): - continue + pass - shifted = value + relativedelta(**{unit: elapsed[unit]}) - self.data[name] = self.form.fields[name].widget.format_value(shifted) + start, end = dates.get("date_start"), dates.get("date_end") + whole_months = ( + start + and end + and start.day == 1 + and (end + datetime.timedelta(days=1)).day == 1 + ) + + for name, value in dates.items(): + shift = shifts[self.relative_date_units[name]] + if whole_months and name == "date_start": + shift = shifts["months"] + elif whole_months and name == "date_end": + # day=31 lands on the last day of the shifted month, whatever its length + shift = shifts["months"] + relativedelta(day=31) + self.data[name] = self.form.fields[name].widget.format_value(value + shift) @staticmethod def filter_category(queryset, name, value): diff --git a/app/apps/transactions/tests/test_filter_presets.py b/app/apps/transactions/tests/test_filter_presets.py index 451ff8b1..304c1df3 100644 --- a/app/apps/transactions/tests/test_filter_presets.py +++ b/app/apps/transactions/tests/test_filter_presets.py @@ -166,6 +166,28 @@ class FilterPresetViewTests(TestCase): self.assertContains(response, 'data-value="2027-01-01"') self.assertContains(response, 'data-value="2027-02-01"') + def test_apply_keeps_a_whole_month_range_on_whole_months(self): + self.user.settings.date_format = "d/m/Y" + self.user.settings.save() + preset = FilterPreset.objects.create( + owner=self.user, + name="This month", + parameters={"date_start": ["01/01/2027"], "date_end": ["31/01/2027"]}, + relative_to=datetime.date(2027, 1, 20), + ) + + with mock.patch( + "django.utils.timezone.localdate", + return_value=datetime.date(2027, 2, 3), + ): + response = self.client.get( + reverse("filter_preset_apply", args=[preset.pk]), + HTTP_HX_REQUEST="true", + ) + + self.assertContains(response, 'data-value="2027-02-01"') + self.assertContains(response, 'data-value="2027-02-28"') + def test_apply_keeps_fixed_dates_unchanged(self): self.user.settings.date_format = "d/m/Y" self.user.settings.save()