fix memberships ending today

This commit is contained in:
thomas girod 2024-09-04 16:21:42 +02:00
parent 6380fb193c
commit f1afa3b436
2 changed files with 19 additions and 7 deletions

View File

@ -269,7 +269,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__gte=timezone.now())) return self.filter(Q(end_date=None) | Q(end_date__gt=timezone.now().date()))
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

@ -24,6 +24,7 @@ from django.utils.translation import gettext as _
from club.forms import MailingForm from club.forms import MailingForm
from club.models import Club, Mailing, Membership from club.models import Club, Mailing, Membership
from core.baker_recipes import subscriber_user
from core.models import AnonymousUser, User from core.models import AnonymousUser, User
from sith.settings import SITH_BAR_MANAGER, SITH_MAIN_CLUB_ID from sith.settings import SITH_BAR_MANAGER, SITH_MAIN_CLUB_ID
@ -106,6 +107,18 @@ class TestMembershipQuerySet(TestClub):
expected.sort(key=lambda i: i.id) expected.sort(key=lambda i: i.id)
assert current_members == expected assert current_members == expected
def test_ongoing_with_membership_ending_today(self):
"""Test that a membership ending the present day is considered as ended."""
today = timezone.now().date()
self.richard.memberships.filter(club=self.club).update(end_date=today)
current_members = list(self.club.members.ongoing().order_by("id"))
expected = [
self.skia.memberships.get(club=self.club),
self.comptable.memberships.get(club=self.club),
]
expected.sort(key=lambda i: i.id)
assert current_members == expected
def test_board(self): def test_board(self):
"""Test that the board queryset method returns the memberships """Test that the board queryset method returns the memberships
of user in the club board. of user in the club board.
@ -422,11 +435,11 @@ class TestClubModel(TestClub):
of anyone. of anyone.
""" """
# make subscriber a board member # make subscriber a board member
self.subscriber.memberships.all().delete() subscriber = subscriber_user.make()
Membership.objects.create(club=self.ae, user=self.subscriber, role=3) Membership.objects.create(club=self.ae, user=subscriber, role=3)
nb_memberships = self.club.members.count() nb_memberships = self.club.members.ongoing().count()
self.client.force_login(self.subscriber) self.client.force_login(subscriber)
response = self.client.post( response = self.client.post(
self.members_url, self.members_url,
{"users_old": self.comptable.id}, {"users_old": self.comptable.id},
@ -437,7 +450,7 @@ class TestClubModel(TestClub):
def test_end_membership_as_root(self): def test_end_membership_as_root(self):
"""Test that root users can end the membership of anyone.""" """Test that root users can end the membership of anyone."""
nb_memberships = self.club.members.count() nb_memberships = self.club.members.ongoing().count()
self.client.force_login(self.root) self.client.force_login(self.root)
response = self.client.post( response = self.client.post(
self.members_url, self.members_url,
@ -446,7 +459,6 @@ class TestClubModel(TestClub):
self.assertRedirects(response, self.members_url) self.assertRedirects(response, self.members_url)
self.assert_membership_ended_today(self.comptable) self.assert_membership_ended_today(self.comptable)
assert self.club.members.ongoing().count() == nb_memberships - 1 assert self.club.members.ongoing().count() == nb_memberships - 1
assert self.club.members.count() == nb_memberships
def test_end_membership_as_foreigner(self): def test_end_membership_as_foreigner(self):
"""Test that users who are not in this club cannot end its memberships.""" """Test that users who are not in this club cannot end its memberships."""