def __init__(self, fields, prefix='', meta=DefaultMeta()): """ :param fields: A dict or sequence of 2-tuples of partially-constructed fields. :param prefix: If provided, all fields will have their name prefixed with the value. :param meta: A meta instance which is used for configuration and customization of WTForms behaviors. """ if prefix and prefix[-1] not in '-_;:/.': prefix += '-' self.meta = meta self._prefix = prefix self._errors = None self._fields = OrderedDict() if hasattr(fields, 'items'): fields = fields.items() translations = self._get_translations() extra_fields = [] if meta.csrf: self._csrf = meta.build_csrf(self) extra_fields.extend(self._csrf.setup_form(self)) for name, unbound_field in itertools.chain(fields, extra_fields): options = dict(name=name, prefix=prefix, translations=translations) field = meta.bind_field(self, unbound_field, options) self._fields[name] = field
def __init__(self, formdata=None, obj=None, data=None, csrf_enabled=None, csrf_context=None, secret_key=None, prefix='', meta=DefaultMeta()): if csrf_enabled is None: csrf_enabled = current_app.config.get('WTF_CSRF_ENABLED', True) self.csrf_enabled = csrf_enabled if self.csrf_enabled: if csrf_context is None: csrf_context = session if secret_key is None: # It wasn't passed in, check if the class has a SECRET_KEY secret_key = getattr(self, "SECRET_KEY", None) self.SECRET_KEY = secret_key else: csrf_context = {} self.SECRET_KEY = '' if prefix and prefix[-1] not in '-_;:/.': prefix += '-' self.meta = meta self._prefix = prefix self._errors = None translations = self._get_translations() extra_fields = [] if meta.csrf: self._csrf = meta.build_csrf(self) extra_fields.extend(self._csrf.setup_form(self)) def bind_fields(tab): tab.children = [] for field in tab.unbound_children: name = field.kwargs["name"] del field.kwargs["name"] tab.children.append(field.bind(self, name)) tab.unbound_children = [] for t in tab.tabs: c = bind_fields(t) if c == 0: print(t) print(t.name) del t print(tab.children) return len(tab.children) bind_fields(self._fields.root) self.process(formdata, obj, data)
class UploadForm(FlaskForm): meta = DefaultMeta() message = HiddenField("Masge", validators=[DataRequired()], _meta=meta) demo = FileField(validators=[ FileAllowed(file_ext, 'This extension is not allowed!'), FileRequired('File was empty!') ]) submit = SubmitField('Upload')
import itertools from collections import OrderedDict from wtforms.meta import DefaultMeta __all__ = ("BaseForm", "Form") _default_meta = DefaultMeta() class BaseForm: """ Base Form Class. Provides core behaviour like field construction, validation, and data and error proxying. """ def __init__(self, fields, prefix="", meta=_default_meta): """ :param fields: A dict or sequence of 2-tuples of partially-constructed fields. :param prefix: If provided, all fields will have their name prefixed with the value. :param meta: A meta instance which is used for configuration and customization of WTForms behaviors. """ if prefix and prefix[-1] not in "-_;:/.": prefix += "-" self.meta = meta self._prefix = prefix