Forms

Extended WTForms field.

Classes TimeField, DatePickerWidget, DateTimePickerWidget and TimePickerWidget are taken from flask-admin extension.

copyright:
  1. 2011 by wilsaj.
license:

BSD, see LICENSE for more details.

source:

https://raw.github.com/wilsaj/flask-admin/master/flask_admin/wtforms.py

class invenio.utils.forms.AutocompleteField(label=None, validators=None, data_provide='typeahead', data_source=None, **kwargs)

Text field with simple autocompletion.

class invenio.utils.forms.DatePickerWidget(input_type=None)

TextInput widget that adds a ‘datepicker’ class.

TextInput widget that adds a ‘datepicker’ class to the html input element; this makes it easy to write a jQuery selector that adds a UI widget for date picking.

class invenio.utils.forms.DateTimePickerWidget(input_type=None)

TextInput widget that adds a ‘datetimepicker’ class.

TextInput widget that adds a ‘datetimepicker’ class to the html adds a UI widget for datetime picking.

class invenio.utils.forms.FilterForm(*args, **kwargs)

Filter forms contains hidden fields to keep sorting.

class invenio.utils.forms.FilterStringField(*args, **kwargs)

Define a FilterStringField.

class invenio.utils.forms.InvenioBaseForm(*args, **kwargs)

Define a InvenioBaseForm.

add_fields(name, field)

Add a field.

validate_csrf_token(field)

Disable CRSF proection during testing.

class invenio.utils.forms.InvenioForm(formdata=None, obj=None, prefix='', data=None, meta=None, **kwargs)

Define a Invenio Form.

required_field_names

Return all required field names.

class invenio.utils.forms.RemoteAutocompleteField(label=None, validators=None, remote=None, min_length=None, highlight=None, data_key=None, data_value=None, *args, **kwargs)

Define a text field with autocompletion from remote.

How to use it:

First, use RemoteAutocompleteField in a form.

class MyForm(InvenioBaseForm):
        myfield = RemoteAutocompleteField(
            'My Label',
            # remote url where the field can ask suggestions
            remote='/api/path/to/query?query=%QUERY',
            # minimum length to start query
            min_length=3,
            # highlight the results in suggestions
            highlight='true',
            # field to use as key to submit with the form
            data_key='field_to_use_as_key',
            # field to visualize in the input field
            data_value='field_to_visualize'
        )

Then, create the API function that returns suggestions from url path /api/path/to/query.

Note: you should return the results in JSON format, like:

{
    "results": [
        {
            field_to_use_as_key: 'my_key_1',
            field_to_visualize: 'my_label_1'
        },
        {
            field_to_use_as_key: 'my_key_2',
            field_to_visualize: 'my_label_2'
        },
    ]
}

After that, you should manually initialize the javascript.

To do that, for example, you can create a javascript init file init.js into your module’s directory.

E.g: invenio/modules/mymodule/static/js/mymodule/init.js:

require([
    'js/remote.autocomplete.field'
], function(autocomplete) {
    // init the javascript interface
    autocomplete.attachTo($('input.remote-typeahead-widget'))
})

Finally, if you create a new init.js file, you need to add this in your bundle in your bundles.py file.

E.g:

js = Bundle(
    'js/mymodule/init.js',
    filters=RequireJSFilter(exclude=[_j, _i]),
    output="mymodule-init.js",
    weight=50
)
set_remote(value)

Update remote url.

class invenio.utils.forms.RemoteTypeheadWidget(label, **kwargs)

Typeahead widget that acquire data from remote.

set_remote(value)

Update remote url.

class invenio.utils.forms.RowWidget(**kwargs)

Renders a list of fields as a set of table rows with th/td pairs.

class invenio.utils.forms.TimeField(label=None, validators=None, format='%H:%M:%S', **kwargs)

A text field which stores a time.time matching a format.

process_formdata(valuelist)

Join time string.

class invenio.utils.forms.TimePickerWidget(input_type=None)

TextInput widget that adds a ‘timepicker’ class.

TextInput widget that adds a ‘timepicker’ class to the html input element; this makes it easy to write a jQuery selector that adds a UI widget for time picking.

class invenio.utils.forms.TypeheadWidget(autocomplete_list, data_provide)

TextInput that use typeahead for autocompletion.

invenio.utils.forms.has_file_field(form)

Test whether or not a form has a FileField in it.

This is used to know whether or not we need to set enctype to multipart/form-data.