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 Meta: model = Profile fields = [ 'name', 'about', 'organizations', 'timezone', 'language', 'ace_theme', 'user_script' ] widgets = { 'name': TextInput(attrs={'style': 'width:100%;box-sizing:border-box'}), 'user_script': AceWidget(theme='github'), 'timezone': Select2Widget(attrs={'style': 'width:200px'}), 'language': Select2Widget(attrs={'style': 'width:200px'}), 'ace_theme': Select2Widget(attrs={'style': 'width:200px'}), } has_math_config = bool(getattr(settings, 'MATHOID_URL', False)) if has_math_config: fields.append('math_engine') widgets['math_engine'] = Select2Widget( attrs={'style': 'width:200px'}) if HeavyPreviewPageDownWidget is not None: widgets['about'] = HeavyPreviewPageDownWidget( preview=reverse_lazy('profile_preview'), attrs={'style': 'max-width:700px;min-width:700px;width:700px'})
class Meta: model = Profile fields = ['language', 'ace_theme'] widgets = { 'language': Select2Widget(attrs={'style': 'width:200px'}), 'ace_theme': Select2Widget(attrs={'style': 'width:200px'}), } has_math_config = bool(getattr(settings, 'MATHOID_URL', False))
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
class Meta: model = Profile fields = ['about', 'timezone', 'language'] widgets = { 'timezone': Select2Widget(attrs={'style': 'width:200px'}), 'language': Select2Widget(attrs={'style': 'width:200px'}), } if HeavyPreviewPageDownWidget is not None: widgets['about'] = HeavyPreviewPageDownWidget( preview=reverse_lazy('profile_preview'), attrs={'style': 'max-width:700px;min-width:700px;width:700px'}, )
class Meta: model = Profile fields = ['timezone', 'language', 'ace_theme'] widgets = { 'timezone': Select2Widget(attrs={'style': 'width:200px'}), 'language': Select2Widget(attrs={'style': 'width:200px'}), 'ace_theme': Select2Widget(attrs={'style': 'width:200px'}), } has_math_config = bool(settings.MATHOID_URL) if has_math_config: fields.append('math_engine') widgets['math_engine'] = Select2Widget( attrs={'style': 'width:200px'})
class Meta: model = Profile fields = [ 'name', 'about', 'organizations', 'timezone', 'language', 'ace_theme', 'user_script', 'math_engine' ] widgets = { 'name': TextInput(attrs={'style': 'width:100%;box-sizing:border-box'}), 'user_script': AceWidget(theme='github'), 'timezone': Select2Widget(attrs={'style': 'width:200px'}), 'language': Select2Widget(attrs={'style': 'width:200px'}), 'ace_theme': Select2Widget(attrs={'style': 'width:200px'}), 'math_engine': Select2Widget(attrs={'style': 'width:200px'}), 'about': MartorWidget, }
class Meta: model = Profile fields = ['name', 'about', 'organizations', 'timezone', 'language', 'ace_theme', 'user_script', 'math_engine'] widgets = { 'name': TextInput(attrs={'style': 'width:100%;box-sizing:border-box'}), 'user_script': AceWidget(theme='github'), 'timezone': Select2Widget(attrs={'style': 'width:200px'}), 'language': Select2Widget(attrs={'style': 'width:200px'}), 'ace_theme': Select2Widget(attrs={'style': 'width:200px'}), 'math_engine': Select2Widget(attrs={'style': 'width:200px'}) } if HeavyPreviewPageDownWidget is not None: widgets['about'] = HeavyPreviewPageDownWidget( preview=reverse_lazy('profile_preview'), attrs={'style': 'max-width:700px;min-width:700px;width:700px'} )
def __init__(self, *args, judge_choices=(), **kwargs): super(ProblemSubmitForm, self).__init__(*args, **kwargs) self.fields['language'].empty_label = None self.fields['language'].label_from_instance = attrgetter('display_name') self.fields['language'].queryset = Language.objects.filter(judges__online=True).distinct() if judge_choices: self.fields['judge'].widget = Select2Widget( attrs={'style': 'width: 150px', 'data-placeholder': _('Any judge')}, ) self.fields['judge'].choices = judge_choices
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)')) language = ModelChoiceField(queryset=Language.objects.all(), label=_('Preferred language'), empty_label=None, widget=Select2Widget(attrs={'style': 'width:100%'})) 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 Meta: widgets = { 'contest': Select2Widget(), 'user': HeavySelect2Widget(data_view='profile_select2'), }