inspirehep.modules.forms package

Submodules

inspirehep.modules.forms.bundles module

Bundles for forms used across INSPIRE.

inspirehep.modules.forms.ext module

Forms extension.

class inspirehep.modules.forms.ext.InspireForms(app=None)[source]

Bases: object

init_app(app)[source]

inspirehep.modules.forms.field_base module

Implementation of validators, post-processors and auto-complete functions.

Validators

Following is a short overview over how validators may be defined for fields.

Inline validators (always executed):

class MyForm(...):
    myfield = MyField()

    def validate_myfield(form, field):
        raise ValidationError("Message")

External validators (always executed):

def my_validator(form, field):
    raise ValidationError("Message")

class MyForm(...):
        myfield = MyField(validators=[my_validator])

Field defined validators (always executed):

class MyField(...):
    # ...
    def pre_validate(self, form):
        raise ValidationError("Message")

Default field validators (executed only if external validators are not defined):

class MyField(...):
    def __init__(self, **kwargs):
        defaults = dict(validators=[my_validator])
        defaults.update(kwargs)
        super(MyField, self).__init__(**defaults)

See http://wtforms.simplecodes.com/docs/1.0.4/validators.html for how to write validators.

Post-processors

Post processors follows the same pattern as validators. You may thus specify:

  • Inline processors::

    Form.post_process_<field>(form, field)
    
  • External processors::

    def my_processor(form, field):
        ...
        myfield = MyField(processors=[my_processor])
    
  • Field defined processors (please method documentation)::

    Field.post_process(self, form, extra_processors=[])
    

Auto-complete

  • External auto-completion function::

    def my_autocomplete(form, field, limit=50):
        ...
        myfield = MyField(autocomplete=my_autocomplete)
    
  • Field defined auto-completion function (please method documentation)::

    Field.autocomplete(self, form, limit=50)
    
class inspirehep.modules.forms.field_base.INSPIREField(*args, **kwargs)[source]

Bases: wtforms.fields.core.Field

Base field that all webdeposit fields must inherit from.

add_message(msg, state=None)[source]

Add a message.

Parameters:
  • msg – The message to set
  • state – State of message; info, warning, error, success.
messages

Retrieve field messages.

perform_autocomplete(form, name, term, limit=50)[source]

Run auto-complete method for field.

This method should not be called directly, instead use Form.autocomplete().

post_process(form=None, formfields=[], extra_processors=[], submit=False)[source]

Post process form before saving.

Usually you can do some of the following tasks in the post processing:

  • Set field flags (e.g. self.flags.hidden = True or form.<field>.flags.hidden = True).
  • Set messages (e.g. self.messages.append(‘text’) and self.message_state = ‘info’).
  • Set values of other fields (e.g. form.<field>.data = ‘’).

Processors may stop the processing chain by raising StopIteration.

IMPORTANT: By default the method will execute custom post processors defined in the webdeposit_config. If you override the method, be sure to call this method to ensure extra processors are called:

super(MyField, self).post_process(
    form, extra_processors=extra_processors
)
reset_field_data(exclude=[])[source]

Reset the fields.data value to that of field.object_data.

Usually not called directly, but rather through Form.reset_field_data()

Parameters:exclude – List of formfield names to exclude.
set_flags(flags)[source]

Set field flags.

inspirehep.modules.forms.field_widgets module

Implement custom field widgets.

class inspirehep.modules.forms.field_widgets.BigIconRadioInput(icons={}, **kwargs)[source]

Bases: wtforms.widgets.core.RadioInput

Render a single radio button with icon.

This widget is most commonly used in conjunction with InlineListWidget or some other listing, as a single radio button is not very useful.

input_type = 'radio'
class inspirehep.modules.forms.field_widgets.ButtonWidget(label='', tooltip=None, icon=None, **kwargs)[source]

Bases: object

Implement Bootstrap HTML5 button.

