From 52f654cee8480d2dadcc54c66cd721ec8d9043a6 Mon Sep 17 00:00:00 2001 From: Ryan Cross Date: Fri, 10 Jan 2025 12:13:57 -0800 Subject: [PATCH 1/2] fix: fix leap year bug in static view. Fixes #3910 --- backend/mlarchive/archive/views.py | 4 ++-- backend/mlarchive/tests/archive/views.py | 16 ++++++---------- 2 files changed, 8 insertions(+), 12 deletions(-) diff --git a/backend/mlarchive/archive/views.py b/backend/mlarchive/archive/views.py index 069b547c..beae2056 100644 --- a/backend/mlarchive/archive/views.py +++ b/backend/mlarchive/archive/views.py @@ -102,10 +102,10 @@ def get_this_next_periods(time_period): """ if time_period.month: this_period = datetime.datetime(time_period.year, time_period.month, 1, tzinfo=timezone.utc) - next_period = add_one_month(this_period) + next_period = this_period + relativedelta(months=1) else: this_period = datetime.datetime(time_period.year, 1, 1, tzinfo=timezone.utc) - next_period = this_period + datetime.timedelta(days=365) + next_period = this_period + relativedelta(years=1) return (this_period, next_period) diff --git a/backend/mlarchive/tests/archive/views.py b/backend/mlarchive/tests/archive/views.py index 47b3021d..2e99ded5 100644 --- a/backend/mlarchive/tests/archive/views.py +++ b/backend/mlarchive/tests/archive/views.py @@ -18,8 +18,7 @@ from factories import EmailListFactory, MessageFactory, UserFactory, SubscriberFactory from mlarchive.archive.models import Message, Attachment, Redirect from mlarchive.archive.views import (TimePeriod, add_nav_urls, is_small_year, - add_one_month, get_this_next_periods, get_date_endpoints, get_thread_endpoints, - DateStaticIndexView) + get_this_next_periods, get_date_endpoints, get_thread_endpoints, DateStaticIndexView) from mlarchive.utils.test_utils import login_testing_unauthorized from mlarchive.utils.test_utils import load_message @@ -82,14 +81,11 @@ def test_get_this_next_periods(static_list): assert get_this_next_periods(time_period) == ( datetime.datetime(2017, 1, 1, tzinfo=timezone.utc), datetime.datetime(2018, 1, 1, tzinfo=timezone.utc)) - - -@pytest.mark.django_db(transaction=True) -def test_add_one_month(): - date = datetime.datetime(2018, 1, 1, tzinfo=timezone.utc) - assert add_one_month(date) == datetime.datetime(2018, 2, 1, tzinfo=timezone.utc) - date = datetime.datetime(2018, 12, 1, tzinfo=timezone.utc) - assert add_one_month(date) == datetime.datetime(2019, 1, 1, tzinfo=timezone.utc) + # test leap year + time_period = TimePeriod(year=2024, month=None) + assert get_this_next_periods(time_period) == ( + datetime.datetime(2024, 1, 1, tzinfo=timezone.utc), + datetime.datetime(2025, 1, 1, tzinfo=timezone.utc)) # -------------------------------------------------- From 50f60894197e0611d842ccebbdb434f196947113 Mon Sep 17 00:00:00 2001 From: Ryan Cross Date: Wed, 15 Jan 2025 15:21:40 -0800 Subject: [PATCH 2/2] fix: remove unused function add_one_month --- backend/mlarchive/archive/views.py | 7 ------- 1 file changed, 7 deletions(-) diff --git a/backend/mlarchive/archive/views.py b/backend/mlarchive/archive/views.py index beae2056..6fdf9aa6 100644 --- a/backend/mlarchive/archive/views.py +++ b/backend/mlarchive/archive/views.py @@ -109,13 +109,6 @@ def get_this_next_periods(time_period): return (this_period, next_period) -def add_one_month(dt0): - dt1 = dt0.replace(day=1) - dt2 = dt1 + datetime.timedelta(days=32) - dt3 = dt2.replace(day=1) - return dt3 - - def is_small_year(email_list, year): count = Message.objects.filter(email_list=email_list, date__year=year).count() return count < settings.STATIC_INDEX_YEAR_MINIMUM