2012-08-28 18:44:51 +02:00
|
|
|
from django import forms
|
2012-09-26 20:08:39 +02:00
|
|
|
from django.core import validators
|
|
|
|
from django.core.exceptions import ValidationError
|
|
|
|
from django.contrib.auth.models import User
|
2012-11-21 21:14:55 +01:00
|
|
|
from django.utils.safestring import mark_safe
|
2012-12-13 21:08:07 +01:00
|
|
|
from django.contrib.auth.forms import SetPasswordForm
|
2012-11-21 21:14:55 +01:00
|
|
|
|
|
|
|
from humbug import settings
|
2012-12-13 21:08:07 +01:00
|
|
|
from zephyr.models import Realm, do_change_password
|
2012-09-26 20:08:39 +02:00
|
|
|
|
|
|
|
def is_unique(value):
|
|
|
|
try:
|
2012-12-04 19:56:34 +01:00
|
|
|
User.objects.get(email__iexact=value)
|
2012-09-26 20:08:39 +02:00
|
|
|
raise ValidationError(u'%s is already registered' % value)
|
|
|
|
except User.DoesNotExist:
|
|
|
|
pass
|
|
|
|
|
2012-10-29 19:08:18 +01:00
|
|
|
def is_active(value):
|
|
|
|
try:
|
|
|
|
if User.objects.get(email=value).is_active:
|
|
|
|
raise ValidationError(u'%s is already active' % value)
|
|
|
|
except User.DoesNotExist:
|
|
|
|
pass
|
|
|
|
|
2012-11-21 21:14:55 +01:00
|
|
|
SIGNUP_STRING = '<a href="http://get.humbughq.com/">Sign up</a> to find out when Humbug is ready for you.'
|
|
|
|
|
|
|
|
def has_valid_realm(value):
|
|
|
|
try:
|
|
|
|
Realm.objects.get(domain=value.split("@")[-1])
|
|
|
|
except Realm.DoesNotExist:
|
|
|
|
raise ValidationError(mark_safe(u'Registration is not currently available for your domain. ' + SIGNUP_STRING))
|
|
|
|
|
|
|
|
def isnt_mit(value):
|
|
|
|
if "@mit.edu" in value:
|
|
|
|
raise ValidationError(mark_safe(u'Humbug for MIT is by invitation only. ' + SIGNUP_STRING))
|
|
|
|
|
|
|
|
|
2012-09-26 20:08:39 +02:00
|
|
|
class UniqueEmailField(forms.EmailField):
|
|
|
|
default_validators = [validators.validate_email, is_unique]
|
2012-08-28 18:44:51 +02:00
|
|
|
|
|
|
|
class RegistrationForm(forms.Form):
|
2012-09-11 19:20:01 +02:00
|
|
|
full_name = forms.CharField(max_length=100)
|
2012-09-05 16:20:39 +02:00
|
|
|
password = forms.CharField(widget=forms.PasswordInput, max_length=100)
|
2012-10-25 21:04:46 +02:00
|
|
|
terms = forms.BooleanField(required=True)
|
2012-09-28 22:47:05 +02:00
|
|
|
|
2013-01-08 23:26:40 +01:00
|
|
|
class ToSForm(forms.Form):
|
|
|
|
full_name = forms.CharField(max_length=100)
|
|
|
|
terms = forms.BooleanField(required=True)
|
|
|
|
|
2012-09-28 22:47:05 +02:00
|
|
|
class HomepageForm(forms.Form):
|
2012-11-21 21:14:55 +01:00
|
|
|
if settings.ALLOW_REGISTER:
|
|
|
|
email = UniqueEmailField()
|
|
|
|
else:
|
|
|
|
validators = UniqueEmailField.default_validators + [has_valid_realm, isnt_mit]
|
|
|
|
email = UniqueEmailField(validators=validators)
|
2012-12-13 21:08:07 +01:00
|
|
|
|
|
|
|
class LoggingSetPasswordForm(SetPasswordForm):
|
|
|
|
def save(self, commit=True):
|
|
|
|
do_change_password(self.user, self.cleaned_data['new_password1'],
|
|
|
|
log=True, commit=commit)
|
|
|
|
return self.user
|