form.parser.core
onegov.form includes it’s own markdownish form syntax, inspired by https://github.com/maleldil/wmd
The goal of this syntax is to enable the creation of forms through the web, without having to use javascript, html or python code.
Also, just like Markdown, we want this syntax to be readable by humans.
How it works
Internally, the form syntax is converted into a YAML file, which is in turn parsed and turned into a WTForms class. We decided to go for the intermediate YAML file because it’s easy to define a Syntax which correctly supports indentation. Our pyparsing approach was flimsy at best.
Parser Errors
There’s currently no sophisticated error check. It’s possible that the parser misunderstand something you defined without warning. So be careful to check that what you wanted was actually what you got.
Syntax
Fields
Every field is identified by a label, an optional ‘required’ indicator and a
field definition. The Label can be any kind of text, not including *
and
=
.
The *
indicates that a field is required. The =
separates the
identifier from the definition.
A required field starts like this:
My required field * =
An optional field starts like this:
My optional field =
Following the identifier is the field definition. For example, this defines a textfield:
My textfield = ___
Comments can be added beneath a field, using the same indentation:
My textfield = ___
<< Explanation for my field >>
All characters are allowed except ‘’>’’.
Complex example:
Delivery * =
(x) I want it delivered
Alternate Address =
(x) No
( ) Yes
Street = ___
<< street >>
Town = ___
<< Alt >>
( ) I want to pick it up
<< delivery >>
Kommentar = ...
<< kommentar >>
All possible fields are documented further below.
Fieldsets
Fields are grouped into fieldsets. The fieldset of a field is the fieldset that was last defined:
# Fieldset 1
I belong to Fieldset 1 = ___
# Fieldset 2
I belong to Fieldset 2 = ___
If no fieldset is defined, the fields don’t belong to a fieldset. To stop putting fields in a fieldset, define an empty fieldeset:
# Fieldset 1
I belong to Fieldset 1 = ___
# ...
I don't belong to a Fieldset = ___
Available Fields
Textfield
A textfield consists of exactly three underscores:
I'm a textfield = ___
If the textfield is limited in length, the length can be given:
I'm a limited textfield = ___[50]
The length of such textfields is validated.
Additionally, textfields may use regexes to validate their contents:
I'm a numbers-only textfield = ___/^[0-9]+$
You can combine the length with a regex, though you probably don’t want to:
I'm a length-limited numbers-only textfield = ___[4]/^[0-9]+$
This could be simplified as follows:
I’m a length-limited numbers-only textfield = ___/^[0-9]{0,4}$
Note that you don’t need to specify the beginning (^) and the end ($) of the
string, but not doing so might result in unexpected results. For example,
while ‘123abc’ is invalid for ___/^[0-9]+$
, it is perfectly valid for
___/[0-9]+
. The latter only demands that the text starts with a number,
not that it only consists of numbers!
Textarea
A textarea has no limit and consists of exactly three dots:
I'm a textarea = ...
Optionally, the number of rows can be passed to the field. This changes the way the textarea looks, not the way it acts:
I'm a textarea with 10 rows = ...[10]
Password
A password field consists of exactly three stars:
I'm a password = ***
E-Mail
An e-mail field consists of exactly three @
:
I'm an e-mail field = @@@
URL
An url field consists of the http/https prefix:
I'm an url field = http://
I'm the exact same = https://
Whether or not you enter http or https has no bearing on the validation.
Video Link
An url field pointing to a video video-url
:
I' am a video link = video-url
In case of vimeo or youtube videos the video will be embedded in the page, otherwise the link will be shown.
Date
A date (without time) is defined by this exact string: YYYY.MM.DD
:
I'm a date field = YYYY.MM.DD
Note that this doesn’t mean that the date format can be influenced.
A date field optionally can be limited to a relative or absolute date range.
Note that the edges of the interval are inclusive. The list of possible
grains for relative dates are years
, months
, weeks
and days
as well as the special value today
.
I’m a future date field = YYYY.MM.DD (+1 days..) I’m on today or in the future = YYYY.MM.DD (today..) At least two weeks ago = YYYY.MM.DD (..-2 weeks) Between 2010 and 2020 = YYYY.MM.DD (2010.01.01..2020.12.31)
Datetime
A date (with time) is defined by this exact string: YYYY.MM.DD HH:MM
:
I'm a datetime field = YYYY.MM.DD HH:MM
I'm a futue datetime field = YYYY.MM.DD HH:MM (today..)
Again, this doesn’t mean that the datetime format can be influenced.
The same range validation that can be applied to date fields can also be applied to datetime. Note however that the Validation will be applied to to the date portion. The time portion is ignored completely.
Time
A Time is defined by this exact string: HH:MM
:
I'm a time field = HH:MM
One more time, this doesn’t mean that the datetime format can be influenced.
Numbers
There are two types of number fields. An integer and a float field:
I'm an integer field = 0..99
I'm an integer field of a different range = -100..100
I'm a float field = 0.00..99.00
I'm an float field of a different range = -100.00..100.00
Integer fields optionally can have a price attached to them which will be multiplied by the supplied integer.
Number of stamps to include = 0..30 (0.85 CHF)
Code
To write code in a certain syntax, use the following:
Description = <markdown>
Currently, only markdown is supported.
Files
A file upload is defined like this:
I'm a file upload field = *.*
This particular example would allow any file. To allow only certain files do something like this:
I'm a image filed = *.png|*.jpg|*.gif
I'm a document = *.doc
I'm any document = *.doc|*.pdf
The files are checked against their file extension. Onegov.form also checks that uploaded files have the mimetype they claim to have and it won’t accept obviously dangerous uploads like binaries (unless you really want to).
Standard Numbers
onegov.form uses python-stdnum to offer a wide range of standard formats that are guaranteed to be validated.
To use, simply use a #, followed by the stdnum format to use:
I'm a valid IBAN (or empty) = # iban
I'm a valid IBAN (required) * = # iban
The format string after the # must be importable from stdnum. In other words,
this must work, if you are using ch.ssn
(to use an example):
$ python
>>> from stdnum.ch import ssn
This is a bit of an advanced feature and since it delegates most work to an external library there’s no guarantee that a format once used may be reused in the future.
Still, the library should be somewhat stable and the benefit is huge.
To see the available format, have a look at the docs: https://arthurdejong.org/python-stdnum/doc/1.1/index.html#available-formats
Checkboxes
Checkboxes work exactly like radio buttons, just that you can select multiple fields:
Extras =
[x] Phone insurance
[ ] Phone case
[x] Extra battery
Just like radiobuttons, checkboxes may be nested to created dependencies:
Additional toppings =
[ ] Salami
[ ] Olives
[ ] Other
Description = ___
Pricing Information
Radio buttons and checkboxes may be priced. For example, the following order form can be modeled:
Node Size =
( ) Small (20 USD)
(x) Medium (30 USD)
( ) Large (40 USD)
Extras =
[x] Second IP Address (20 CHF)
[x] Backup (20 CHF)
Delivery =
(x) Pickup (0 CHF)
( ) Delivery (5 CHF!)
The additional pricing metadata can be used to provide simple order forms. As in any other form, dependencies are taken into account.
The optional ! at the end of the price indicates that credit card payment will become mandatory if this option is selected. It is possible to achieve this without a price increase too: (0 CHF!)
Attributes
Classes
Extends the default yaml loader with customized constructors. |
|
Adds decorated functions to as constructors to the CustomLoader. |
|
Represents a parsed fieldset. |
|
Represents a parsed choice. |
|
Represents a parsed field. |
|
Represents a parsed field. |
|
Represents a parsed field. |
|
Represents a parsed field. |
|
Represents a parsed field. |
|
Represents a parsed field. |
|
Represents a parsed field. |
|
Represents a parsed field. |
|
Represents a parsed field. |
|
Represents a parsed field. |
|
Represents a parsed field. |
|
Represents a parsed field. |
|
Represents a parsed field. |
|
Represents a parsed field. |
|
Represents a parsed field. |
|
Represents a parsed field. |
|
Represents a parsed field. |
|
Represents a parsed field. |
Functions
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Takes the given formcode and returns an intermediate representation |
|
Takes the given parsed field block and yields the fields from it |
|
|
|
Returns true if the given parser expression matches the given text. |
|
Returns the result of the given parser expression and text, or None. |
|
Takes the raw form source and prepares it for the translation into |
|
Makes sure that the given lines all belong to a fieldset. That means |
|
Returns False if indent is other than a multiple of 4, else True |
|
Takes the given form text and constructs an easier to parse yaml |
Module Contents
- form.parser.core.BasicParsedField: TypeAlias = 'PasswordField | EmailField | UrlField | VideoURLField | DateField | DatetimeField | TimeField |...[source]
- class form.parser.core.CustomLoader(stream)[source]
Bases:
yaml.SafeLoader
Extends the default yaml loader with customized constructors.
- class form.parser.core.constructor(tag: str)[source]
Adds decorated functions to as constructors to the CustomLoader.
- __call__(fn: Callable[[CustomLoader, ScalarNode], pp.ParseResults]) Callable[[CustomLoader, ScalarNode], pp.ParseResults] [source]
- form.parser.core.construct_textfield(loader: CustomLoader, node: yaml.nodes.ScalarNode) pyparsing.ParseResults [source]
- form.parser.core.construct_textarea(loader: CustomLoader, node: yaml.nodes.ScalarNode) pyparsing.ParseResults [source]
- form.parser.core.construct_syntax(loader: CustomLoader, node: yaml.nodes.ScalarNode) pyparsing.ParseResults [source]
- form.parser.core.construct_email(loader: CustomLoader, node: yaml.nodes.ScalarNode) pyparsing.ParseResults [source]
- form.parser.core.construct_url(loader: CustomLoader, node: yaml.nodes.ScalarNode) pyparsing.ParseResults [source]
- form.parser.core.construct_video_url(loader: CustomLoader, node: yaml.nodes.ScalarNode) pyparsing.ParseResults [source]
- form.parser.core.construct_stdnum(loader: CustomLoader, node: yaml.nodes.ScalarNode) pyparsing.ParseResults [source]
- form.parser.core.construct_date(loader: CustomLoader, node: yaml.nodes.ScalarNode) pyparsing.ParseResults [source]
- form.parser.core.construct_datetime(loader: CustomLoader, node: yaml.nodes.ScalarNode) pyparsing.ParseResults [source]
- form.parser.core.construct_time(loader: CustomLoader, node: yaml.nodes.ScalarNode) pyparsing.ParseResults [source]
- form.parser.core.construct_radio(loader: CustomLoader, node: yaml.nodes.ScalarNode) pyparsing.ParseResults [source]
- form.parser.core.construct_checkbox(loader: CustomLoader, node: yaml.nodes.ScalarNode) pyparsing.ParseResults [source]
- form.parser.core.construct_fileinput(loader: CustomLoader, node: yaml.nodes.ScalarNode) pyparsing.ParseResults [source]
- form.parser.core.construct_multiplefileinput(loader: CustomLoader, node: yaml.nodes.ScalarNode) pyparsing.ParseResults [source]
- form.parser.core.construct_password(loader: CustomLoader, node: yaml.nodes.ScalarNode) pyparsing.ParseResults [source]
- form.parser.core.construct_decimal_range(loader: CustomLoader, node: yaml.nodes.ScalarNode) pyparsing.ParseResults [source]
- form.parser.core.construct_integer_range(loader: CustomLoader, node: yaml.nodes.ScalarNode) pyparsing.ParseResults [source]
- form.parser.core.flatten_fields(fields: Sequence[ParsedField] | None) Iterator[ParsedField] [source]
- form.parser.core.find_field(fieldsets: Iterable[Fieldset], id: str | None) Fieldset | ParsedField | None [source]
- class form.parser.core.Fieldset(label: str, fields: Sequence[ParsedField] | None = None)[source]
Represents a parsed fieldset.
- class form.parser.core.Choice(key: str, label: str, selected: bool = False, fields: Sequence[ParsedField] | None = None)[source]
Represents a parsed choice.
Note: Choices may have child-fields which are meant to be shown to the user if the given choice was selected.
- class form.parser.core.Field(label: str, required: bool, parent: ParsedField | None = None, fieldset: Fieldset | None = None, field_help: str | None = None, human_id: str | None = None, **extra_attributes: Any)[source]
Represents a parsed field.
- class form.parser.core.PasswordField(label: str, required: bool, parent: ParsedField | None = None, fieldset: Fieldset | None = None, field_help: str | None = None, human_id: str | None = None, **extra_attributes: Any)[source]
Bases:
Field
Represents a parsed field.
- class form.parser.core.EmailField(label: str, required: bool, parent: ParsedField | None = None, fieldset: Fieldset | None = None, field_help: str | None = None, human_id: str | None = None, **extra_attributes: Any)[source]
Bases:
Field
Represents a parsed field.
- class form.parser.core.UrlField(label: str, required: bool, parent: ParsedField | None = None, fieldset: Fieldset | None = None, field_help: str | None = None, human_id: str | None = None, **extra_attributes: Any)[source]
Bases:
Field
Represents a parsed field.
- class form.parser.core.VideoURLField(label: str, required: bool, parent: ParsedField | None = None, fieldset: Fieldset | None = None, field_help: str | None = None, human_id: str | None = None, **extra_attributes: Any)[source]
Bases:
Field
Represents a parsed field.
- class form.parser.core.DateField(label: str, required: bool, parent: ParsedField | None = None, fieldset: Fieldset | None = None, field_help: str | None = None, human_id: str | None = None, **extra_attributes: Any)[source]
Bases:
Field
Represents a parsed field.
- class form.parser.core.DatetimeField(label: str, required: bool, parent: ParsedField | None = None, fieldset: Fieldset | None = None, field_help: str | None = None, human_id: str | None = None, **extra_attributes: Any)[source]
Bases:
Field
Represents a parsed field.
- classmethod create(field: pyparsing.ParseResults, identifier: pyparsing.ParseResults, parent: ParsedField | None = None, fieldset: Fieldset | None = None, field_help: str | None = None) DatetimeField [source]
- class form.parser.core.TimeField(label: str, required: bool, parent: ParsedField | None = None, fieldset: Fieldset | None = None, field_help: str | None = None, human_id: str | None = None, **extra_attributes: Any)[source]
Bases:
Field
Represents a parsed field.
- class form.parser.core.StringField(label: str, required: bool, parent: ParsedField | None = None, fieldset: Fieldset | None = None, field_help: str | None = None, human_id: str | None = None, **extra_attributes: Any)[source]
Bases:
Field
Represents a parsed field.
- classmethod create(field: pyparsing.ParseResults, identifier: pyparsing.ParseResults, parent: ParsedField | None = None, fieldset: Fieldset | None = None, field_help: str | None = None) StringField [source]
- class form.parser.core.TextAreaField(label: str, required: bool, parent: ParsedField | None = None, fieldset: Fieldset | None = None, field_help: str | None = None, human_id: str | None = None, **extra_attributes: Any)[source]
Bases:
Field
Represents a parsed field.
- classmethod create(field: pyparsing.ParseResults, identifier: pyparsing.ParseResults, parent: ParsedField | None = None, fieldset: Fieldset | None = None, field_help: str | None = None) TextAreaField [source]
- class form.parser.core.CodeField(label: str, required: bool, parent: ParsedField | None = None, fieldset: Fieldset | None = None, field_help: str | None = None, human_id: str | None = None, **extra_attributes: Any)[source]
Bases:
Field
Represents a parsed field.
- class form.parser.core.StdnumField(label: str, required: bool, parent: ParsedField | None = None, fieldset: Fieldset | None = None, field_help: str | None = None, human_id: str | None = None, **extra_attributes: Any)[source]
Bases:
Field
Represents a parsed field.
- class form.parser.core.IntegerRangeField(label: str, required: bool, parent: ParsedField | None = None, fieldset: Fieldset | None = None, field_help: str | None = None, human_id: str | None = None, **extra_attributes: Any)[source]
Bases:
Field
Represents a parsed field.
- classmethod create(field: pyparsing.ParseResults, identifier: pyparsing.ParseResults, parent: ParsedField | None = None, fieldset: Fieldset | None = None, field_help: str | None = None) IntegerRangeField [source]
- class form.parser.core.DecimalRangeField(label: str, required: bool, parent: ParsedField | None = None, fieldset: Fieldset | None = None, field_help: str | None = None, human_id: str | None = None, **extra_attributes: Any)[source]
Bases:
Field
Represents a parsed field.
- classmethod create(field: pyparsing.ParseResults, identifier: pyparsing.ParseResults, parent: ParsedField | None = None, fieldset: Fieldset | None = None, field_help: str | None = None) DecimalRangeField [source]
- class form.parser.core.FileinputField(label: str, required: bool, parent: ParsedField | None = None, fieldset: Fieldset | None = None, field_help: str | None = None, human_id: str | None = None, **extra_attributes: Any)[source]
Bases:
FileinputBase
,Field
Represents a parsed field.
- class form.parser.core.MultipleFileinputField(label: str, required: bool, parent: ParsedField | None = None, fieldset: Fieldset | None = None, field_help: str | None = None, human_id: str | None = None, **extra_attributes: Any)[source]
Bases:
FileinputBase
,Field
Represents a parsed field.
- class form.parser.core.OptionsField[source]
- class form.parser.core.RadioField(label: str, required: bool, parent: ParsedField | None = None, fieldset: Fieldset | None = None, field_help: str | None = None, human_id: str | None = None, **extra_attributes: Any)[source]
Bases:
OptionsField
,Field
Represents a parsed field.
- class form.parser.core.CheckboxField(label: str, required: bool, parent: ParsedField | None = None, fieldset: Fieldset | None = None, field_help: str | None = None, human_id: str | None = None, **extra_attributes: Any)[source]
Bases:
OptionsField
,Field
Represents a parsed field.
- form.parser.core.parse_formcode(formcode: str, enable_edit_checks: bool = False) list[Fieldset] [source]
Takes the given formcode and returns an intermediate representation that can be used to generate forms or do other things.
- Parameters:
formcode – string representing formcode to be parsed
enable_edit_checks – bool to activate additional check after
editing the form. Should only be active originating from forms.validators.py
- form.parser.core.parse_field_block(field_block: dict[str, Any], field_classes: dict[str, type[ParsedField]], used_ids: set[str], fieldset: Fieldset, parent: ParsedField | None = None) ParsedField [source]
Takes the given parsed field block and yields the fields from it
- form.parser.core.match(expr: pyparsing.ParserElement, text: str) bool [source]
Returns true if the given parser expression matches the given text.
- form.parser.core.try_parse(expr: pyparsing.ParserElement, text: str) pyparsing.ParseResults | None [source]
Returns the result of the given parser expression and text, or None.
- form.parser.core.prepare(text: str) Iterator[tuple[int, str]] [source]
Takes the raw form source and prepares it for the translation into yaml.
- form.parser.core.ensure_a_fieldset(lines: Iterable[tuple[int, str]]) Iterator[tuple[int, str]] [source]
Makes sure that the given lines all belong to a fieldset. That means adding an empty fieldset before all lines, if none is found first.
- form.parser.core.validate_indent(indent: str) bool [source]
Returns False if indent is other than a multiple of 4, else True
- form.parser.core.translate_to_yaml(text: str, enable_edit_checks: bool = False) Iterator[str] [source]
Takes the given form text and constructs an easier to parse yaml string.
- Parameters:
text – string to be parsed
enable_edit_checks – bool to activate additional checks after
editing a form. Should only be active originating from forms.validators.py