def add_member(request, team_id): if request.method == 'POST': team = get_object_or_404(Team, pk=team_id) if request.user != team.leader: messages.error(request, _(u'You are not the team leader, you cannot add team members.')) else: user_id = request.POST.get('selectMember') user = get_object_or_404(User, pk=user_id) if len(Member.objects.filter(user=user, team=team)) > 0: messages.error(request, unicode(user) + _(u' is already on your team.')) else: member = Member() member.team = team member.user = user member.save() messages.success(request, unicode(user) + _(u' was added to your team')) return redirect(team)
def join_team(request, team_id): team = get_object_or_404(Team, pk=team_id) invitation = Invitation.objects.filter(invitee=request.user, team=team) if not invitation: messages.error(request, _(u'You are not invited to join this team.')) else: user = request.user if len(Member.objects.filter(user=user, team=team)) > 0: messages.error(request, unicode(user) + _(u', you are already on this team.')) else: member = Member() member.team = team member.user = user member.save() message = get_object_or_404(Message, subject=invitation[0].token) message.delete() invitation.delete() messages.success(request, _(u'You successfully joined the team.')) return redirect(team)
def accept_member_invite(request, team_id): team = get_object_or_404(Team, pk=team_id) user = request.user if request.user == team.leader: messages.error( request, _(u'You are already the team leader of team {team}.').format( team=team)) return redirect(team) if Member.objects.filter(team=team, user=user).exists(): messages.error( request, _(u'You are already on team {team}.').format(user=user, team=team)) return redirect(team) invitation_qs = Invitation.objects.filter(team=team, invitee=user) if not invitation_qs.exists(): messages.error( request, _(u'You are not invited to team {team}.').format(user=user, team=team)) return redirect(team) invitation = invitation_qs[0] member = Member() member.team = team member.user = user member.save() # Delete both invitation and message Message.objects.filter(subject=invitation.token).delete() invitation.delete() messages.success( request, _(u'You have accepted the membership invitation from team {team}.'). format(team=team)) return redirect(team)