implemented invitations

This commit is contained in:
James Tauber 2012-08-03 00:21:22 -04:00
parent 7fe481ee98
commit 4272c8f8a8
3 changed files with 94 additions and 0 deletions

50
symposion/teams/forms.py Normal file
View file

@ -0,0 +1,50 @@
from django import forms
from django.contrib.auth.models import User
from symposion.teams.models import Membership
class TeamInvitationForm(forms.Form):
email = forms.EmailField(help_text="email address must be that of a user on the site")
def __init__(self, *args, **kwargs):
self.team = kwargs.pop("team")
super(TeamInvitationForm, self).__init__(*args, **kwargs)
def clean(self):
cleaned_data = super(TeamInvitationForm, self).clean()
email = cleaned_data.get("email")
if email is None:
raise forms.ValidationError("valid email address required")
try:
user = User.objects.get(email=email)
except User.DoesNotExist:
# eventually we can invite them but for now assume they are
# already on the site
raise forms.ValidationError("no known user with email address %s" % email)
state = self.team.get_state_for_user(user)
if state in ["member", "manager"]:
raise forms.ValidationError("user already in team")
if state in ["invited"]:
raise forms.ValidationError("user already invited to team")
self.user = user
self.state = state
return cleaned_data
def invite(self):
if self.state is None:
Membership.objects.create(team=self.team, user=self.user, state="invited")
elif self.state == "applied":
# if they applied we shortcut invitation process
membership = Membership.objects.get(team=self.team, user=self.user)
membership.state = "member"
membership.save()

View file

@ -3,6 +3,7 @@ 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.forms import TeamInvitationForm
from symposion.teams.models import Team, Membership from symposion.teams.models import Team, Membership
@ -41,6 +42,14 @@ def can_apply(team, user):
return False return False
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
## views ## views
@ -51,9 +60,22 @@ def team_detail(request, slug):
if team.access == "invitation" and state is None and not request.user.is_staff: if team.access == "invitation" and state is None and not request.user.is_staff:
raise Http404() raise Http404()
if can_invite(team, request.user):
if request.method == "POST":
form = TeamInvitationForm(request.POST, team=team)
if form.is_valid():
form.invite()
# contrib.message
return redirect("team_detail", slug=slug)
else:
form = TeamInvitationForm(team=team)
else:
form = None
return render(request, "teams/team_detail.html", { return render(request, "teams/team_detail.html", {
"team": team, "team": team,
"state": state, "state": state,
"invite_form": form,
"can_join": can_join(team, request.user), "can_join": can_join(team, request.user),
"can_leave": can_leave(team, request.user), "can_leave": can_leave(team, request.user),
"can_apply": can_apply(team, request.user), "can_apply": can_apply(team, request.user),

View file

@ -1,5 +1,7 @@
{% extends "site_base.html" %} {% extends "site_base.html" %}
{% load bootstrap_tags %}
{% block head_title %}{{ team.name }}{% endblock %} {% block head_title %}{{ team.name }}{% endblock %}
{% block body %} {% block body %}
@ -69,5 +71,25 @@
{% endfor %} {% endfor %}
</table> </table>
{% endif %} {% endif %}
{% if team.invitees %}
<h2>Invitees</h2>
<table class="table table-striped">
{% for membership in team.invitees %}
<tr>
<td>{{ membership.user.email }}</td>
</tr>
{% endfor %}
</table>
{% endif %}
{% if invite_form %}
<form method="POST" action="" class="form-horizontal">
{% csrf_token %}
<legend>Invite User to Team</legend>
{{ invite_form|as_bootstrap }}
<div class="form-actions">
<input class="btn btn-primary" type="submit" value="Invite" />
</div>
</form>
{% endif %}
{% endif %} {% endif %}
{% endblock %} {% endblock %}