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.utils import timezone
from django.utils.functional import cached_property
from django.utils.timezone import localdate
from django.utils.translation import gettext_lazy as _
from core.models import Group, MetaGroup, Notification, Page, RealGroup, SithFile, User
@ -269,7 +270,7 @@ class Club(models.Model):
class MembershipQuerySet(models.QuerySet):
def ongoing(self) -> Self:
"""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:
"""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.urls import reverse
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 club.forms import MailingForm
@ -109,7 +109,7 @@ class TestMembershipQuerySet(TestClub):
def test_ongoing_with_membership_ending_today(self):
"""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)
current_members = list(self.club.members.ongoing().order_by("id"))
expected = [

View File

@ -34,6 +34,7 @@ from django.http import HttpResponseRedirect
from django.shortcuts import get_object_or_404, redirect
from django.urls import reverse, reverse_lazy
from django.utils import timezone
from django.utils.timezone import localdate
from django.utils.translation import gettext_lazy as _
from django.views.generic import DetailView, ListView, View
from django.views.generic.detail import SingleObjectMixin
@ -381,8 +382,8 @@ class NewsListView(CanViewMixin, ListView):
kwargs["timedelta"] = timedelta
kwargs["birthdays"] = (
User.objects.filter(
date_of_birth__month=timezone.now().month,
date_of_birth__day=timezone.now().day,
date_of_birth__month=localdate().month,
date_of_birth__day=localdate().day,
)
.filter(role__in=["STUDENT", "FORMER STUDENT"])
.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.db import connection
from django.utils import timezone
from django.utils.timezone import localdate
from PIL import Image
from accounting.models import (
@ -914,7 +915,7 @@ Welcome to the wiki page!
Membership(
user=comunity,
club=bar_club,
start_date=self.now,
start_date=localdate(),
role=settings.SITH_CLUB_ROLES_ID["Board member"],
).save()
# Adding user tutu
@ -1274,28 +1275,28 @@ Welcome to the wiki page!
club=troll,
role=9,
description="Padawan Troll",
start_date=self.now - timedelta(days=17),
start_date=localdate() - timedelta(days=17),
).save()
Membership(
user=krophil,
club=troll,
role=10,
description="Maitre Troll",
start_date=self.now - timedelta(days=200),
start_date=localdate() - timedelta(days=200),
).save()
Membership(
user=skia,
club=troll,
role=2,
description="Grand Ancien Troll",
start_date=self.now - timedelta(days=400),
end_date=self.now - timedelta(days=86),
start_date=localdate() - timedelta(days=400),
end_date=localdate() - timedelta(days=86),
).save()
Membership(
user=richard,
club=troll,
role=2,
description="",
start_date=self.now - timedelta(days=200),
end_date=self.now - timedelta(days=100),
start_date=localdate() - timedelta(days=200),
end_date=localdate() - timedelta(days=100),
).save()

View File

@ -9,7 +9,7 @@ from django.conf import settings
from django.core.management.base import BaseCommand
from django.db.models import Count, Exists, F, Min, OuterRef, Subquery, Sum
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 club.models import Club, Membership
@ -125,7 +125,7 @@ class Command(BaseCommand):
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
# (expect if it would make the subscription start after tomorrow)
sub = prepare_subscription(
@ -331,7 +331,7 @@ class Command(BaseCommand):
seller=random.choice(sellers),
customer=customer,
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),
customer=customer,
date=make_aware(
self.faker.date_time_between(customer.since, now().date())
self.faker.date_time_between(customer.since, localdate())
),
is_validated=True,
)
@ -368,7 +368,7 @@ class Command(BaseCommand):
active_period_start = self.faker.past_date("-10y")
active_period_end = self.faker.date_between(
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):
counter = (

View File

@ -23,7 +23,7 @@ import PIL
from django.conf import settings
from django.core.files.base import ContentFile
from django.http import HttpRequest
from django.utils import timezone
from django.utils.timezone import localdate
from PIL import ExifTags
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
"""
if today is None:
today = timezone.now().date()
today = localdate()
autumn = date(today.year, *settings.SITH_SEMESTER_START_AUTUMN)
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
"""
if d is None:
d = timezone.now().date()
d = localdate()
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.models import Case, Count, F, Q, Value, When
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 club.models import Club
@ -338,7 +338,7 @@ class Galaxy(models.Model):
for user1_membership in user1_memberships:
if user1_membership.end_date is None:
# 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
start_date__lte=user1_membership.start_date,
end_date__gte=user1_membership.start_date,
@ -354,7 +354,7 @@ class Galaxy(models.Model):
query, club=user1_membership.club
):
if user2_membership.end_date is None:
user2_membership.end_date = timezone.now().date()
user2_membership.end_date = localdate()
latest_start = max(
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.urls import reverse
from django.utils import timezone
from django.utils.timezone import localdate
from django.utils.translation import gettext as _
from django.views.generic import ListView
from django.views.generic.edit import FormView
@ -63,8 +64,8 @@ def __merge_subscriptions(u1: User, u2: User):
subscription_end__gte=timezone.now()
):
subscription.subscription_start = subscription_end
if subscription.subscription_start > timezone.now().date():
remaining = subscription.subscription_end - timezone.now().date()
if subscription.subscription_start > localdate():
remaining = subscription.subscription_end - localdate()
else:
remaining = (
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.db import models
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 core.models import User
@ -92,7 +92,7 @@ class Subscription(models.Model):
return reverse("core:user_edit", kwargs={"user_id": self.member.pk})
def clean(self):
today = timezone.now().date()
today = localdate()
active_subscriptions = Subscription.objects.exclude(pk=self.pk).filter(
subscription_start__gte=today, subscription_end__lte=today
)