class inspirehep.modules.forms.field_widgets.ColumnInput(widget=None, wrapper=None, **kwargs)[source]

Bases: inspirehep.modules.forms.field_widgets.WrappedInput

Specialized column wrapped input.

wrapper

Wrapper template with description support.

class inspirehep.modules.forms.field_widgets.DynamicItemWidget(**kwargs)[source]

Bases: inspirehep.modules.forms.field_widgets.ListItemWidget

Render each subfield in a ExtendedListWidget enclosed in a div.

It adds also tag with buttons for sorting and removing the item. I.e. something like:

<div><span>"buttons</span>:field</div>
render_subfield(subfield, **kwargs)[source]

Render subfield.

class inspirehep.modules.forms.field_widgets.DynamicListWidget(**kwargs)[source]

Bases: inspirehep.modules.forms.field_widgets.ExtendedListWidget

Render a list of fields as a list of divs.

Additionally adds: * A hidden input to keep track of the last index. * An ‘add another’ item button.

Each subfield is rendered with DynamicItemWidget, which will add buttons for each item to sort and remove the item.

close_tag(field, **kwargs)[source]

Render close tag.

icon_add = 'fa fa-plus'
item_kwargs(field, subfield)[source]

Return keyword arguments for a field.

item_widget = <inspirehep.modules.forms.field_widgets.DynamicItemWidget object>
open_tag(field, **kwargs)[source]

Render open tag.

class inspirehep.modules.forms.field_widgets.ExtendedListWidget(html_tag='ul', item_widget=None, class_=None)[source]

Bases: object

Render a list of fields as a ul, ol or div list.

This is used for fields which encapsulate a list of other fields as subfields. The widget will try to iterate the field to get access to the subfields and call them to render them.

The item_widget decide how subfields are rendered, and usually just provide a thin wrapper around the subfields render method. E.g. ExtendedListWidget renders the ul-tag, while the ListItemWidget renders each li-tag. The content of the li-tag is rendered by the subfield’s widget.

close_tag(field, **kwargs)[source]

Render close tag.

item_kwargs(field, subfield)[source]

Return keyword arguments for a field.

item_widget = <inspirehep.modules.forms.field_widgets.ListItemWidget object>
open_tag(field, **kwargs)[source]

Render open tag.

class inspirehep.modules.forms.field_widgets.ItemWidget[source]

Bases: object

Render each subfield without additional markup around the subfield.

class inspirehep.modules.forms.field_widgets.ListItemWidget(html_tag='li', with_label=True, prefix_label=True, class_=None)[source]

Bases: inspirehep.modules.forms.field_widgets.ItemWidget

Render each subfield in a ExtendedListWidget as a list element.

If with_label is set, the fields label will be rendered. If prefix_label is set, the label will be prefixed, otherwise it will be suffixed.

close_tag(subfield, **kwargs)[source]

Return close tag.

open_tag(subfield, **kwargs)[source]

Return open tag.

render_subfield(subfield, **kwargs)[source]

Render subfield.

class inspirehep.modules.forms.field_widgets.TagInput(input_type=None)[source]

Bases: wtforms.widgets.core.Input

Implement tag input widget.

input_type = 'hidden'
class inspirehep.modules.forms.field_widgets.WrappedInput(widget=None, wrapper=None, **kwargs)[source]

Bases: wtforms.widgets.core.Input

Widget to wrap text input in further markup.

wrapped_widget = <wtforms.widgets.core.TextInput object>
wrapper = '<div>%(field)s</div>'

inspirehep.modules.forms.filter_utils module

WTForm filters implementation.

Filters can be applied to incoming form data, after process_formdata() has run.

See more information on: http://wtforms.simplecodes.com/docs/1.0.4/fields.html#wtforms.fields.Field

inspirehep.modules.forms.filter_utils.clean_empty_list(value)[source]

Created to clean a list produced by Bootstrap multi-select.

