symposion_app/vendor/symposion/teams/forms.py

60 lines
2 KiB
Python
Raw Normal View History

2012-08-03 04:21:22 +00:00
from django import forms
from django.utils.html import escape
from django.utils.safestring import mark_safe
from django.utils.translation import ugettext_lazy as _
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
required_css_class = 'label-required'
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:
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(
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"]:
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"]:
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")