class DummyField: """ Implement a very small subset of WTFormsField to encapsulate csrf tokens if no field is present (e. g. AJAX) """ def __init__(self, data): self._translations = DummyTranslations() self.data = data def gettext(self, string): """ Get a translation for the given message. This proxies for the internal translations object. :param string: A unicode string to be translated. :return: A unicode string which is the translated output. """ return self._translations.gettext(string)
class Field(object): """ Field base class """ errors = tuple() process_errors = tuple() raw_data = None validators = tuple() widget = None _formfield = True _translations = DummyTranslations() do_not_call_in_templates = True # Allow Django 1.4 traversal def __new__(cls, *args, **kwargs): if '_form' in kwargs and '_name' in kwargs: return super(Field, cls).__new__(cls) else: return UnboundField(cls, *args, **kwargs) def __init__(self, label=None, validators=None, filters=tuple(), description='', id=None, default=None, widget=None, render_kw=None, _form=None, _name=None, _prefix='', _translations=None, _meta=None): """ Construct a new field. :param label: The label of the field. :param validators: A sequence of validators to call when `validate` is called. :param filters: A sequence of filters which are run on input data by `process`. :param description: A description for the field, typically used for help text. :param id: An id to use for the field. A reasonable default is set by the form, and you shouldn't need to set this manually. :param default: The default value to assign to the field, if no form or object input is provided. May be a callable. :param widget: If provided, overrides the widget used to render the field. :param dict render_kw: If provided, a dictionary which provides default keywords that will be given to the widget at render time. :param _form: The form holding this field. It is passed by the form itself during construction. You should never pass this value yourself. :param _name: The name of this field, passed by the enclosing form during its construction. You should never pass this value yourself. :param _prefix: The prefix to prepend to the form name of this field, passed by the enclosing form during construction. :param _translations: A translations object providing message translations. Usually passed by the enclosing form during construction. See :doc:`I18n docs <i18n>` for information on message translations. :param _meta: If provided, this is the 'meta' instance from the form. You usually don't pass this yourself. If `_form` and `_name` isn't provided, an :class:`UnboundField` will be returned instead. Call its :func:`bind` method with a form instance and a name to construct the field. """ if _translations is not None: self._translations = _translations if _meta is not None: self.meta = _meta elif _form is not None: self.meta = _form.meta else: raise TypeError("Must provide one of _form or _meta") self.default = default self.description = description self.render_kw = render_kw self.filters = filters self.flags = Flags() self.name = _prefix + _name self.short_name = _name self.type = type(self).__name__ self.validators = validators or list(self.validators) self.id = id or self.name self.label = Label(self.id, label if label is not None else self.gettext(_name.replace('_', ' ').title())) if widget is not None: self.widget = widget for v in itertools.chain(self.validators, [self.widget]): flags = getattr(v, 'field_flags', ()) for f in flags: setattr(self.flags, f, True) def __unicode__(self): """ Returns a HTML representation of the field. For more powerful rendering, see the `__call__` method. """ return self() def __str__(self): """ Returns a HTML representation of the field. For more powerful rendering, see the `__call__` method. """ return self() def __html__(self): """ Returns a HTML representation of the field. For more powerful rendering, see the :meth:`__call__` method. """ return self() def __call__(self, **kwargs): """ Render this field as HTML, using keyword args as additional attributes. This delegates rendering to :meth:`meta.render_field <wtforms.meta.DefaultMeta.render_field>` whose default behavior is to call the field's widget, passing any keyword arguments from this call along to the widget. In all of the WTForms HTML widgets, keyword arguments are turned to HTML attributes, though in theory a widget is free to do anything it wants with the supplied keyword arguments, and widgets don't have to even do anything related to HTML. """ return self.meta.render_field(self, kwargs) def gettext(self, string): """ Get a translation for the given message. This proxies for the internal translations object. :param string: A unicode string to be translated. :return: A unicode string which is the translated output. """ return self._translations.gettext(string) def ngettext(self, singular, plural, n): """ Get a translation for a message which can be pluralized. :param str singular: The singular form of the message. :param str plural: The plural form of the message. :param int n: The number of elements this message is referring to """ return self._translations.ngettext(singular, plural, n) def validate(self, form, extra_validators=tuple()): """ Validates the field and returns True or False. `self.errors` will contain any errors raised during validation. This is usually only called by `Form.validate`. Subfields shouldn't override this, but rather override either `pre_validate`, `post_validate` or both, depending on needs. :param form: The form the field belongs to. :param extra_validators: A sequence of extra validators to run. """ self.errors = list(self.process_errors) stop_validation = False # Call pre_validate try: self.pre_validate(form) except StopValidation as e: if e.args and e.args[0]: self.errors.append(e.args[0]) stop_validation = True except ValueError as e: self.errors.append(e.args[0]) # Run validators if not stop_validation: chain = itertools.chain(self.validators, extra_validators) stop_validation = self._run_validation_chain(form, chain) # Call post_validate try: self.post_validate(form, stop_validation) except ValueError as e: self.errors.append(e.args[0]) return len(self.errors) == 0 def _run_validation_chain(self, form, validators): """ Run a validation chain, stopping if any validator raises StopValidation. :param form: The Form instance this field belongs to. :param validators: a sequence or iterable of validator callables. :return: True if validation was stopped, False otherwise. """ for validator in validators: try: validator(form, self) except StopValidation as e: if e.args and e.args[0]: self.errors.append(e.args[0]) return True except ValueError as e: self.errors.append(e.args[0]) return False def pre_validate(self, form): """ Override if you need field-level validation. Runs before any other validators. :param form: The form the field belongs to. """ pass def post_validate(self, form, validation_stopped): """ Override if you need to run any field-level validation tasks after normal validation. This shouldn't be needed in most cases. :param form: The form the field belongs to. :param validation_stopped: `True` if any validator raised StopValidation. """ pass def process(self, formdata, data=unset_value): """ Process incoming data, calling process_data, process_formdata as needed, and run filters. If `data` is not provided, process_data will be called on the field's default. Field subclasses usually won't override this, instead overriding the process_formdata and process_data methods. Only override this for special advanced processing, such as when a field encapsulates many inputs. """ self.process_errors = [] if data is unset_value: try: data = self.default() except TypeError: data = self.default self.object_data = data try: self.process_data(data) except ValueError as e: self.process_errors.append(e.args[0]) if formdata is not None: if self.name in formdata: self.raw_data = formdata.getlist(self.name) else: self.raw_data = [] try: self.process_formdata(self.raw_data) except ValueError as e: self.process_errors.append(e.args[0]) try: for filter in self.filters: self.data = filter(self.data) except ValueError as e: self.process_errors.append(e.args[0]) def process_data(self, value): """ Process the Python data applied to this field and store the result. This will be called during form construction by the form's `kwargs` or `obj` argument. :param value: The python object containing the value to process. """ self.data = value def process_formdata(self, valuelist): """ Process data received over the wire from a form. This will be called during form construction with data supplied through the `formdata` argument. :param valuelist: A list of strings to process. """ if valuelist: self.data = valuelist[0] def populate_obj(self, obj, name): """ Populates `obj.<name>` with the field's data. :note: This is a destructive operation. If `obj.<name>` already exists, it will be overridden. Use with caution. """ setattr(obj, name, self.data)
def dummy_translation(cls): return DummyTranslations()
def __init__(self, data): self._translations = DummyTranslations() self.data = data