2015-07-18 07:09:17 +00:00
|
|
|
from __future__ import unicode_literals
|
2012-08-03 04:21:22 +00:00
|
|
|
from django import forms
|
|
|
|
|
2012-08-22 21:56:22 +00:00
|
|
|
from django.utils.html import escape
|
|
|
|
from django.utils.safestring import mark_safe
|
2015-06-26 03:46:09 +00:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2012-08-22 21:56:22 +00:00
|
|
|
|
2012-08-03 04:21:22 +00:00
|
|
|
from django.contrib.auth.models import User
|
|
|
|
|
|
|
|
from symposion.teams.models import Membership
|
|
|
|
|
|
|
|
|
|
|
|
class TeamInvitationForm(forms.Form):
|
2014-07-30 18:19:26 +00:00
|
|
|
|
2015-06-26 03:46:09 +00:00
|
|
|
email = forms.EmailField(label=_("Email"),
|
|
|
|
help_text=_("email address must be that of an account on this "
|
|
|
|
"conference site"))
|
2014-07-30 18:19:26 +00:00
|
|
|
|
2012-08-03 04:21:22 +00:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
self.team = kwargs.pop("team")
|
|
|
|
super(TeamInvitationForm, self).__init__(*args, **kwargs)
|
2014-07-30 18:19:26 +00:00
|
|
|
|
2012-08-03 04:21:22 +00:00
|
|
|
def clean(self):
|
|
|
|
cleaned_data = super(TeamInvitationForm, self).clean()
|
|
|
|
email = cleaned_data.get("email")
|
2014-07-30 18:19:26 +00:00
|
|
|
|
2012-08-03 04:21:22 +00:00
|
|
|
if email is None:
|
2015-06-26 03:46:09 +00:00
|
|
|
raise forms.ValidationError(_("valid email address required"))
|
2014-07-30 18:19:26 +00:00
|
|
|
|
2012-08-03 04:21:22 +00:00
|
|
|
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
|
2014-07-30 18:19:26 +00:00
|
|
|
raise forms.ValidationError(
|
2015-06-26 03:46:09 +00:00
|
|
|
mark_safe(_("no account with email address <b>%s</b> found on this conference "
|
|
|
|
"site") % escape(email)))
|
2014-07-30 18:19:26 +00:00
|
|
|
|
2012-08-03 04:21:22 +00:00
|
|
|
state = self.team.get_state_for_user(user)
|
2014-07-30 18:19:26 +00:00
|
|
|
|
2012-08-03 04:21:22 +00:00
|
|
|
if state in ["member", "manager"]:
|
2015-06-26 03:46:09 +00:00
|
|
|
raise forms.ValidationError(_("user already in team"))
|
2014-07-30 18:19:26 +00:00
|
|
|
|
2012-08-03 04:21:22 +00:00
|
|
|
if state in ["invited"]:
|
2015-06-26 03:46:09 +00:00
|
|
|
raise forms.ValidationError(_("user already invited to team"))
|
2014-07-30 18:19:26 +00:00
|
|
|
|
2012-08-03 04:21:22 +00:00
|
|
|
self.user = user
|
|
|
|
self.state = state
|
2014-07-30 18:19:26 +00:00
|
|
|
|
2012-08-03 04:21:22 +00:00
|
|
|
return cleaned_data
|
2014-07-30 18:19:26 +00:00
|
|
|
|
2012-08-03 04:21:22 +00:00
|
|
|
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
|
2012-09-15 23:47:59 +00:00
|
|
|
membership = Membership.objects.filter(team=self.team, user=self.user)
|
|
|
|
membership.update(state="member")
|