diff --git a/gregui/api/views/invitation.py b/gregui/api/views/invitation.py index cb043f06e1be7aaed2db87a8eb046e38ffcc3f27..a73ba02421f85a73a4e6f99fec9e0c7889d9c50f 100644 --- a/gregui/api/views/invitation.py +++ b/gregui/api/views/invitation.py @@ -71,8 +71,8 @@ class InvitationView(CreateAPIView, DestroyAPIView): person = serializer.save() for invitationlink in InvitationLink.objects.filter( - invitation__role__person_id=person.id, - invitation__role__sponsor_id=sponsor_user.sponsor_id, + invitation__role__person_id=person.id, + invitation__role__sponsor_id=sponsor_user.sponsor_id, ): send_invite_mail(invitationlink) @@ -80,9 +80,8 @@ class InvitationView(CreateAPIView, DestroyAPIView): def delete(self, request, *args, **kwargs) -> Response: person_id = int(request.query_params["person_id"]) - - # TODO Determine if person should be deleted as well person = Person.objects.get(id=person_id) + if person.is_registered or person.is_verified: # The guest has already gone through the registration step. The guest should # not be verified, but including that check just in case here. @@ -93,18 +92,10 @@ class InvitationView(CreateAPIView, DestroyAPIView): ) return Response(status.HTTP_400_BAD_REQUEST) - # Since the person is not registered, all roles attached to invitations for him should be removed - invitationlinks = InvitationLink.objects.filter( - invitation__role__person_id=person_id - ) - roles = set( - [invitationlink.invitation.role for invitationlink in invitationlinks] - ) - - for role in roles: - # Delete the role, the cascading will cause all the invitation links connected - # to it to be removed as well - role.delete() + # Delete the person. The delete will cascade and all roles, identities and invitations will be removed. + # It is OK to do this here since the person has not gone through the registration, so it is not + # expected that there is any information that cannot be deleted without problems attached to him + person.delete() return Response(status=status.HTTP_200_OK) @@ -286,7 +277,7 @@ class InvitedGuestView(GenericAPIView): @staticmethod def _get_identity_or_none( - person: Person, identity_type: Identity.IdentityType + person: Person, identity_type: Identity.IdentityType ) -> Optional[str]: try: return person.identities.get(type=identity_type).value