mirror of
https://github.com/zulip/zulip.git
synced 2025-11-05 22:43:42 +00:00
The previous situation was bad for two reasons: (1) It had a lot of copies of the code, some of them missing pieces: UserProfile.objects.get(user__email__iexact=foo) This was in particular going to be inconvenient since we are dropping the __user part of that. (2) It didn't take advantage of our memcached caching. (imported from commit 2325795f288a7cf306cdae191f5d3080aac0651a)
58 lines
2.0 KiB
Python
58 lines
2.0 KiB
Python
from django import forms
|
|
from django.core import validators
|
|
from django.core.exceptions import ValidationError
|
|
from django.utils.safestring import mark_safe
|
|
from django.contrib.auth.forms import SetPasswordForm
|
|
|
|
from humbug import settings
|
|
from zephyr.models import Realm, get_user_profile_by_email, UserProfile
|
|
from zephyr.lib.actions import do_change_password
|
|
|
|
def is_unique(value):
|
|
try:
|
|
get_user_profile_by_email(value)
|
|
raise ValidationError(u'%s is already registered' % value)
|
|
except UserProfile.DoesNotExist:
|
|
pass
|
|
|
|
def is_inactive(value):
|
|
try:
|
|
if get_user_profile_by_email(value).user.is_active:
|
|
raise ValidationError(u'%s is already active' % value)
|
|
except UserProfile.DoesNotExist:
|
|
pass
|
|
|
|
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))
|
|
|
|
class RegistrationForm(forms.Form):
|
|
full_name = forms.CharField(max_length=100)
|
|
password = forms.CharField(widget=forms.PasswordInput, max_length=100)
|
|
terms = forms.BooleanField(required=True)
|
|
|
|
class ToSForm(forms.Form):
|
|
full_name = forms.CharField(max_length=100)
|
|
terms = forms.BooleanField(required=True)
|
|
|
|
class HomepageForm(forms.Form):
|
|
if settings.ALLOW_REGISTER:
|
|
email = forms.EmailField()
|
|
else:
|
|
validators = [has_valid_realm, isnt_mit, is_inactive]
|
|
email = forms.EmailField(validators=validators)
|
|
|
|
class LoggingSetPasswordForm(SetPasswordForm):
|
|
def save(self, commit=True):
|
|
do_change_password(self.user.userprofile, self.cleaned_data['new_password1'],
|
|
log=True, commit=commit)
|
|
return self.user
|