Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix leap year bug in static view. Fixes #3910 #3912

Merged
merged 2 commits into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 2 additions & 9 deletions backend/mlarchive/archive/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,20 +102,13 @@ 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)


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
Expand Down
16 changes: 6 additions & 10 deletions backend/mlarchive/tests/archive/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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))


# --------------------------------------------------
Expand Down
Loading