fix localdate issues

This commit is contained in:
thomas girod 2024-10-03 00:21:16 +02:00 committed by Bartuccio Antoine
parent 271d57051e
commit 0eaa20e09d
9 changed files with 31 additions and 27 deletions

View File

@ -35,6 +35,7 @@ from django.db.models import Q
from django.urls import reverse from django.urls import reverse
from django.utils import timezone from django.utils import timezone
from django.utils.functional import cached_property from django.utils.functional import cached_property
from django.utils.timezone import localdate
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
from core.models import Group, MetaGroup, Notification, Page, RealGroup, SithFile, User from core.models import Group, MetaGroup, Notification, Page, RealGroup, SithFile, User
@ -269,7 +270,7 @@ class Club(models.Model):
class MembershipQuerySet(models.QuerySet): class MembershipQuerySet(models.QuerySet):
def ongoing(self) -> Self: def ongoing(self) -> Self:
"""Filter all memberships which are not finished yet.""" """Filter all memberships which are not finished yet."""
return self.filter(Q(end_date=None) | Q(end_date__gt=timezone.now().date())) return self.filter(Q(end_date=None) | Q(end_date__gt=localdate()))
def board(self) -> Self: def board(self) -> Self:
"""Filter all memberships where the user is/was in the board. """Filter all memberships where the user is/was in the board.

View File

