flask_velox.views.forms

Module provides views for basic form rendering and processing.

Note

The following packages must be installed:

  • Flask-WTF
class flask_velox.views.forms.FormView(*args, **kwargs)

Bases: flask_velox.mixins.forms.FormMixin

Class implements flask_velox.mixins.forms.FormMixin allowing rendering of Flask-WTF forms.

Note

Context pased to template:

  • form: The instantiated form class
  • submit_url_rule: Raw flask url rule
  • submit_url: Function to call to generate the submit url for the form

Example

from flask.ext.velox.views.forms import FormView
from yourapp.forms import MyForm

class MyView(FormView):
    template = 'form.html'
    form = MyForm
__delattr__

x.__delattr__(‘name’) <==> del x.name

__format__()

default object formatter

__getattribute__

x.__getattribute__(‘name’) <==> x.name

__hash__

x.__hash__() <==> hash(x)

__reduce__()

helper for pickle

__reduce_ex__()

helper for pickle

__repr__

x.__repr__() <==> repr(x)

__setattr__

x.__setattr__(‘name’, value) <==> x.name = value

__sizeof__() → int

size of object in memory, in bytes

__str__

x.__str__() <==> str(x)

add_context(key, val)

Adds a new element to the context.

Parameters:
  • key (str) – The context key name
  • val – The value of the new context item of any type
Returns:

The new context

Return type:

dict

classmethod as_view(name, *class_args, **class_kwargs)

Converts the class into an actual view function that can be used with the routing system. Internally this generates a function on the fly which will instantiate the View on each request and call the dispatch_request() method on it.

The arguments passed to as_view() are forwarded to the constructor of the class.

del_context(key)

Removes an element from the context dictionary.

Example

>>> class FooView(ContextMixin):
...     context = {
...         'foo': 'bar',
...     }
...
>>> view = FooView()
>>> view.del_context('foo')
{}
Parameters:key (str) – The context key name
Returns:Success or Failure
Return type:bool
Returns:The new context
Return type:dict
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(*args, **kwargs)

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

Returns:Rendered template
Return type:str
get_context()

Propety method which returns the current context.

Returns:Current context value
Return type:dict
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
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

merge_context(subject)

Merge the passed dictionary into the current _context

Parameters:subject (dict) – The dict to merge into _context

Example

>>> class FooView(ContextMixin):
...     context = {
...         'foo': 'bar',
...     }
...
>>> view = FooView()
>>> view.merge_context({'hello': 'world'})
>>> view.get_context()
{
    'hello': 'world',
    'foo': 'bar'
}
Returns:The new context
Return type:dict
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
render()

Renders a template. This method will attempt to pass context to the template but if the context attribute does not exist then an empty dict will be passed to the render_template method.

Returns:Rendered template
Return type:str
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
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
update_context(new)

Overwrites the existing context with the provided new context value.

Example

>>> class FooView(ContextMixin):
...     context = {
...         'foo': 'bar',
...     }
...
>>> view = FooView()
>>> view.update_context({'hello': 'world'})
{
    'hello': 'world'
}
Parameters:new (dict) – Value to update context too
Returns:The new context
Return type:dict
class flask_velox.views.forms.MultiFormView(*args, **kwargs)

Bases: flask_velox.mixins.forms.MultiFormMixin

Class implements flask_velox.mixins.forms.MultiFormMixin allowing rendering of multiple Flask-WTF forms.

Note

Context pased to template:

  • forms: The instantiated form classes
  • submit_url_rule: Raw flask url rule
  • submit_url: Function to call to generate the submit url for the form

Example

from flask.ext.velox.views.forms import MultiFormView
from yourapp.forms import FooForm, BarForm

class MyView(MultiFormView):
    template = 'forms.html'
    forms = [FooForm, BarForm]
__delattr__

x.__delattr__(‘name’) <==> del x.name

__format__()

default object formatter

__getattribute__

x.__getattribute__(‘name’) <==> x.name

__hash__

x.__hash__() <==> hash(x)

__reduce__()

helper for pickle

__reduce_ex__()

helper for pickle

__repr__

x.__repr__() <==> repr(x)

__setattr__

x.__setattr__(‘name’, value) <==> x.name = value

__sizeof__() → int

size of object in memory, in bytes

__str__

x.__str__() <==> str(x)

add_context(key, val)

Adds a new element to the context.

Parameters:
  • key (str) – The context key name
  • val – The value of the new context item of any type
Returns:

The new context

Return type:

dict

classmethod as_view(name, *class_args, **class_kwargs)

Converts the class into an actual view function that can be used with the routing system. Internally this generates a function on the fly which will instantiate the View on each request and call the dispatch_request() method on it.

The arguments passed to as_view() are forwarded to the constructor of the class.

del_context(key)

Removes an element from the context dictionary.

Example

>>> class FooView(ContextMixin):
...     context = {
...         'foo': 'bar',
...     }
...
>>> view = FooView()
>>> view.del_context('foo')
{}
Parameters:key (str) – The context key name
Returns:Success or Failure
Return type:bool
Returns:The new context
Return type:dict
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(*args, **kwargs)

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

Returns:Rendered template
Return type:str
get_context()

Propety method which returns the current context.

Returns:Current context value
Return type:dict
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
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

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

merge_context(subject)

Merge the passed dictionary into the current _context

Parameters:subject (dict) – The dict to merge into _context

Example

>>> class FooView(ContextMixin):
...     context = {
...         'foo': 'bar',
...     }
...
>>> view = FooView()
>>> view.merge_context({'hello': 'world'})
>>> view.get_context()
{
    'hello': 'world',
    'foo': 'bar'
}
Returns:The new context
Return type:dict
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
render()

Renders a template. This method will attempt to pass context to the template but if the context attribute does not exist then an empty dict will be passed to the render_template method.

Returns:Rendered template
Return type:str
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
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
update_context(new)

Overwrites the existing context with the provided new context value.

Example

>>> class FooView(ContextMixin):
...     context = {
...         'foo': 'bar',
...     }
...
>>> view = FooView()
>>> view.update_context({'hello': 'world'})
{
    'hello': 'world'
}
Parameters:new (dict) – Value to update context too
Returns:The new context
Return type:dict

http://thisissoon.com

Related Topics

This Page