I'm interested in hearing people's approaches to creating form widgets
and feedback on the approach I've been taking.  I've used the
traditional approach rather than the declarative approach because I
often have more logic than would fit into a single declaration and
because Kevin has stated he prefers the traditional approach.  I also
wrap my form creation up in a function to keep the namespace clean and
make form creation callable since many of my forms are populated with
live data.  Here is an example:

def createReviewerForm(controller=None):
    criteria = Permission.q.permissionId.endswith('reviewer')
    reviewer_permissions = Permission.select(criteria)
    criteria = Permission.q.permissionId.endswith('writer')
    writer_permissions = Permission.select(criteria)
    def usersForPermissions(permissions):
        options = [(None, '')]
        for permission in permissions:
            users = permission.all_users
            for user in users:
                user_tuple = (user.id, user.name or '')
                if user_tuple not in users:
                    options.append(user_tuple)
        return options
    reviewers = usersForPermissions(reviewer_permissions)
    writers = usersForPermissions(writer_permissions)
    permissions = [(None, '')]
    permissions.extend([(x.id, x.permissionId) for x in
reviewer_permissions])
    reviewer_wgt = DataSelectField(name="reviewer", options=reviewers)
    writer_wgt = DataSelectField(name="writer", options=writers)
    permission_wgt = DataSelectField(name="permission",
options=permissions)
    reviewer_wgt.validator = validators.NotEmpty()
    writer_wgt.validator = validators.NotEmpty()
    permission_wgt.validator = validators.NotEmpty()
    form_widgets = [reviewer_wgt, writer_wgt, permission_wgt]
    reviewer_form = WSTableForm(form_widgets, submit_text="Save")
    return reviewer_form

Some notes.  WSTableForm is a subclass of TableForm.  Permission is an
SQLObject subclass.  I don't know what the controller argument in the
function is.  I just know I had to put it in there to work.  Any
feedback or suggestions is welcome.

Randall

Reply via email to