class Form(forms.Form): foo = forms.CharField( widget=forms.TextInput(datalist=['Foo', 'Bar', 'Baz'], ))
class ComboForm(forms.Form): combo = forms.ComboField( fields=[forms.EmailField(), forms.CharField(max_length=10)])
class SelectForm(forms.Form): foo = forms.CharField(widget=forms.Select(choices=choices))
class TextForm(forms.Form): text = forms.CharField(max_length=2)
class PwForm(forms.Form): text = forms.CharField() pw = forms.CharField(widget=forms.PasswordInput(render_value=True))
class ColorForm(forms.Form): Color = forms.CharField(widget=forms.ColorInput)
class TextForm(forms.Form): text = forms.CharField(required=False)
class NewNodeForm(Form, CleanAttrsMixin): parent = False perms = False role = forms.ChoiceField( label=_("Node Type"), help_text= _("Each Node has specific role in forums tree. This role cannot be changed after node is created." ), choices=( ('category', _("Category")), ('forum', _("Forum")), ('redirect', _("Redirection")), )) name = forms.CharField( label=_("Node Name"), max_length=255, validators=[ validate_sluggable( _("Category name must contain alphanumeric characters."), _("Category name is too long.")) ]) redirect = forms.URLField( label=_("Redirect URL"), help_text= _("Redirection nodes require you to specify URL they will redirect users to upon click." ), max_length=255, required=False) description = forms.CharField(label=_("Node Description"), widget=forms.Textarea, required=False) closed = forms.BooleanField(label=_("Closed Node"), widget=YesNoSwitch, required=False) attrs = forms.CharField( label=_("Node Style"), help_text= _('You can add custom CSS classess to this node, to change way it looks on board index.' ), max_length=255, required=False) show_details = forms.BooleanField( label=_("Node Style"), help_text= _('You can add custom CSS classess to this node, to change way it looks on board index.' ), widget=YesNoSwitch, required=False, initial=True) style = forms.CharField( label=_("Node Style"), help_text= _('You can add custom CSS classess to this node, to change way it looks on board index.' ), max_length=255, required=False) layout = ( ( _("Basic Options"), ( ('parent', { 'label': _("Node Parent") }), ('perms', { 'label': _("Copy Permissions from") }), ('role', { 'label': _("Node Type"), 'help_text': _("Each Node has specific role in forums tree. This role cannot be changed after node is created." ) }), ('name', { 'label': _("Node Name") }), ('description', { 'label': _("Node Description") }), ('redirect', { 'label': _("Redirect URL"), 'help_text': _("Redirection nodes require you to specify URL they will redirect users to upon click." ) }), ('closed', { 'label': _("Closed Node") }), ), ), ( _("Display Options"), ( ('attrs', { 'label': _("Node Attributes"), 'help_text': _('Custom templates can check nodes for predefined attributes that will change way they are rendered.' ) }), ('show_details', { 'label': _("Show Subforums Details"), 'help_text': _('Allows you to prevent this node subforums from displaying statistics, last post data, etc. ect. on forums lists.' ) }), ('style', { 'label': _("Node Style"), 'help_text': _('You can add custom CSS classess to this node, to change way it looks on board index.' ) }), ), ), ) def finalize_form(self): self.add_field( 'parent', TreeNodeChoiceField( label=_("Node Parent"), widget=forms.Select, queryset=Forum.objects.get(special='root').get_descendants( include_self=True), level_indicator=u'- - ')) self.add_field( 'perms', TreeNodeChoiceField( label=_("Copy Permissions from"), widget=forms.Select, queryset=Forum.objects.get(special='root').get_descendants(), level_indicator=u'- - ', required=False, empty_label=_("Don't copy permissions"))) def clean(self): cleaned_data = super(NewNodeForm, self).clean() node_role = cleaned_data['role'] if node_role != 'category' and cleaned_data['parent'].special == 'root': raise forms.ValidationError( _("Only categories can use Root Category as their parent.")) if node_role == 'redirect' and not cleaned_data['redirect']: raise forms.ValidationError( _("You have to define redirection URL")) return cleaned_data
class ForumForm(Form, CleanAttrsMixin): parent = False perms = False pruned_archive = False name = forms.CharField( label=_("Forum Name"), max_length=255, validators=[ validate_sluggable( _("Forum name must contain alphanumeric characters."), _("Forum name is too long.")) ]) description = forms.CharField(label=_("Forum Description"), widget=forms.Textarea, required=False) closed = forms.BooleanField(label=_("Closed Forum"), widget=YesNoSwitch, required=False) style = forms.CharField( label=_("Forum Style"), help_text= _('You can add custom CSS classess to this forum to change way it looks on forums lists.' ), max_length=255, required=False) prune_start = forms.IntegerField( label=_("Delete threads with first post older than"), help_text= _('Enter number of days since thread start after which thread will be deleted or zero to don\'t delete threads.' ), min_value=0, initial=0) prune_last = forms.IntegerField( label=_("Delete threads with last post older than"), help_text= _('Enter number of days since since last reply in thread after which thread will be deleted or zero to don\'t delete threads.' ), min_value=0, initial=0) attrs = forms.CharField( label=_("Forum Attributes"), help_text= _('Custom templates can check forums for predefined attributes that will change way subforums lists are rendered.' ), max_length=255, required=False) show_details = forms.BooleanField( label=_("Show Subforums Details"), help_text= _("Allows you to prevent this forum's subforums from displaying statistics, last post data, etc. ect. on subforums list." ), widget=YesNoSwitch, required=False, initial=True) layout = ( ( _("Basic Options"), ( ('parent', { 'label': _("Forum Parent") }), ('perms', { 'label': _("Copy Permissions from") }), ('name', { 'label': _("Forum Name") }), ('description', { 'label': _("Forum Description") }), ('closed', { 'label': _("Closed Forum") }), ), ), ( _("Prune Forum"), (('prune_start', { 'label': _("Delete threads with first post older than"), 'help_text': _('Enter number of days since thread start after which thread will be deleted or zero to don\'t delete threads.' ) }), ('prune_last', { 'label': _("Delete threads with last post older than"), 'help_text': _('Enter number of days since since last reply in thread after which thread will be deleted or zero to don\'t delete threads.' ) }), ('pruned_archive', { 'label': _("Archive pruned threads?"), 'help_text': _('If you want, you can archive pruned threads in other forum instead of deleting them.' ) })), ), ( _("Display Options"), ( ('attrs', { 'label': _("Forum Attributes"), 'help_text': _('Custom templates can check forums for predefined attributes that will change way subforums lists are rendered.' ) }), ('show_details', { 'label': _("Show Subforums Details"), 'help_text': _("Allows you to prevent this forum's subforums from displaying statistics, last post data, etc. ect. on subforums list." ) }), ('style', { 'label': _("Forum Style"), 'help_text': _('You can add custom CSS classess to this forum to change way it looks on forums lists.' ) }), ), ), ) def finalize_form(self): self.add_field( 'perms', TreeNodeChoiceField( label=_("Copy Permissions from"), widget=forms.Select, queryset=Forum.objects.get(special='root').get_descendants(), level_indicator=u'- - ', required=False, empty_label=_("Don't copy permissions"))) self.add_field( 'pruned_archive', TreeNodeChoiceField( label=_("Archive pruned threads?"), help_text= _('If you want, you can archive pruned threads in other forum instead of deleting them.' ), widget=forms.Select, queryset=Forum.objects.get(special='root').get_descendants(), level_indicator=u'- - ', required=False, empty_label=_("Don't archive pruned threads"))) def clean_pruned_archive(self): data = self.cleaned_data['pruned_archive'] if data and data.pk == self.target_forum.pk: raise forms.ValidationError(_("Forum cannot be its own archive.")) return data
class TestForm(forms.Form): test = forms.CharField(widget=MediaWidget) test2 = forms.CharField(widget=TextInput)
class CategoryForm(Form, CleanAttrsMixin): parent = False perms = False name = forms.CharField( label=_("Category Name"), max_length=255, validators=[ validate_sluggable( _("Category name must contain alphanumeric characters."), _("Category name is too long.")) ]) description = forms.CharField(label=_("Category Description"), widget=forms.Textarea, required=False) closed = forms.BooleanField(label=_("Closed Category"), widget=YesNoSwitch, required=False) style = forms.CharField( label=_("Category Style"), help_text= _('You can add custom CSS classess to this category, to change way it looks on board index.' ), max_length=255, required=False) attrs = forms.CharField( label=_("Category Attributes"), help_text= _('Custom templates can check categories for predefined attributes that will change way they are rendered.' ), max_length=255, required=False) show_details = forms.BooleanField( label=_("Show Subforums Details"), help_text= _('Allows you to prevent this category subforums from displaying statistics, last post data, etc. ect. on forums lists.' ), widget=YesNoSwitch, required=False, initial=True) layout = ( ( _("Basic Options"), ( ('parent', { 'label': _("Category Parent") }), ('perms', { 'label': _("Copy Permissions from") }), ('name', { 'label': _("Category Name") }), ('description', { 'label': _("Category Description") }), ('closed', { 'label': _("Closed Category") }), ), ), ( _("Display Options"), ( ('attrs', { 'label': _("Category Attributes"), 'help_text': _('Custom templates can check categories for predefined attributes that will change way they are rendered.' ) }), ('show_details', { 'label': _("Show Subforums Details"), 'help_text': _('Allows you to prevent this category subforums from displaying statistics, last post data, etc. ect. on forums lists.' ) }), ('style', { 'label': _("Category Style"), 'help_text': _('You can add custom CSS classess to this category, to change way it looks on board index.' ) }), ), ), ) def finalize_form(self): self.add_field( 'perms', TreeNodeChoiceField( label=_("Copy Permissions from"), widget=forms.Select, queryset=Forum.objects.get(special='root').get_descendants(), level_indicator=u'- - ', required=False, empty_label=_("Don't copy permissions")))
class HardcodedForm(forms.Form): name = forms.CharField(widget=HardcodedWidget())
class PersonForm(forms.Form): firstname = forms.CharField() lastname = forms.CharField() age = forms.IntegerField() bio = forms.CharField(widget=forms.Textarea)
class SimpleForm(forms.Form): name = forms.CharField()
class SearchForm(forms.Form): query = forms.CharField(widget=forms.SearchInput)
class RedirectForm(Form, CleanAttrsMixin): parent = False perms = False name = forms.CharField( max_length=255, validators=[ validate_sluggable( _("Redirect name must contain alphanumeric characters."), _("Redirect name is too long.")) ]) description = forms.CharField(widget=forms.Textarea, required=False) redirect = forms.URLField(max_length=255) style = forms.CharField(max_length=255, required=False) layout = ( ( _("Basic Options"), ( ('parent', { 'label': _("Redirect Parent") }), ('perms', { 'label': _("Copy Permissions from") }), ('name', { 'label': _("Redirect Name") }), ('redirect', { 'label': _("Redirect URL") }), ('description', { 'label': _("Redirect Description") }), ), ), ( _("Display Options"), ( ('attrs', { 'label': _("Forum Attributes"), 'help_text': _('Custom templates can check forums for predefined attributes that will change way subforums lists are rendered.' ) }), ('style', { 'label': _("Redirect Style"), 'help_text': _('You can add custom CSS classess to this redirect to change way it looks on forums lists.' ) }), ), ), ) def finalize_form(self): self.add_field( 'perms', TreeNodeChoiceField( label=_("Copy Permissions from"), widget=forms.Select, queryset=Forum.objects.get(special='root').get_descendants(), level_indicator=u'- - ', required=False, empty_label=_("Don't copy permissions")))
class TextForm(forms.Form): text = forms.CharField(label='My text field')
def get_field(self): from misago.forms import YesNoSwitch extra = self.get_extra() # Set validators field_validators = [] if 'min' in extra: if self.normalize_to in ('string', 'array'): field_validators.append(validators.MinLengthValidator(extra['min'])) if self.normalize_to in ('integer', 'float'): field_validators.append(validators.MinValueValidator(extra['min'])) if 'max' in extra: if self.normalize_to in ('string', 'array'): field_validators.append(validators.MaxLengthValidator(extra['max'])) if self.normalize_to in ('integer', 'float'): field_validators.append(validators.MaxValueValidator(extra['max'])) # Yes-no if self.field == 'yesno': return forms.BooleanField( initial=self.value, label=_(self.name), help_text=_(self.description) if self.description else None, required=False, widget=YesNoSwitch, ) # Multi-list if self.field == 'mlist': return forms.MultipleChoiceField( initial=self.value, label=_(self.name), help_text=_(self.description) if self.description else None, widget=forms.CheckboxSelectMultiple, validators=field_validators, required=False, choices=extra['choices'] ) # Select or choice if self.field == 'select' or self.field == 'choice': # Timezone list? if extra['choices'] == '#TZ#': extra['choices'] = tzlist() return forms.ChoiceField( initial=self.value, label=_(self.name), help_text=_(self.description) if self.description else None, widget=forms.RadioSelect if self.field == 'choice' else forms.Select, validators=field_validators, required=False, choices=extra['choices'] ) # Textarea if self.field == 'textarea': return forms.CharField( initial=self.value, label=_(self.name), help_text=_(self.description) if self.description else None, validators=field_validators, required=False, widget=forms.Textarea ) kwargs = { 'initial': self.value, 'label': _(self.name), 'help_text': _(self.description) if self.description else None, 'validators': field_validators, 'required': False, } # Default input default_input = forms.CharField if self.normalize_to == 'integer': default_input = forms.IntegerField if self.normalize_to == 'float': default_input = forms.FloatField # Make text-input return default_input(**kwargs)
class PhoneForm(forms.Form): tel = forms.CharField(widget=forms.PhoneNumberInput)
class OneFieldForm(forms.Form): text = forms.CharField() def clean(self): if self.errors: raise ValidationError('Please correct the errors below.')
class TextForm(forms.Form): text = forms.CharField(widget=forms.TextInput( attrs={'placeholder': 'Heheheh'}))
class ShortForm(forms.Form): name = forms.CharField(label=_('Your first name?')) age = forms.IntegerField(required=False) metadata = forms.CharField(required=False, widget=forms.HiddenInput)
class Form(forms.Form): text = forms.CharField(widget=forms.TextInput(attrs={ 'foo': True, 'bar': False, }))
class UserForm(forms.Form): username = forms.CharField(label_suffix='!') no_suffix = forms.CharField(label_suffix='') password = forms.CharField()
class HiddenForm(forms.Form): hide = forms.CharField(widget=forms.HiddenInput())
class TextForm(forms.Form): text = forms.CharField(widget=forms.Textarea)
class PwForm(forms.Form): text = forms.CharField() pw = forms.CharField(widget=forms.PasswordInput)
class TextForm(forms.Form): text = forms.CharField(widget=forms.Textarea(attrs={ 'rows': 42, 'cols': 55 }))
class SelectForm(forms.Form): foo = forms.CharField(widget=forms.Select(choices=choices), required=False)
class ContactForm(forms.Form): """ Base contact form class. The following form attributes can be overridden when creating the form or in a subclass. If you need more flexibility, you can instead override the associated methods such as `get_from_email()` (see below). ``subject_intro`` Prefix used to create the subject line. Default is ``settings.ENVELOPE_SUBJECT_INTRO``. ``from_email`` Used in the email from. Defaults to ``settings.ENVELOPE_FROM_EMAIL``. ``email_recipients`` List of email addresses to send the email to. Defaults to ``settings.ENVELOPE_EMAIL_RECIPIENTS``. ``template_name`` Template used to render the email message. Defaults to ``envelope/email_body.txt``. """ sender = forms.CharField(label=_("From")) email = forms.EmailField(label=_("Email")) subject = forms.CharField(label=_("Subject"), required=False) message = forms.CharField(label=_("Message"), widget=forms.Textarea()) subject_intro = settings.SUBJECT_INTRO from_email = settings.FROM_EMAIL email_recipients = settings.EMAIL_RECIPIENTS template_name = 'envelope/email_body.txt' def __init__(self, *args, **kwargs): for kwarg in list(kwargs): if hasattr(self, kwarg): setattr(self, kwarg, kwargs.pop(kwarg)) super(ContactForm, self).__init__(*args, **kwargs) def save(self): """ Sends the message. """ subject = self.get_subject() from_email = self.get_from_email() email_recipients = self.get_email_recipients() message_body = self.get_message_body() try: message = mail.EmailMessage( subject=subject, body=message_body, from_email=from_email, to=email_recipients, headers={'Reply-To': self.cleaned_data['email']}) message.send() after_send.send(sender=self.__class__, message=message, form=self) logger.info( _("Contact form submitted and sent (from: %s)") % self.cleaned_data['email']) except SMTPException: logger.exception(_("An error occured while sending the email")) return False else: return True def get_context(self): """ Returns context dictionary for the email body template. By default, the template has access to all form fields' values stored in ``self.cleaned_data``. Override this method to set additional template variables. """ return self.cleaned_data.copy() def get_subject(self): """ Returns a string to be used as the email subject. Override this method to customize the display of the subject. """ return self.subject_intro + self.cleaned_data['subject'] def get_message_body(self): """ Returns a string to be used as the email body. Override this method to customize the display of the body. """ context = self.get_context() return render_to_string(self.get_template_names(), context) def get_from_email(self): """ Returns the from email address. Override to customize how the from email address is determined. """ return self.from_email def get_email_recipients(self): """ Returns a list of recipients for the message. Override to customize how the email recipients are determined. """ return self.email_recipients def get_template_names(self): """ Returns a template_name (or list of template_names) to be used for the email message. Override to use your own method choosing a template name. """ return self.template_name