wtforms field factory
Create fields on-the-fly at form construction time.
Why?
In order to e.g. translate field labels depending on the request without relying on global state. Additionally, you can conditionally exclude fields. This avoids dodgy workarounds needed when e.g. having a form field that is not relevant or feasible to pass during unit testing.
How?
Let's look at a use case where a field label must change depending on the locale(s) of the request:
from gettext import translation
from typing import List
from wtforms_field_factory import field, Form, DefaultMeta
from wtforms import StringField
class MyMeta(DefaultMeta):
def __init__(self, ordered_locales: List[str]):
self.ordered_locales = ordered_locales
@property
def locales(self):
# translate messages within wtforms depending on the request's locale(s)
return self.ordered_locales
class MyBaseForm(Form):
def __init__(self, ordered_locales: List[str], **kwargs):
self.ordered_locales = ordered_locales
super().__init__(meta=MyMeta(ordered_locales), **kwargs)
@field(name="name")
def name_field(self):
_ = translation("default", languages=self.ordered_locales)
return StringField(label=_("Name"))
The example above will not only translate the name field's label but also internal wtforms messages such as field errors.
In cases where an external function is responsible for creating the field (useful for reusing field factories) or if you want to precompute certain objects (e.g. the GNUTranslations object), the following can be done:
@field(name="name")
def name_field(_cls, _): # since the associated attribute is bound, we need the class type as first arg
return StringField(label=_("Name"))
class MyBaseForm(Form):
some_class_attribute = name_field # to make Form actually discover this factory
def __init__(self, ordered_locales: List[str], **kwargs):
self.set_factory_args(translation("default", languages=self.ordered_locales))
super().__init__(meta=MyMeta(ordered_locales), **kwargs)
Just use whatever method you find best. There is not "one" correct way of achieving your goal here. The important part is that you now have an explicit contract and do not rely on global state.
Contributing
Before committing, run the following and check if it succeeds:
pip install --user -r requirements-dev.txt && \
black wtforms_field_factory.py && \
pylint wtforms_field_factory.py && \
pytest && \
coverage report --fail-under=100
Metadata
Release files for wtforms-field-factory 0.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| wtforms-field-factory-0.1.tar.gz | 3.9 kB | Without provenance | Details for wtforms-field-factory-0.1.tar.gz |
Release files / wtforms-field-factory-0.1.tar.gz
| Download URL | wtforms-field-factory-0.1.tar.gz |
|---|---|
| Size | 3.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
7bbc364fcc88ad4aa6ccc464fd4ef4bcac793f5b81165ac0b6cceb88d4b1d82e
|
|
BLAKE2b-256 checksum How to use checksums |
51f2e248787b47aa8f44ee8cd34883502bd87d45f0ec03ed073508cc195493e4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.1.1 pkginfo/1.5.0.1 requests/2.20.1 setuptools/46.0.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.8.0
|
Security for wtforms-field-factory 0.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.