implemented join/leave teams

This commit is contained in:
James Tauber 2012-08-01 22:49:27 -04:00
parent 4f888ffd57
commit a4dea58195
4 changed files with 87 additions and 13 deletions

View file

@ -2,5 +2,8 @@ from django.conf.urls.defaults import *
urlpatterns = patterns("symposion.teams.views", urlpatterns = patterns("symposion.teams.views",
url(r"^([\w\-]+)/$", "team_detail", name="team_detail"), url(r"^(?P<slug>[\w\-]+)/$", "team_detail", name="team_detail"),
url(r"^(?P<slug>[\w\-]+)/join/$", "team_join", name="team_join"),
url(r"^(?P<slug>[\w\-]+)/leave/$", "team_leave", name="team_leave"),
) )

View file

@ -1,9 +1,35 @@
from django.http import Http404 from django.http import Http404
from django.shortcuts import render, get_object_or_404 from django.shortcuts import render, redirect, get_object_or_404
from django.contrib.auth.decorators import login_required from django.contrib.auth.decorators import login_required
from symposion.teams.models import Team 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
## views
@login_required @login_required
@ -16,4 +42,39 @@ def team_detail(request, slug):
return render(request, "teams/team_detail.html", { return render(request, "teams/team_detail.html", {
"team": team, "team": team,
"state": state, "state": state,
"can_join": can_join(team, request.user),
"can_leave": can_leave(team, request.user),
}) })
@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)

View file

@ -132,18 +132,14 @@
<div class="dashboard-panel-content"> <div class="dashboard-panel-content">
{% if user.memberships.exists %} {% if user.memberships.exists %}
<h4>Your Teams</h4> <h4>Your Teams</h4>
<ul> <dl>
{% for membership in user.memberships.all %} {% for membership in user.memberships.all %}
<li> <dt>
{% if membership.state == "manager" %} <a href="{% url team_detail membership.team.slug %}">{{ membership.team.name }}</a>
<a href="{% url team_detail membership.team.slug %}">{{ membership.team.name }}</a> <span class="label">{{ membership.get_state_display }}</span>
{% else %} </dt>
{{ membership.team.name }}
{% endif %}
<span class="badge">{{ membership.get_state_display }}</span>
</li>
{% endfor %} {% endfor %}
</ul> </dl>
{% endif %} {% endif %}
{% available_teams as available_teams %} {% available_teams as available_teams %}
{% if available_teams %} {% if available_teams %}

View file

@ -5,4 +5,18 @@
{% block body %} {% block body %}
<h1>{{ team.name }}</h1> <h1>{{ team.name }}</h1>
{% if can_join %}
<form method="post" action="{% url team_join team.slug %}">
{% csrf_token %}
<input type="submit" class="btn btn-primary" value="join">
</form>
{% endif %}
{% if can_leave %}
<form method="post" action="{% url team_leave team.slug %}">
{% csrf_token %}
<input type="submit" class="btn" value="leave">
</form>
{% endif %}
{% endblock %} {% endblock %}