symposion_app/symposion/teams/views.py

195 lines
6.3 KiB
Python
Raw Normal View History

from django.http import Http404, HttpResponseNotAllowed
2012-08-02 02:49:27 +00:00
from django.shortcuts import render, redirect, get_object_or_404
2012-07-28 22:30:00 +00:00
from django.contrib.auth.decorators import login_required
2012-08-03 05:11:33 +00:00
from django.contrib import messages
2012-07-28 22:30:00 +00:00
2012-08-14 20:59:06 +00:00
from symposion.utils.mail import send_email
2012-08-03 04:21:22 +00:00
from symposion.teams.forms import TeamInvitationForm
2012-08-02 02:49:27 +00:00
from symposion.teams.models import Team, Membership
2014-07-30 18:19:26 +00:00
# perm checks
2012-08-02 02:49:27 +00:00
#
# @@@ these can be moved
def can_join(team, user):
state = team.get_state_for_user(user)
if team.access == "open" and state is None:
return True
2012-08-03 04:33:28 +00:00
elif state == "invited":
2012-08-02 02:49:27 +00:00
return True
elif user.is_staff and state is None:
return True
2012-08-02 02:49:27 +00:00
else:
return False
def can_leave(team, user):
state = team.get_state_for_user(user)
if state == "member": # managers can't leave at the moment
return True
else:
return False
2012-08-02 02:59:41 +00:00
def can_apply(team, user):
state = team.get_state_for_user(user)
if team.access == "application" and state is None:
return True
else:
return False
2012-08-03 04:21:22 +00:00
def can_invite(team, user):
state = team.get_state_for_user(user)
if team.access == "invitation":
if state == "manager" or user.is_staff:
return True
return False
2014-07-30 18:19:26 +00:00
# views
2012-07-28 22:30:00 +00:00
@login_required
def team_detail(request, slug):
team = get_object_or_404(Team, slug=slug)
state = team.get_state_for_user(request.user)
if team.access == "invitation" and state is None and not request.user.is_staff:
2012-07-28 22:30:00 +00:00
raise Http404()
2014-07-30 18:19:26 +00:00
2012-08-03 04:21:22 +00:00
if can_invite(team, request.user):
if request.method == "POST":
form = TeamInvitationForm(request.POST, team=team)
if form.is_valid():
form.invite()
2012-08-14 20:59:06 +00:00
send_email([form.user.email], "teams_user_invited", context={"team": team})
2012-08-03 05:11:33 +00:00
messages.success(request, "Invitation created.")
2012-08-03 04:21:22 +00:00
return redirect("team_detail", slug=slug)
else:
form = TeamInvitationForm(team=team)
else:
form = None
2014-07-30 18:19:26 +00:00
2012-07-28 22:30:00 +00:00
return render(request, "teams/team_detail.html", {
2012-08-02 01:38:50 +00:00
"team": team,
"state": state,
2012-08-03 04:21:22 +00:00
"invite_form": form,
2012-08-02 02:49:27 +00:00
"can_join": can_join(team, request.user),
"can_leave": can_leave(team, request.user),
2012-08-02 02:59:41 +00:00
"can_apply": can_apply(team, request.user),
2012-07-28 22:30:00 +00:00
})
2012-08-02 02:49:27 +00:00
@login_required
def team_join(request, slug):
team = get_object_or_404(Team, slug=slug)
state = team.get_state_for_user(request.user)
if team.access == "invitation" and state is None and not request.user.is_staff:
2012-08-02 02:49:27 +00:00
raise Http404()
2014-07-30 18:19:26 +00:00
2012-08-02 02:49:27 +00:00
if can_join(team, request.user) and request.method == "POST":
membership, created = Membership.objects.get_or_create(team=team, user=request.user)
membership.state = "member"
membership.save()
2012-08-03 05:11:33 +00:00
messages.success(request, "Joined team.")
2012-08-02 02:49:27 +00:00
return redirect("team_detail", slug=slug)
else:
return redirect("team_detail", slug=slug)
@login_required
def team_leave(request, slug):
team = get_object_or_404(Team, slug=slug)
state = team.get_state_for_user(request.user)
if team.access == "invitation" and state is None and not request.user.is_staff:
2012-08-02 02:49:27 +00:00
raise Http404()
2014-07-30 18:19:26 +00:00
2012-08-02 02:49:27 +00:00
if can_leave(team, request.user) and request.method == "POST":
membership = Membership.objects.get(team=team, user=request.user)
membership.delete()
2012-08-03 05:11:33 +00:00
messages.success(request, "Left team.")
2012-08-02 02:49:27 +00:00
return redirect("dashboard")
else:
return redirect("team_detail", slug=slug)
2012-08-02 02:59:41 +00:00
@login_required
def team_apply(request, slug):
team = get_object_or_404(Team, slug=slug)
state = team.get_state_for_user(request.user)
if team.access == "invitation" and state is None and not request.user.is_staff:
2012-08-02 02:59:41 +00:00
raise Http404()
2014-07-30 18:19:26 +00:00
2012-08-02 02:59:41 +00:00
if can_apply(team, request.user) and request.method == "POST":
membership, created = Membership.objects.get_or_create(team=team, user=request.user)
membership.state = "applied"
membership.save()
2012-08-14 20:59:06 +00:00
managers = [m.user.email for m in team.managers()]
send_email(managers, "teams_user_applied", context={
"team": team,
"user": request.user
})
2012-08-03 05:11:33 +00:00
messages.success(request, "Applied to join team.")
2012-08-02 02:59:41 +00:00
return redirect("team_detail", slug=slug)
else:
return redirect("team_detail", slug=slug)
@login_required
def team_promote(request, pk):
if request.method != "POST":
return HttpResponseNotAllowed(["POST"])
membership = get_object_or_404(Membership, pk=pk)
state = membership.team.get_state_for_user(request.user)
if request.user.is_staff or state == "manager":
if membership.state == "member":
membership.state = "manager"
membership.save()
messages.success(request, "Promoted to manager.")
return redirect("team_detail", slug=membership.team.slug)
@login_required
def team_demote(request, pk):
if request.method != "POST":
return HttpResponseNotAllowed(["POST"])
membership = get_object_or_404(Membership, pk=pk)
state = membership.team.get_state_for_user(request.user)
if request.user.is_staff or state == "manager":
if membership.state == "manager":
membership.state = "member"
membership.save()
messages.success(request, "Demoted from manager.")
return redirect("team_detail", slug=membership.team.slug)
2012-08-02 23:09:44 +00:00
@login_required
def team_accept(request, pk):
if request.method != "POST":
return HttpResponseNotAllowed(["POST"])
membership = get_object_or_404(Membership, pk=pk)
state = membership.team.get_state_for_user(request.user)
if request.user.is_staff or state == "manager":
if membership.state == "applied":
membership.state = "member"
membership.save()
messages.success(request, "Accepted application.")
2012-08-02 23:09:44 +00:00
return redirect("team_detail", slug=membership.team.slug)
@login_required
def team_reject(request, pk):
if request.method != "POST":
return HttpResponseNotAllowed(["POST"])
membership = get_object_or_404(Membership, pk=pk)
state = membership.team.get_state_for_user(request.user)
if request.user.is_staff or state == "manager":
if membership.state == "applied":
membership.state = "rejected"
membership.save()
messages.success(request, "Rejected application.")
2012-08-02 23:09:44 +00:00
return redirect("team_detail", slug=membership.team.slug)