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=_('Location'), 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) 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 ContentTagForm(BaseForm): tag = forms.CharField(required=True) movies = SortedMultipleChoiceField( queryset=models.Movie.objects.all(), required=False, ) shows = SortedMultipleChoiceField( queryset=models.Show.objects.all(), required=False, ) episodes = SortedMultipleChoiceField( queryset=models.Episode.objects.all(), required=False, ) collections = SortedMultipleChoiceField( queryset=models.Collection.objects.all(), required=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
def __init__(self, *args, **kwargs): super(PersonAdminForm, self).__init__(*args, **kwargs) if not ALDRYN_PEOPLE_SUMMARY_RICHTEXT: self.fields['description'].widget = widgets.Textarea() if IS_THERE_COMPANIES: self.fields['companies'] = SortedMultipleChoiceField( queryset=Company.objects.all(), required=False) # self.instance.companies self.fields['companies'].widget = SortedFilteredSelectMultiple() self.fields['companies'].queryset = Company.objects.all() if self.instance.pk and self.instance.companies.count(): self.fields[ 'companies'].initial = self.instance.sorted_companies
class QuestionListPluginForm(forms.ModelForm): questions = SortedMultipleChoiceField(queryset=Question.objects.none()) class Meta: model = QuestionListPlugin fields = [ 'questions', ] def __init__(self, *args, **kwargs): super(QuestionListPluginForm, self).__init__(*args, **kwargs) questions_field = self.fields['questions'] questions_field.queryset = Question.objects.language()
class RelatedPeoplePluginForm(forms.ModelForm): layout = forms.ChoiceField(choices=RELATED_PEOPLE_LAYOUT) related_people = SortedMultipleChoiceField( label='key people', queryset=models.Person.objects.all(), required=False, widget=SortedFilteredSelectMultiple( 'person', False, attrs={'verbose_name_plural': 'people'})) related_groups = forms.ModelMultipleChoiceField( queryset=models.Group.objects.all().exclude( namespace=DEFAULT_APP_NAMESPACE), required=False, widget=FilteredSelectMultiple('groups', False)) related_locations = forms.ModelMultipleChoiceField( queryset=Location.objects.all(), required=False, widget=FilteredSelectMultiple('locations', False)) related_categories = forms.ModelMultipleChoiceField( queryset=Category.objects.all(), required=False, widget=FilteredSelectMultiple('categories', False)) related_services = forms.ModelMultipleChoiceField( queryset=Service.objects.all(), required=False, widget=FilteredSelectMultiple('services', False)) related_companies = forms.CharField(required=False, widget=forms.HiddenInput) def __init__(self, *args, **kwargs): super(RelatedPeoplePluginForm, self).__init__(*args, **kwargs) if 'related_groups' in self.fields and ALDRYN_PEOPLE_HIDE_GROUPS != 0: del self.fields['related_groups'] if 'related_locations' in self.fields and ALDRYN_PEOPLE_HIDE_LOCATION != 0: del self.fields['related_locations'] if IS_THERE_COMPANIES: self.fields['related_companies'] = forms.ModelMultipleChoiceField( queryset=Company.objects.all(), required=False) self.fields[ 'related_companies'].widget = SortedFilteredSelectMultiple() self.fields['related_companies'].queryset = Company.objects.all() if self.instance.pk and self.instance.related_companies.count(): self.fields[ 'related_companies'].initial = self.instance.related_companies.all( )
class SortedForm(forms.Form): values = SortedMultipleChoiceField(queryset=Book.objects.all(), required=False)
class SortedNameForm(forms.Form): values = SortedMultipleChoiceField( queryset=Book.objects.all(), to_field_name='name')
class UserTagForm(BaseForm): tag = forms.CharField(required=True) users = SortedMultipleChoiceField( queryset=models.User.objects.all(), required=False, )