symposion_app/symposion/teams/views.py

107 lines
3 KiB
Python
Raw Normal View History

2012-07-28 22:30:00 +00:00
from django.http import Http404
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-02 02:49:27 +00:00
from symposion.teams.models import Team, Membership
## perm checks
#
# @@@ 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
elif team.access == "invitation" and state is "invited":
return True
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-02 02:49:27 +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:
2012-07-28 22:30:00 +00:00
raise Http404()
2012-08-02 01:38:50 +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-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:
raise Http404()
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()
# contrib.message
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:
raise Http404()
if can_leave(team, request.user) and request.method == "POST":
membership = Membership.objects.get(team=team, user=request.user)
membership.delete()
# contrib.message
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:
raise Http404()
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()
# contrib.message
return redirect("team_detail", slug=slug)
else:
return redirect("team_detail", slug=slug)