[Zope3-Users] Custom Image Widget

Tom Dossis td at yoma.com.au
Fri Dec 15 06:26:16 EST 2006


Adam Summers wrote:
> Hi Tom & Widget Afficionados.
> 
> Thanks for the help so far.
> 
> My problem is now this:
> 
>     From this code (which Tom supplied), how do I code the logic (in
> bold)      
> 
>     def _toFieldValue(self, input):
>         data = super(ImageWidget, self)._toFieldValue(input)
>         if data is not None:
>             img = Image(data)
>             notify(ObjectCreatedEvent(img))
>             return img
> 	*else: #data is None, ie. the File input field was left blank and we don't want to 
> 	    #replace the current value of the Image Widget with an empty value.
> 	    currentImg = the Image object which the field is being rendered for
> 	    return currentImg 
> 
> *
> 
> I can't rely on
> 
>     	field = self.context
> 
>             image = field.get(field.context)
> 
> 
> logic to find the data, because my schema can contain:
> 
> class Iclaim(IContained):
> 
> 	"""Claim"""
> 
> 	supDoc = List(title=_(u"Supporting Docs List"), value_type=Object(IImage, __name__='ImgItem', title=_(u"Image")))
> 
> 	img = Object(IImage, title=_(u"Single img"), required=False)
> 
> 
>     And hence, the self.context.context points to the claim object, not
> the list inside when rendering supDoc

Hi Adam,
You can rely on:

  field = self.context
  image = field.get(field.context)

because the widget is for an attribute object of type IImage.

Your supDoc attribute object is a List Type - not an IImage.
In this case you'd need another widget - for a list of IImage.

I wouldn't been too keen to tackle the html work effort and would
probably look at an alternative along the lines of...

class IImageList(IContainer):
  contains(zope.app.image.interfaces.IImage)

class IClaim(IContained):
  supDoc=Object(schema=IImageList)

Make supDoc traversable, then you wouldn't need the custom widget here
because you can store Images directly in supDoc view with the
browser:containerView's.



More information about the Zope3-users mailing list