inspirehep.modules.forms.filter_utils.strip_prefixes(*prefixes)[source]

Return a filter function that removes leading prefixes from a string.

inspirehep.modules.forms.filter_utils.strip_string(value)[source]

Remove leading and trailing spaces from string.

inspirehep.modules.forms.form module

inspirehep.modules.forms.form.CFG_FIELD_FLAGS = ['hidden', 'disabled', 'touched']

List of WTForm field flags to be saved in draft.

inspirehep.modules.forms.form.CFG_GROUPS_META = {'classes': None, 'indication': None, 'description': None, 'icon': None}

Default group metadata.

class inspirehep.modules.forms.form.DataExporter(filter_func=None)[source]

Bases: inspirehep.modules.forms.form.FormVisitor

Visitor to export form data into dictionary supporting filtering and key renaming.

Usage::
form = ... visitor = DataExporter(filter_func=lambda f: not f.flags.disabled) visitor.visit(form)

Given e.g. the following form:

class MyForm(INSPIREForm):
    title = TextField(export_key='my_title')
    notes = TextAreaField()
    authors = FieldList(FormField(AuthorForm))

the visitor will export a dictionary similar to:

{'my_title': ..., 'notes': ..., authors: [{...}, ...], }
visit_field(field)[source]
visit_fieldlist(fieldlist)[source]
visit_formfield(formfield)[source]
class inspirehep.modules.forms.form.FormVisitor[source]

Bases: object

Generic form visitor to iterate over all fields in a form. See DataExporter for example how to export all data.

visit(form_or_field)[source]
visit_field(field)[source]
visit_fieldlist(fieldlist)[source]
visit_form(form)[source]
visit_formfield(formfield)[source]
class inspirehep.modules.forms.form.INSPIREForm(*args, **kwargs)[source]

Bases: wtforms.form.Form

Generic WebDeposit Form class.

get_groups()[source]

Get a list of the (group metadata, list of fields)-tuples. The last element of the list has no group metadata (i.e. None), and contains the list of fields not assigned to any group.

get_template()[source]

Get template to render this form. Define a data member template to customize which template to use. By default, it will render the template deposit/run.html

json_data

Return form data in a format suitable for the standard JSON encoder. Return form data in a format suitable for the standard JSON encoder, by calling Field.json_data() on each field if it exists, otherwise is uses the value of Field.data.

messages

Return a dictionary of form messages.

post_process(form=None, formfields=[], submit=False)[source]

Run form post-processing.

Run form post-processing by calling post_process on each field, passing any extra Form.post_process_<fieldname> processors to the field.

If formfields are specified, only the given fields’ processors will be run (which may touch all fields of the form). The post processing allows the form to alter other fields in the form, via e.g. contacting external services (e.g a DOI field could retrieve title, authors from CrossRef/DataCite).

inspirehep.modules.forms.utils module

Forms utilities.

inspirehep.modules.forms.utils.filter_empty_elements(recjson, list_fields)[source]

Filter empty fields.

inspirehep.modules.forms.utils.filter_empty_helper(keys=None)[source]

Remove empty elements from a list.

inspirehep.modules.forms.validation_utils module

Validation functions.

class inspirehep.modules.forms.validation_utils.DOISyntaxValidator(message=None)[source]

Bases: object

DOI syntax validator.

inspirehep.modules.forms.validation_utils.ORCIDValidator(form, field)[source]

Validate that the given ORCID exists.

class inspirehep.modules.forms.validation_utils.RegexpStopValidator(regex, flags=0, message=None)[source]

Bases: object

Validates the field against a user provided regexp.

Parameters:
  • regex – The regular expression string to use. Can also be a compiled regular expression pattern.
  • flags – The regexp flags to use, for example re.IGNORECASE. Ignored if regex is not a string.
  • message – Error message to raise in case of a validation error.

inspirehep.modules.forms.views module

Module contents

Forms module.