def create_group_invitation(self, user, group, email, permissions, message, base_url): """ Creates a new group invitation for the given email address and sends out an email containing the invitation. :param user: The user on whose behalf the invitation is created. :type user: User :param group: The group for which the user is invited. :type group: Group :param email: The email address of the person that is invited to the group. Can be an existing or not existing user. :type email: str :param permissions: The group permissions that the user will get once he has accepted the invitation. :type permissions: str :param message: A custom message that will be included in the invitation email. :type message: str :param base_url: The base url of the frontend, where the user can accept his invitation. The signed invitation id is appended to the URL (base_url + '/TOKEN'). Only the PUBLIC_WEB_FRONTEND_HOSTNAME is allowed as domain name. :type base_url: str :raises ValueError: If the provided permissions are not allowed. :raises UserInvalidGroupPermissionsError: If the user does not belong to the group or doesn't have right permissions in the group. :return: The created group invitation. :rtype: GroupInvitation """ group.has_user(user, "ADMIN", raise_error=True) if permissions not in dict(GROUP_USER_PERMISSION_CHOICES): raise ValueError("Incorrect permissions provided.") email = normalize_email_address(email) if GroupUser.objects.filter(group=group, user__email=email).exists(): raise GroupUserAlreadyExists( f"The user {email} is already part of the " f"group.") invitation, created = GroupInvitation.objects.update_or_create( group=group, email=email, defaults={ "message": message, "permissions": permissions, "invited_by": user, }, ) self.send_group_invitation_email(invitation, base_url) return invitation
def to_internal_value(self, data): data = super().to_internal_value(data) return normalize_email_address(data)
def test_normalize_email_address(): assert normalize_email_address(" [email protected] ") == "*****@*****.**" assert normalize_email_address("*****@*****.**") == "*****@*****.**"
def test_normalize_email_address(): assert normalize_email_address(' [email protected] ') == '*****@*****.**' assert normalize_email_address('*****@*****.**') == '*****@*****.**'