Skip to content

Commit

Permalink
Merge pull request #996 from CodingPirates/975-invite-check-age-today…
Browse files Browse the repository at this point in the history
…s-date

[#975] Check for alder på dags dato om person kan inviteres til aktivitet
  • Loading branch information
lakridserne authored Jan 14, 2024
2 parents 3136669 + 50d3f71 commit 7c0fc08
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 33 deletions.
19 changes: 6 additions & 13 deletions members/admin/admin_actions.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -152,6 +151,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(
Expand All @@ -165,21 +166,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
Expand Down
55 changes: 35 additions & 20 deletions members/tests/test_admin_admin_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,31 +48,37 @@ 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(
idname="ACT_INVITE",
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),
)
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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()
)

0 comments on commit 7c0fc08

Please sign in to comment.