def add_or_invite_to_team(inviter, team, user_obj=None, email=None, requires_invite=True): # If the user is a member of the organization, then we simply add the # user directly to the team. Otherwise, an invite is created for the user/email. # We return None if the user was directly added and the invite object if the user was invited. if user_obj and requires_invite: orgname = team.organization.username # If the user is part of the organization (or a robot), then no invite is required. if user_obj.robot: requires_invite = False if not user_obj.username.startswith(orgname + "+"): raise InvalidTeamMemberException( "Cannot add the specified robot to this team, " + "as it is not a member of the organization") else: query = (TeamMember.select().where( TeamMember.user == user_obj).join(Team).join(User).where( User.username == orgname, User.organization == True)) requires_invite = not any(query) # If we have a valid user and no invite is required, simply add the user to the team. if user_obj and not requires_invite: add_user_to_team(user_obj, team) return None email_address = email if not user_obj else None return TeamMemberInvite.create(user=user_obj, email=email_address, team=team, inviter=inviter)
def delete_team_user_invite(team, user_obj): try: found = TeamMemberInvite.get(TeamMemberInvite.user == user_obj, TeamMemberInvite.team == team) except TeamMemberInvite.DoesNotExist: return False found.delete_instance() return True
def delete_team_email_invite(team, email): try: found = TeamMemberInvite.get(TeamMemberInvite.email == email, TeamMemberInvite.team == team) except TeamMemberInvite.DoesNotExist: return False found.delete_instance() return True
def find_organization_invites(organization, user_obj): """ Finds all organization team invites for the given user under the given organization. """ invite_check = TeamMemberInvite.user == user_obj if user_obj.verified: invite_check = invite_check | (TeamMemberInvite.email == user_obj.email) query = (TeamMemberInvite.select().join(Team).where( invite_check, Team.organization == organization)) return query
def lookup_team_invite(code, user_obj=None): # Lookup the invite code. try: found = TeamMemberInvite.get(TeamMemberInvite.invite_token == code) except TeamMemberInvite.DoesNotExist: raise DataModelException("Invalid confirmation code.") if user_obj and found.user != user_obj: raise DataModelException("Invalid confirmation code.") return found
def lookup_team_invites(user_obj): return TeamMemberInvite.select().where(TeamMemberInvite.user == user_obj)
def lookup_team_invites_by_email(email): return TeamMemberInvite.select().where(TeamMemberInvite.email == email)
def get_organization_team_member_invites(teamid): joined = TeamMemberInvite.select().join(Team).join(User) query = joined.where(Team.id == teamid) return query