class CustomRegistrationForm(RegistrationForm): username = forms.RegexField(regex=r'^\w+$', max_length=30, label=_('Username'), error_messages={'invalid': _('A username must contain letters, ' 'numbers, or underscores')}) display_name = CharField(max_length=50, required=False, label=_('Real name (optional)')) timezone = ChoiceField(label=_('Timezone'), choices=TIMEZONE, widget=Select2Widget(attrs={'style': 'width:100%'})) language = ModelChoiceField(queryset=Language.objects.all(), label=_('Preferred language'), empty_label=None, widget=Select2Widget(attrs={'style': 'width:100%'})) organizations = ModelMultipleChoiceField(queryset=Organization.objects.filter(is_open=True), label=_('Organizations'), required=False, widget=Select2MultipleWidget(attrs={'style': 'width:100%'})) if newsletter_id is not None: newsletter = forms.BooleanField(label=_('Subscribe to newsletter?'), initial=True, required=False) if settings.USE_RECAPTCHA: captcha = ReCaptchaField(widget=ReCaptchaWidget()) def clean_email(self): if User.objects.filter(email=self.cleaned_data['email']).exists(): raise forms.ValidationError(ugettext(u'The email address "%s" is already taken. Only one registration ' u'is allowed per address.') % self.cleaned_data['email']) if '@' in self.cleaned_data['email']: domain = self.cleaned_data['email'].split('@')[-1].lower() if (domain in getattr(settings, 'BAD_MAIL_PROVIDERS', ()) or any(regex.match(domain) for regex in bad_mail_regex)): raise forms.ValidationError(ugettext(u'Your email provider is not allowed due to history of abuse. ' u'Please use a reputable email provider.')) return self.cleaned_data['email']
class CustomRegistrationForm(RegistrationForm): username = forms.RegexField(regex=r'^\w+$', max_length=30, label=_('Username'), error_messages={ 'invalid': _('A username must contain letters, ' 'numbers, or underscores') }) timezone = ChoiceField(label=_('Timezone'), choices=TIMEZONE, widget=Select2Widget(attrs={'style': 'width:100%'})) language = ModelChoiceField( queryset=Language.objects.all(), label=_('Preferred language'), empty_label=None, widget=Select2Widget(attrs={'style': 'width:100%'})) organizations = SortedMultipleChoiceField( queryset=Organization.objects.filter(is_open=True), label=_('Organizations'), required=False, widget=Select2MultipleWidget(attrs={'style': 'width:100%'})) if newsletter_id is not None: newsletter = forms.BooleanField(label=_('Subscribe to newsletter?'), initial=True, required=False) if ReCaptchaField is not None: captcha = ReCaptchaField(widget=ReCaptchaWidget()) def clean_organizations(self): organizations = self.cleaned_data.get('organizations') or [] max_orgs = settings.DMOJ_USER_MAX_ORGANIZATION_COUNT if sum(org.is_open for org in organizations) > max_orgs: raise forms.ValidationError( _('You may not be part of more than {count} public organizations.' ).format(count=max_orgs)) return self.cleaned_data['organizations'] def clean_email(self): if User.objects.filter(email=self.cleaned_data['email']).exists(): raise forms.ValidationError( gettext( 'The email address "%s" is already taken. Only one registration ' 'is allowed per address.') % self.cleaned_data['email']) if '@' in self.cleaned_data['email']: domain = self.cleaned_data['email'].split('@')[-1].lower() if (domain in settings.BAD_MAIL_PROVIDERS or any(regex.match(domain) for regex in bad_mail_regex)): raise forms.ValidationError( gettext( 'Your email provider is not allowed due to history of abuse. ' 'Please use a reputable email provider.')) return self.cleaned_data['email']
class MySignupForm(SignupForm): username = forms.RegexField(regex=r'^\w+$', max_length=30, label=_('Username'), error_messages={ 'invalid': _('A username must contain letters, ' 'numbers, or underscores') }) display_name = CharField(max_length=50, required=False, label=_('Real name (optional)')) timezone = ChoiceField(label=_('Timezone'), choices=TIMEZONE, widget=Select2Widget(attrs={'style': 'width:100%'}), initial=getattr(settings, 'DEFAULT_USER_TIME_ZONE', 'Asia/Shanghai')) language = ModelChoiceField( queryset=Language.objects.all(), label=_('Preferred language'), empty_label=None, widget=Select2Widget(attrs={'style': 'width:100%'}), initial=Language.objects.get( key=getattr(settings, 'DEFAULT_USER_LANGUAGE', 'CPP11'))) organizations = SortedMultipleChoiceField( queryset=Organization.objects.filter(is_open=True), label=_('Organizations'), required=False, widget=Select2MultipleWidget(attrs={'style': 'width:100%'})) if newsletter_id is not None: newsletter = forms.BooleanField(label=_('Subscribe to newsletter?'), initial=True, required=False) if ReCaptchaField is not None: captcha = ReCaptchaField(widget=ReCaptchaWidget()) def save(self, request): user = super(MySignupForm, self).save(request) profile, _ = Profile.objects.get_or_create( user=user, defaults={'language': Language.get_python2()}) profile.name = self.cleaned_data.get('display_name') profile.timezone = self.cleaned_data.get('timezone') profile.language = self.cleaned_data.get('language') profile.organizations.add(*self.cleaned_data.get('organizations')) profile.save() if newsletter_id is not None and self.cleaned_data.get('newsletter'): Subscription(user=user, newsletter_id=newsletter_id, subscribed=True).save() return user