← Back to team overview

schooltool-developers team mailing list archive

Re: Demographic field limit keys

 

On 01/06/2011 06:13 AM, Alan Elkner wrote:
Hey Guys,
<...>
Justus,

Currently, both the person and the resource demo field edit views use
the CustomEnumFieldTextWidget for the limit_keys field, a textarea
widget, but that makes the field free-from which is not what we want.
We need the widget to be something like the default list widget,
allowing the user to create a list of select drop-downs, one for each
limit_key.  In the case of person demos, the select field would
contain 'students', 'teachers', and 'administrators'.  In the case of
resource demo fields, the list would be 'resource', 'location', and
'equipment'.  Can you point me to code that would help me create the
custom widget that allows the user to add one or more limit_keys using
a different Choice field for the person demo field limit_keys from
that or the resource demo fields?  I tried seeing what happens when I
comment out the setting of the custom widget
(fields['limit_keys'].widgetFactory = ...), but that causes it to
raise a ComponentLookupError with name of u'' and context of None.
There are few ways to do this. One would be using a set for limit_keys instead of a list:

    foo = zope.schema.Set(
        title=_("Foo set"),
        value_type=Choice(
            title=_(u"Foo"),
            source="schooltool.something.group_vocabulary",
            required=False)
        )

This will give you a multi-select HTML widget that creates sets with desired keys. Vocabularies here are good - you can use them to dynamically collect the keys. For example, getUtilitiesFor(IResourceFactoryUtility) yields utilities with (translatable) "title" and "__name__" you want. And you can make a schooltool.app.utils.vocabulary out of schooltool.group.defaultGroups. Lots of goodness there :)


Alternatively - z3c.form also has CheckBoxWidget (not registered by default) that uses checkboxes instead of <select>. You could register it roughly like this:

@adapter(
    zope.schema.interfaces.IUnorderedCollection,
    zope.schema.interfaces.IChoice, z3c.form.interfaces.IFormLayer)
@implementer(z3c.form.interfacesIFieldWidget)
def ChoiceSelectFieldCheckboxWidget(field, value_type, request):
return FieldWidget(field, z3c.form.browser.checkbox.CheckBoxWidget(request))

A minor nuisance is that it's already registered for these interfaces, so you'll need a different layer, or just set the CheckBoxWidget as the custom widget.

Or maybe we can override unordered collection widgets for entire schooltool. Personally I like checboxes better than multi-select.

Justas.



References