[Zope-dev] z3c.form pass context to multi widgets in MultiConverter

Gerhard Weis gweis at gmx.at
Wed Jul 8 08:20:33 EDT 2009


Hi Roger,

Roger Ineichen wrote:
> Hi Gerhard
>
>   
>> Betreff: [Zope-dev] z3c.form pass context to multi widgets in 
>> MultiConverter
>>
>> Hi,
>>
>> some widgets may require a valid context to work properly. 
>> one such widget is z3c.formquery.
>>
>> The attached patch passes the current widget's context to the 
>> sub widgets during data conversion in 
>> z3c.form.converter.MultiConverter.
>> The patch handles this the same way as passing the form 
>> property is implemented. (The patch is against current trunk).
>>
>> Sorry, but I was not sure how to add a test for this. 
>> (However, there is also no explicit test for passing the form 
>> property).
>>
>> I ran the current test suite with python2.4 and it did not 
>> cause any additional issues :) .
>>
>> The second patch here was required on my system. It seems, 
>> that zope.app.component is a missing dependency for z3c.form. 
>> (at least for now).
>>
>> cheers,
>>
>> Gerhard
>>
>> --------------------------------------------------------------
>>
>> Index: src/z3c/form/converter.py
>> ===================================================================
>> --- src/z3c/form/converter.py	(revision 101721)
>> +++ src/z3c/form/converter.py	(working copy)
>> @@ -359,6 +359,10 @@
>>              #form property required by objecwidget
>>              widget.form = self.widget.form
>>              zope.interface.alsoProvides(widget, 
>> interfaces.IFormAware)
>> +        if interfaces.IContextAware.providedBy(self.widget):
>> +            # context property may be required by objectwidget
>> +            widget.context = self.widget.context
>> +            zope.interface.alsoProvides(widget, 
>> + interface.IContextAware)
>>          converter = zope.component.getMultiAdapter((field, widget),
>>              interfaces.IDataConverter)
>>  
>> @@ -377,6 +381,10 @@
>>              #form property required by objecwidget
>>              widget.form = self.widget.form
>>              zope.interface.alsoProvides(widget, 
>> interfaces.IFormAware)
>> +        if interfaces.IContextAware.providedBy(self.widget):
>> +            # context property may be required by objectwidget
>> +            widget.context = self.widget.context
>> +            zope.interface.alsoProvides(widget, 
>> + interface.IContextAware)
>>          converter = zope.component.getMultiAdapter((field, widget),
>>              interfaces.IDataConverter)
>>     
>
> Much thanks for your improvments!
>
> There is a form property called ignoreContext. If this property
> is set, the context is ignored explicit. You can set the ignoreContext
> property on selected widgets or in a form.
>
> I'm not sure but it looks to me that the ignoreContext property should
> also be a part of this patch.
>
> My question,
> The following line in the code will set/dispatch the context:
> "widget.context = self.widget.context"
>
> Are I'm right, the context of the self.widget.context could be an
> ignored context but the widget.context should not ba an ignored
> context?
>
> Is this a valid usecase? At least I think this was also not
> supported without your patch.
>
> If this makes sense, the patch should probably do soemthing like:
>
> if not widget.ignoreContext
>     widget.context = widget.form.context
>
> This whould prevent to set an ignored context from
> self.widget.context to the widget.context.
>
>
> I'm really not sure if this is working, but does this make sense to you?
>
> Regards
> Roger Ineichen
>
>
>
>   
Thanks for the comment. For this particular use case it won't work 
without the context,. The context is used to look up further objects in 
the containment hierarchy. At this particular part in the code it is 
just about converting from/to widget/field value. So 
ignoreContext/ignoreRequest are not used anyway. (at least not to 
retrieve widget values).

However, maybe it would be better to pass ignoreContex and ignoreRequest 
to the sub-widget, and let the sub-widget decide whether to ignore the 
context/request or not. This could especially be useful in other parts 
of the muti-widget code.

Regards,
Gerhard


More information about the Zope-Dev mailing list