bag.web.pyramid.deform_view module

Abstract base classes for creating deform views in Pyramid.

class bag.web.pyramid.deform_view.BaseDeformView(context, request)[source]

Bases: object

An abstract base class (ABC) for Pyramid views that use deform.

The workflow is divided into several methods so you can change details easily in subclasses.

Subclasses must provide at least:

  • a static schema. (This should subclass deform’s CSRFSchema.)

  • one or more view methods that use the methods in this ABC, especially _deform_workflow() or _colander_workflow().

Example usage:

from deform.schema import CSRFSchema
from bag.web.pyramid.deform_view import BaseDeformView

class InvitationView(BaseDeformView):
    formid = 'invitation_form'
    button_text = _("Send invitations to the above users")
    button_icon = 'icon-envelope icon-white'
    schema = MyInvitationSchema  # a CSRFSchema subclass

    @view_config(name='invite-users',
                 renderer='myapp:templates/invite-users.genshi')
    def invite_users(self):
        return self._deform_workflow()

    def _valid(self, form, controls):
        '''The form validates, so now we send out the invitations,
        set up a flash message and redirect to the home page.
        '''
        (...)

If you want to do something with the POSTed data before validation, just implement this method in your subclass:

def _preprocess_controls(self, controls):
bootstrap_form_style = 'form-horizontal'
button_icon: Optional[str] = None
button_text = 'Submit'
formid = 'form'
schema_instance

Give subclasses a chance to mutate the schema.

Example:

@reify
def schema_instance(self):
    return self.schema().bind(now=datetime.utcnow())

The default implementation binds the request for CSRF protection and, if self.schema_validator is defined, uses it for the form as a whole.

schema_validator = None
use_ajax = False
bag.web.pyramid.deform_view.button(title='Submit', name=None, icon=None)[source]

Conveniently generate a Deform button while setting its name attribute, translating the label and capitalizing it. The button may also have a bootstrap icon.

bag.web.pyramid.deform_view.translator(term)[source]