The pyck.forms Package

The PyCK Forms Package

class pyck.forms.Form(formdata=None, obj=None, prefix='', request_obj=None, use_csrf_protection=False, **kwargs)

The Form base class, extends from WTForms.Form

This class provides some additional features to the base wtforms.Form class. These include:

  • Methods to render the form in HTML
    • as_p to render form using p tags
    • as_table to render using html table
  • CSRF protection availability

  • TODO: Since WTForms wants presentation attributes (like rows and cols for textarea) to be set on

instantiated form instances (not the class itself), allow some way to specify field attributes that are then read at field display time. Possibly a field_attrs dict??? May be reimplementing sub-classes of some fields to make use of it would be a good idea??

as_div()

Output each form field as html p tags. By default labels are displayed on top of the form fields and validation erros are displayed on the right of the form fields. Both these behaviors can be changed by settings values for the labels and errors parameters.

Values can be left, top, right or bottom

Parameters:
  • labels – Placement of labels relative to the field
  • errors – Placement of validation errors (if any) relative to the field
as_p(labels='top', errors='right')

Output each form field as html p tags. By default labels are displayed on top of the form fields and validation erros are displayed on the right of the form fields. Both these behaviors can be changed by settings values for the labels and errors parameters.

Values can be left, top, right or bottom

Parameters:
  • labels – Placement of labels relative to the field
  • errors – Placement of validation errors (if any) relative to the field
as_table(labels='left', errors='top', include_table_tag=False)

Output the form as HTML Table, optionally add the table tags too if include_table_tag is set to True (default False)

Parameters:
  • labels – Placement of labels relative to the field
  • errors – Placement of validation errors (if any) relative to the field
  • include_table_tag – Whether to include the html <table> and </table> tags in the output or not
validate()

Validate form fields and check for CSRF token match if use_csrf_protection was set to true when initializing the form.

pyck.forms.model_form(model, db_session=None, base_class=<class 'pyck.forms.form.Form'>, only=None, exclude=None, field_args=None, converter=None, exclude_pk=True, exclude_fk=True, type_name=None)

A Wrapper around wtforms.ext.sqlalchemy.orm.model_form() function to facilitate creating model forms using a wtforms compatible model_form call but using pyck.forms.Form Create a wtforms Form for a given SQLAlchemy model class:

from pyck.forms import model_form
from myapp.models import User
UserForm = model_form(User)
Parameters:
  • model – A SQLAlchemy mapped model class.
  • base_class – Base form class to extend from. Must be a wtforms.Form subclass.
  • only – An optional iterable with the property names that should be included in the form. Only these properties will have fields.
  • exclude – An optional iterable with the property names that should be excluded from the form. All other properties will have fields.
  • field_args – An optional dictionary of field names mapping to keyword arguments used to construct each field object.
  • converter – A converter to generate the fields based on the model properties. If not set, ModelConverter is used.
pyck.forms.dojo_model_form(model, db_session=None, base_class=<class 'pyck.forms.form.Form'>, only=None, exclude=None, field_args=None, converter=None, exclude_pk=False, exclude_fk=False, type_name=None)

A Wrapper around wtforms.ext.sqlalchemy.orm.model_form() function to facilitate creating model forms using a wtforms compatible model_form call but using pyck.forms.Form and :module:`WTDojo` form components

Create a wtforms Form for a given SQLAlchemy model class:

from pyck.forms import dojo_model_form
from myapp.models import User
UserForm = dojo_model_form(User)
Parameters:
  • model – A SQLAlchemy mapped model class.
  • base_class – Base form class to extend from. Must be a wtforms.Form subclass.
  • only – An optional iterable with the property names that should be included in the form. Only these properties will have fields.
  • exclude – An optional iterable with the property names that should be excluded from the form. All other properties will have fields.
  • field_args – An optional dictionary of field names mapping to keyword arguments used to construct each field object.
  • converter – A converter to generate the fields based on the model properties. If not set, ModelConverter is used.
  • exclude_pk – An optional boolean to force primary key exclusion.
  • exclude_fk – An optional boolean to force foreign keys exclusion.
  • type_name – An optional string to set returned type name.

Here is an example usage to demonstrate the improvements:

from pyck.forms import Form
from wtforms import TextField, validators
class MyForm(Form):
    name = TextField("Name", [validators.required()])
    fname = TextField("Father's Name", [validators.required()])

myform = MyForm()
str(myform.as_p())
r'\n<p>\n<label for="name">Name</label><br /> <input id="name" name="name" type="text" value="" /> </p>\n\n<p>\n<label for="fname">Father\'s Name</label><br /> <input id="fname" name="fname" type="text" value="" /> </p>\n'
myform.validate()
False
str(myform.as_p(labels='left', errors='right'))
'\n<p>\n<label for="name">Name</label> <input id="name" name="name" type="text" value="" /> <span class="errors">This field is required.</span> </p>\n\n<p>\n<label for="fname">Father\'s Name</label> <input id="fname" name="fname" type="text" value="" /> <span class="errors">This field is required.</span> </p>\n'

Within a template, all you need to do is to just call the form’s rendering function, for example:

${myform.as_p() | n}