@ -19,7 +19,7 @@ from django.core.cache import cache
from django.test import TestCase from django.test import TestCase
from django.urls import reverse from django.urls import reverse
from django.utils import timezone from django.utils import timezone
from django.utils.timezone import localtime, now from django.utils.timezone import localdate, localtime, now
from django.utils.translation import gettext as _ from django.utils.translation import gettext as _
from club.forms import MailingForm from club.forms import MailingForm
@ -109,7 +109,7 @@ class TestMembershipQuerySet(TestClub):
def test_ongoing_with_membership_ending_today(self): def test_ongoing_with_membership_ending_today(self):
"""Test that a membership ending the present day is considered as ended.""" """Test that a membership ending the present day is considered as ended."""
today = timezone.now().date() today = localdate()
self.richard.memberships.filter(club=self.club).update(end_date=today) self.richard.memberships.filter(club=self.club).update(end_date=today)
current_members = list(self.club.members.ongoing().order_by("id")) current_members = list(self.club.members.ongoing().order_by("id"))
expected = [ expected = [

View File

@ -34,6 +34,7 @@ from django.http import HttpResponseRedirect
from django.shortcuts import get_object_or_404, redirect from django.shortcuts import get_object_or_404, redirect
from django.urls import reverse, reverse_lazy from django.urls import reverse, reverse_lazy
from django.utils import timezone from django.utils import timezone
from django.utils.timezone import localdate
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
from django.views.generic import DetailView, ListView, View from django.views.generic import DetailView, ListView, View
from django.views.generic.detail import SingleObjectMixin from django.views.generic.detail import SingleObjectMixin
@ -381,8 +382,8 @@ class NewsListView(CanViewMixin, ListView):
kwargs["timedelta"] = timedelta kwargs["timedelta"] = timedelta
kwargs["birthdays"] = ( kwargs["birthdays"] = (
User.objects.filter( User.objects.filter(
date_of_birth__month=timezone.now().month, date_of_birth__month=localdate().month,
date_of_birth__day=timezone.now().day, date_of_birth__day=localdate().day,
) )
.filter(role__in=["STUDENT", "FORMER STUDENT"]) .filter(role__in=["STUDENT", "FORMER STUDENT"])
.order_by("-date_of_birth") .order_by("-date_of_birth")

View File

@ -33,6 +33,7 @@ from django.core.management import call_command
from django.core.management.base import BaseCommand from django.core.management.base import BaseCommand
from django.db import connection from django.db import connection
from django.utils import timezone from django.utils import timezone
from django.utils.timezone import localdate
from PIL import Image from PIL import Image
from accounting.models import ( from accounting.models import (
@ -914,7 +915,7 @@ Welcome to the wiki page!
Membership( Membership(
user=comunity, user=comunity,
club=bar_club, club=bar_club,
start_date=self.now, start_date=localdate(),
role=settings.SITH_CLUB_ROLES_ID["Board member"], role=settings.SITH_CLUB_ROLES_ID["Board member"],
).save() ).save()
# Adding user tutu # Adding user tutu
@ -1274,28 +1275,28 @@ Welcome to the wiki page!
club=troll, club=troll,
role=9, role=9,
description="Padawan Troll", description="Padawan Troll",
start_date=self.now - timedelta(days=17), start_date=localdate() - timedelta(days=17),
).save() ).save()
Membership( Membership(
user=krophil, user=krophil,
club=troll, club=troll,
role=10, role=10,
description="Maitre Troll", description="Maitre Troll",
start_date=self.now - timedelta(days=200), start_date=localdate() - timedelta(days=200),
).save() ).save()
Membership( Membership(
user=skia, user=skia,
club=troll, club=troll,
role=2, role=2,
description="Grand Ancien Troll", description="Grand Ancien Troll",
start_date=self.now - timedelta(days=400), start_date=localdate() - timedelta(days=400),
end_date=self.now - timedelta(days=86), end_date=localdate() - timedelta(days=86),
).save() ).save()
Membership( Membership(
user=richard, user=richard,
club=troll, club=troll,
role=2, role=2,
description="", description="",
start_date=self.now - timedelta(days=200), start_date=localdate() - timedelta(days=200),
end_date=self.now - timedelta(days=100), end_date=localdate() - timedelta(days=100),
).save() ).save()

View File

@ -9,7 +9,7 @@ from django.conf import settings
from django.core.management.base import BaseCommand from django.core.management.base import BaseCommand
from django.db.models import Count, Exists, F, Min, OuterRef, Subquery, Sum from django.db.models import Count, Exists, F, Min, OuterRef, Subquery, Sum
from django.db.models.functions import Coalesce from django.db.models.functions import Coalesce
from django.utils.timezone import make_aware, now from django.utils.timezone import localdate, make_aware, now
from faker import Faker from faker import Faker
from club.models import Club, Membership from club.models import Club, Membership
@ -125,7 +125,7 @@ class Command(BaseCommand):
account_id=f"{9900 + i}{self.faker.random_lowercase_letter()}", account_id=f"{9900 + i}{self.faker.random_lowercase_letter()}",
) )
) )
while sub.subscription_end < now().date() and random.random() > 0.7: while sub.subscription_end < localdate() and random.random() > 0.7:
# 70% chances to subscribe again # 70% chances to subscribe again
# (expect if it would make the subscription start after tomorrow) # (expect if it would make the subscription start after tomorrow)
sub = prepare_subscription( sub = prepare_subscription(
@ -331,7 +331,7 @@ class Command(BaseCommand):
seller=random.choice(sellers), seller=random.choice(sellers),
customer=customer, customer=customer,
date=make_aware( date=make_aware(
self.faker.date_time_between(customer.since, now().date()) self.faker.date_time_between(customer.since, localdate())
), ),
) )
) )
@ -347,7 +347,7 @@ class Command(BaseCommand):
operator=random.choice(sellers), operator=random.choice(sellers),
customer=customer, customer=customer,
date=make_aware( date=make_aware(
self.faker.date_time_between(customer.since, now().date()) self.faker.date_time_between(customer.since, localdate())
), ),
is_validated=True, is_validated=True,
) )
@ -368,7 +368,7 @@ class Command(BaseCommand):
active_period_start = self.faker.past_date("-10y") active_period_start = self.faker.past_date("-10y")
active_period_end = self.faker.date_between( active_period_end = self.faker.date_between(
active_period_start, active_period_start,
min(now().date(), active_period_start + relativedelta(years=5)), min(localdate(), active_period_start + relativedelta(years=5)),
) )
for _ in range(nb_perms): for _ in range(nb_perms):
counter = ( counter = (

View File

@ -23,7 +23,7 @@ import PIL
from django.conf import settings from django.conf import settings
from django.core.files.base import ContentFile from django.core.files.base import ContentFile
from django.http import HttpRequest from django.http import HttpRequest
from django.utils import timezone from django.utils.timezone import localdate
from PIL import ExifTags from PIL import ExifTags
from PIL.Image import Image, Resampling from PIL.Image import Image, Resampling
@ -45,7 +45,7 @@ def get_start_of_semester(today: Optional[date] = None) -> date:
the date of the start of the semester the date of the start of the semester
""" """
if today is None: if today is None:
today = timezone.now().date() today = localdate()
autumn = date(today.year, *settings.SITH_SEMESTER_START_AUTUMN) autumn = date(today.year, *settings.SITH_SEMESTER_START_AUTUMN)
spring = date(today.year, *settings.SITH_SEMESTER_START_SPRING) spring = date(today.year, *settings.SITH_SEMESTER_START_SPRING)
@ -73,7 +73,7 @@ def get_semester_code(d: Optional[date] = None) -> str:
the semester code corresponding to the given date the semester code corresponding to the given date
""" """
if d is None: if d is None:
d = timezone.now().date() d = localdate()
start = get_start_of_semester(d) start = get_start_of_semester(d)

View File

@ -31,7 +31,7 @@ from typing import NamedTuple, TypedDict
from django.db import models from django.db import models
from django.db.models import Case, Count, F, Q, Value, When from django.db.models import Case, Count, F, Q, Value, When
from django.db.models.functions import Concat from django.db.models.functions import Concat
from django.utils import timezone from django.utils.timezone import localdate
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
from club.models import Club from club.models import Club
@ -338,7 +338,7 @@ class Galaxy(models.Model):
for user1_membership in user1_memberships: for user1_membership in user1_memberships:
if user1_membership.end_date is None: if user1_membership.end_date is None:
# user1_membership.save() is not called in this function, hence this is safe # user1_membership.save() is not called in this function, hence this is safe
user1_membership.end_date = timezone.now().date() user1_membership.end_date = localdate()
query = Q( # start2 <= start1 <= end2 query = Q( # start2 <= start1 <= end2
start_date__lte=user1_membership.start_date, start_date__lte=user1_membership.start_date,
end_date__gte=user1_membership.start_date, end_date__gte=user1_membership.start_date,
@ -354,7 +354,7 @@ class Galaxy(models.Model):
query, club=user1_membership.club query, club=user1_membership.club
): ):
if user2_membership.end_date is None: if user2_membership.end_date is None:
user2_membership.end_date = timezone.now().date() user2_membership.end_date = localdate()
latest_start = max( latest_start = max(
user1_membership.start_date, user2_membership.start_date user1_membership.start_date, user2_membership.start_date
) )

View File

@ -28,6 +28,7 @@ from django import forms
from django.core.exceptions import PermissionDenied from django.core.exceptions import PermissionDenied
from django.urls import reverse from django.urls import reverse
from django.utils import timezone from django.utils import timezone
from django.utils.timezone import localdate
from django.utils.translation import gettext as _ from django.utils.translation import gettext as _
from django.views.generic import ListView from django.views.generic import ListView
from django.views.generic.edit import FormView from django.views.generic.edit import FormView
@ -63,8 +64,8 @@ def __merge_subscriptions(u1: User, u2: User):
subscription_end__gte=timezone.now() subscription_end__gte=timezone.now()
): ):
subscription.subscription_start = subscription_end subscription.subscription_start = subscription_end
if subscription.subscription_start > timezone.now().date(): if subscription.subscription_start > localdate():
remaining = subscription.subscription_end - timezone.now().date() remaining = subscription.subscription_end - localdate()
else: else:
remaining = ( remaining = (
subscription.subscription_end - subscription.subscription_start subscription.subscription_end - subscription.subscription_start

View File

@ -22,7 +22,7 @@ from django.contrib.auth.forms import PasswordResetForm
from django.core.exceptions import ValidationError from django.core.exceptions import ValidationError
from django.db import models from django.db import models
from django.urls import reverse from django.urls import reverse
from django.utils import timezone from django.utils.timezone import localdate
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
from core.models import User from core.models import User
@ -92,7 +92,7 @@ class Subscription(models.Model):
return reverse("core:user_edit", kwargs={"user_id": self.member.pk}) return reverse("core:user_edit", kwargs={"user_id": self.member.pk})
def clean(self): def clean(self):
today = timezone.now().date() today = localdate()
active_subscriptions = Subscription.objects.exclude(pk=self.pk).filter( active_subscriptions = Subscription.objects.exclude(pk=self.pk).filter(
subscription_start__gte=today, subscription_end__lte=today subscription_start__gte=today, subscription_end__lte=today
) )