From 60b5fae43b9058407c849368b97a65100fcaef69 Mon Sep 17 00:00:00 2001 From: Rasmus Selsmark Date: Sun, 14 Jan 2024 16:52:44 +0100 Subject: [PATCH 1/2] Check for current date, to see if person can be invited to activity --- members/admin/admin_actions.py | 18 +++----- members/tests/test_admin_admin_actions.py | 55 ++++++++++++++--------- 2 files changed, 41 insertions(+), 32 deletions(-) diff --git a/members/admin/admin_actions.py b/members/admin/admin_actions.py index 443ac6aa..a567e508 100644 --- a/members/admin/admin_actions.py +++ b/members/admin/admin_actions.py @@ -152,6 +152,8 @@ class MassInvitationForm(forms.Form): with transaction.atomic(): # for current_person in queryset: for current_person in persons: + person_age = current_person.age_years() + # check for already participant if current_person.id in already_participant_ids: persons_already_participant.append( @@ -165,21 +167,13 @@ class MassInvitationForm(forms.Form): ) # Check for age constraint: too young ? - elif ( - current_person.birthday - > activity.start_date - - relativedelta(years=activity.min_age) - ): + elif person_age < activity.min_age: persons_too_young.append(current_person.name) + # Check for age constraint: too old ? - elif ( - current_person.birthday - < activity.start_date - - relativedelta( - years=activity.max_age + 1, days=-1 - ) - ): + elif person_age > activity.max_age: persons_too_old.append(current_person.name) + # Otherwise - person can be invited else: invited_counter = invited_counter + 1 diff --git a/members/tests/test_admin_admin_actions.py b/members/tests/test_admin_admin_actions.py index f9d90540..b80d8178 100644 --- a/members/tests/test_admin_admin_actions.py +++ b/members/tests/test_admin_admin_actions.py @@ -48,21 +48,18 @@ def setUp(self): self.family = Family() self.family.save() - self.person_too_young = self.create_person_and_waiting_list_entry( - str(datetime.now().year - 5) + "-01-01" - ) # 5 years old - self.person_exactly_start_age = self.create_person_and_waiting_list_entry( - str(datetime.now().year - 7) + "-01-01" - ) # 7 years old - self.person_correct_age = self.create_person_and_waiting_list_entry( - str(datetime.now().year - 10) + "-01-01" - ) # 10 years old - self.person_exactly_end_age = self.create_person_and_waiting_list_entry( - str(datetime.now().year - 17) + "-01-01" - ) # 17 years old - self.person_too_old = self.create_person_and_waiting_list_entry( - str(datetime.now().year - 18) + "-01-01" - ) # 18 years old + # create test persons with specific ages + self.person_too_young = self.create_person_and_waiting_list_entry(age=5) + self.person_exactly_start_age = self.create_person_and_waiting_list_entry(age=7) + self.person_correct_age = self.create_person_and_waiting_list_entry(age=10) + self.person_exactly_end_age = self.create_person_and_waiting_list_entry(age=17) + self.person_too_old = self.create_person_and_waiting_list_entry(age=18) + + self.person_too_young_at_activity_start = ( + self.create_person_and_waiting_list_entry( + birthday=str(datetime.now().year - 7) + "-01-02" + ) + ) # setup email template EmailTemplate.objects.create( @@ -70,9 +67,18 @@ def setUp(self): subject="test email subject", ) - def create_person_and_waiting_list_entry(self, person_birthday): + def create_person_and_waiting_list_entry(self, age=None, birthday=None): + if age is not None: + person_birthday = str(datetime.now().year - age) + "-01-01" + person_name = f"Testperson {age} år, født {person_birthday}" + elif birthday is not None: + person_birthday = birthday + person_name = f"Testperson født {person_birthday} år" + else: + raise ValueError("Either age or birthday must be specified") + person = Person.objects.create( - name=person_birthday, + name=person_name, family=self.family, birthday=datetime.fromisoformat(person_birthday), ) @@ -93,7 +99,7 @@ def create_mock_request_object(self): request.POST = { "activity": "1", "department": "1", - "expire": datetime.fromisoformat("2023-12-31"), + "expire": datetime.fromisoformat(f"{datetime.now().year}-12-31"), "email_text": "Lidt ekstra tekst", } request.user = self.user @@ -110,8 +116,12 @@ def test_invite_many_to_activity_action_correct_persons_are_invited(self): # Assert that the correct persons are invited invitations = ActivityInvite.objects.all() - self.assertEqual(invitations.count(), 3) - self.assertTrue(invitations.filter(person=self.person_correct_age).exists()) + self.assertEqual(invitations.count(), 4) + self.assertTrue( + invitations.filter( + person=self.person_correct_age, activity=self.activity + ).exists() + ) self.assertTrue( invitations.filter( person=self.person_exactly_start_age, activity=self.activity @@ -122,3 +132,8 @@ def test_invite_many_to_activity_action_correct_persons_are_invited(self): person=self.person_exactly_end_age, activity=self.activity ).exists() ) + self.assertTrue( + invitations.filter( + person=self.person_too_young_at_activity_start, activity=self.activity + ).exists() + ) From 50d3f7180afd0f561a3ea480227e6a518eb594de Mon Sep 17 00:00:00 2001 From: Rasmus Selsmark Date: Sun, 14 Jan 2024 17:13:41 +0100 Subject: [PATCH 2/2] Remove unused import --- members/admin/admin_actions.py | 1 - 1 file changed, 1 deletion(-) diff --git a/members/admin/admin_actions.py b/members/admin/admin_actions.py index a567e508..b314a1b5 100644 --- a/members/admin/admin_actions.py +++ b/members/admin/admin_actions.py @@ -1,5 +1,4 @@ from datetime import timedelta -from dateutil.relativedelta import relativedelta from django import forms from django.contrib import admin from django.contrib import messages