flask_velox.mixins.forms

Mixin classes for handeling form rendering and processing.

Note

The following packages must be installed:

  • Flask-WTF
class flask_velox.mixins.forms.BaseFormMixin(*args, **kwargs)

Bases: flask_velox.mixins.context.ContextMixin, flask_velox.mixins.template.TemplateMixin

Base Form Mixin class, defines some standard methods required for both single and multi forms.

redirect_url_rule str

Raw Flask url rule, e.g: some.url.rule

submit_url_rule str, optional

Flask url rule for form submit action e.g: ‘some.url.rule’

flash()

Override this method to call a flask flash method. By default this method does nothing.

Example

class MyView(FromMixin):
    form = Form

    def flash(self):
        flash('Message', 'success')
get_redirect_url_rule()

Returns raw redirect url rule to be used in url_for. The redirect_url_rule must be defined else NotImplementedError will be raised.

Returns:Raw flask url endpoint
Return type:str
Raises:NotImplementedError – If redirect_url_rule is not defined
get_submit_url_rule()

Returns a submit url rule for usage in generating a submit form action url. Defaults to the views current url rule endpoint but can be overridden by defining submit_url_rule on the view class.abs

Returns:Raw flask url rule endpoint
Return type:str
instantiate_form(kls=None, obj=None, prefix='')

Instantiates form if instance does not already exisst. Override this method to tailor form instantiation.

Parameters:
  • kls (class, optional) – Form class to instantiate, defaults to None
  • obj (object, optional) – Object to pass into the form to pre populate with
  • prefix (str, optional) – Add a prefix to the form class
Returns:

Instantiated form

Return type:

object

post(*args, **kwargs)

Handle HTTP POST requets using Flask MethodView rendering a single html template.

Returns:Rendered template
Return type:str
redirect_url(**kwargs)

Returns the url to a redirect endpoint, when the form is valid and the callback is called.

Parameters:**kwargs – Arbitrary keyword arguments passed to Flask.url_for
Returns:Generated url
Return type:str or None
set_context()

Overrides set_context to set extra context variables.

See also

  • from flask_velox.mixins.context.ContextMixin.set_context()

Note

Adds the following extra context variables:

  • is_hidden_field: Function for determining is field is hidden
submit_url(**kwargs)

Returns the url to a submit endpoint, this is used to render a link in forms actions:

<form action="{{ submit_url() }}" method="POST">
...
</form>
Parameters:**kwargs – Arbitrary keyword arguments passed to Flask.url_for
Returns:Generated url
Return type:str or None
success_callback()

Called on successful form validation, by default this will perform a redirect if redirect_url_rule is defined. Override this method to perform any custom actions on successful form validation.

Returns:Redirects request to somewhere else
Return type:werkzeug.wrappers.Response
class flask_velox.mixins.forms.FormMixin(*args, **kwargs)

Bases: flask_velox.mixins.forms.BaseFormMixin

Renders and validates a single Form.

Example

1
2
3
4
5
from flask.velox.mixins.forms import FormMixin
from yourapp.forms import MyForm

class MyView(FormMixin):
    form_class = MyForm
form class

An uninstantiated WTForm class

get_form()

Returns an instantiated WTForm class.

Returns:Instantiated form
Return type:object
get_form_class()

Returns defined form_class or riases NotImplementedError

Returns:Uninstantiated WTForm class
Return type:class
Raises:NotImplementedError – If form_class is not defined
set_context()

Overrides set_context to set extra context variables.

See also

  • from flask_velox.mixins.context.ContextMixin.set_context()

Note

Adds the following extra context variables:

  • form: Instantiated form object
class flask_velox.mixins.forms.MultiFormMixin(*args, **kwargs)

Bases: flask_velox.mixins.forms.BaseFormMixin

Mixin allows rendering of multiple forms in a single template, each form is submit individually and validated individually but to the same endpoint.

Example

1
2
3
4
5
6
7
8
from flask.velox.mixins.forms import MultiFormMixin
from yourapp.forms import Form1, Form2

class MyView(MultiFormMixin):
    forms = [
        ('Form 1', Form1)
        ('Form 2', Form2)
    ]
get_form()

Get the submit form if one exists else return None, this allows us to get the correctly submit form to validate against and populate objects if required.

A hidden field must be included in each <form> block named form containing the value of the forms prefix. This is the forms uniqie identifier and is used to obtain the submit form.

Returns:Submit form object or None if submit form not found
Return type:obj or None
get_form_classes()

Return list of tuples of form classes, the tuple should contain a human readable name and a form class defined in forms attribute. If not set NotImplementedError is risen.

Returns:Tuples containing human name and form class
Return type:list
Raises:NotImplementedErrorforms attribute is not defined
get_forms()

Instantiates forms set in forms attribute giving each form an individual prefix and storing each form in a dict using its prefix as the key.

Returns:Instantiated forms with form prefix as key and a tuple containing the human readable form name and form object:
{
    'form1': ('Foo Form', <object>),
    'form1': ('Bar Form', <object>)
}
Return type:collections.OrderedDict
is_submit(form, prefix)

If the form has been submit run the validate on submit method and call the success callback if the form is valid.

Parameters:
  • form (object) – Instantiated form object
  • prefix (str) – Form prefix id
Returns:

If the form was submit§

Return type:

bool

set_context()

Updates context to contain extra variables.

See also

  • from flask_velox.mixins.context.ContextMixin.set_context()

Note

Adds the following extra context variables:

  • forms: List of forms

http://thisissoon.com

Related Topics

This Page