[Zope-CMF] [dev] using zope.formlib - a proposal

yuppie y.2006_ at wcm-solutions.de
Thu Nov 2 03:44:46 EST 2006


Hi!


CMF 2.0 ships with some view based forms. They use their own framework 
and are explicitly labeled as experimental.

CMF 2.1 depends on Zope 2.10, so zope.formlib is now available. I 
propose to replace the view based forms using zope.formlib.


This is the approach I propose:


The interfaces of classic CMF content objects are not suitable for 
zope.formlib:

- Text fields are encoded strings while zope.formlib expects unicode.

- Datetime fields are Zope DateTime objects while zope.formlib expects 
generic datetime objects.

- Tuples or lists are used for unordered collections while zope.formlib 
expects Set fields.

- Different setter and getter methods are used while zope.formlib 
expects properties.

I propose to add suitable schemas in the browser packages and to use 
adapters to map the form schemas to the existing content interfaces.

I have a ProxyFieldProperty class (similar to zope.schema's 
FieldProperty class) that does the necessary mapping. This is how it is 
used for a CMFCalendar Event::


   class EventSchemaAdapter(SchemaAdapterBase):

       """Adapter for IMutableEvent.
       """

       adapts(IMutableEvent)
       implements(IEventSchema)

       title = ProxyFieldProperty(IEventSchema['title'],
                                  'Title', 'setTitle')
       contact_name = ProxyFieldProperty(IEventSchema['contact_name'])
       location = ProxyFieldProperty(IEventSchema['location'])
       contact_email = ProxyFieldProperty(IEventSchema['contact_email'])
       categories = ProxyFieldProperty(IEventSchema['categories'],
                                       'Subject', 'setSubject')
       contact_phone = ProxyFieldProperty(IEventSchema['contact_phone'])
       event_url = ProxyFieldProperty(IEventSchema['event_url'])
       start_date = ProxyFieldProperty(IEventSchema['start_date'],
                                       'start', 'setStartDate')
       stop_date = ProxyFieldProperty(IEventSchema['stop_date'],
                                      'end', 'setEndDate')
       description = ProxyFieldProperty(IEventSchema['description'],
                                        'Description', 'setDescription')

Maybe my ProxyFieldProperty implementation does too much magic at the 
moment. I expect it has to evolve over time, adding a way to control the 
mapping better.


If we have that adapter in place we can use zope.formlib in the generic 
way::

   class EventEditView(ContentEditFormBase):

       """Edit view for IMutableEvent.
       """

       form_fields = form.FormFields(IEventSchema)


Some CMF specific base classes are needed for this. I propose to add 
them in CMFDefault.formlib.

I have working code for IMutableEvent, IMutableFavorite and 
IMutableLink. I can't promise to convert the other forms.


Any feedback is welcome.


Cheers,

	Yuppie



More information about the Zope-CMF mailing list