feat(transactions): keep whole-month preset ranges on whole months

A preset whose dates run from the 1st to the last day of a month moved
by days, so a "this month" range drifted into the next month. Such a
range now moves by months, ending on the last day of its new month.
This commit is contained in:
Herculino Trotta committed 2026-11-14 11:27:38 -03:00
1 parent 5cc68f4bfc
commit c5717d41c2
2 files changed
+48 -8

No files matched your search

+26 -8
View File
@@ -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):
@@ -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()