From 3964b602a93a2c3797f718e13363d582cdbb28ae Mon Sep 17 00:00:00 2001 From: mhewel <58829786+mhewel@users.noreply.github.com> Date: Sun, 27 Oct 2024 14:29:32 +0100 Subject: [PATCH] Notifikation om invitationer MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Filtrer så deltagere ikke vises (dvs hvis man har tilmeldt sig efter modtaget invitation). --- members/views/EntryPage.py | 10 +++------- members/views/FamilyDetails.py | 9 +++------ members/views/UnacceptedInvitations.py | 26 ++++++++++++++++++++++++++ 3 files changed, 32 insertions(+), 13 deletions(-) create mode 100644 members/views/UnacceptedInvitations.py diff --git a/members/views/EntryPage.py b/members/views/EntryPage.py index 00b29fee..cd29d3f6 100644 --- a/members/views/EntryPage.py +++ b/members/views/EntryPage.py @@ -1,9 +1,8 @@ from django.views.decorators.clickjacking import xframe_options_exempt from django.shortcuts import render -from django.utils import timezone -from members.models.activityinvite import ActivityInvite from members.utils.user import user_to_person +from members.views.UnacceptedInvitations import get_unaccepted_invitations_for_family @xframe_options_exempt @@ -15,12 +14,9 @@ def EntryPage(request): context = {} else: family = user.family - invites = ActivityInvite.objects.filter( - person__family=family, expire_dtm__gte=timezone.now(), rejected_at=None - ) - + unaccepted_invitations = get_unaccepted_invitations_for_family(family) context = { - "invites": invites, + "invites": unaccepted_invitations, } else: context = {} diff --git a/members/views/FamilyDetails.py b/members/views/FamilyDetails.py index 92b2358e..c692b010 100644 --- a/members/views/FamilyDetails.py +++ b/members/views/FamilyDetails.py @@ -4,9 +4,9 @@ from django.utils import timezone from django.contrib.auth.decorators import login_required, user_passes_test -from members.models.activityinvite import ActivityInvite from members.models.person import Person from members.utils.user import user_to_person, has_user +from members.views.UnacceptedInvitations import get_unaccepted_invitations_for_family @login_required @@ -23,10 +23,7 @@ def FamilyDetails(request): < timezone.now() - datetime.timedelta(days=settings.REQUEST_FAMILY_VALIDATION_PERIOD) ) - - invites = ActivityInvite.objects.filter( - person__family=family, expire_dtm__gte=timezone.now(), rejected_at=None - ) + unaccepted_invitations = get_unaccepted_invitations_for_family(family) context = { "family": family, @@ -35,6 +32,6 @@ def FamilyDetails(request): "request_parents": family.person_set.exclude(membertype=Person.CHILD).count() < 1, "ordered_persons": family.person_set.order_by("membertype").all(), - "invites": invites, + "invites": unaccepted_invitations, } return render(request, "members/family_details.html", context) diff --git a/members/views/UnacceptedInvitations.py b/members/views/UnacceptedInvitations.py new file mode 100644 index 00000000..b37d6794 --- /dev/null +++ b/members/views/UnacceptedInvitations.py @@ -0,0 +1,26 @@ +from django.utils import timezone +from django.db.models import OuterRef, Exists + +from members.models.activityinvite import ActivityInvite +from members.models.activityparticipant import ActivityParticipant + + +def get_unaccepted_invitations_for_family(family_id): + participant_subquery = ActivityParticipant.objects.filter( + person__family_id=family_id, + person_id=OuterRef("person_id"), + activity_id=OuterRef("activity_id"), + ) + + # Get invitations where the participant record does not exist + unaccepted_invitations = ( + ActivityInvite.objects.filter( + person__family_id=family_id, + expire_dtm__gte=timezone.now(), + rejected_at=None, + ) + .annotate(is_participant=Exists(participant_subquery)) + .filter(is_participant=False) + ) + + return unaccepted_invitations