[Zope3-Users] Custom Image Widget

Adam Groszer adamg at fw.hu
Thu Dec 14 10:58:56 EST 2006


Hi Adam,

I would try to figure out how the data gets extracted when it gets
displayed. Something like _getCurrentValue.

Or better, have a look at hurry.file. It uses a session variable or
hidden input to solve your problem.
And well, thinking further, hurry.file might be _the_ solution for you.

cheers,

Thursday, December 14, 2006, 4:47:51 PM, you wrote:

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

AS> I have the following widgets.


AS> class MyImageDisplayWidget(DisplayWidget):

AS>    class_ = Image

AS>    def __call__(self):
AS>        mycontent = u"oops! no img"
AS>        if self._renderedValueSet():
AS>            mycontent = "<img src=\"data:image/gif;base64, " +
AS> b64encode(self._data.data) + " \" />"
AS>        return mycontent

AS> MyImageListDisplayWidget = CustomWidgetFactory(SequenceDisplayWidget,
AS> subwidget = MyImageDisplayWidget)

AS> class MyImageWidget(FileWidget):

AS>    class_ = Image

AS>    def _toFieldValue(self, input):
AS>        value = super(MyImageWidget, self)._toFieldValue(input)
AS>        return self.class_(value)

AS> MyImageListWidget = CustomWidgetFactory(ListSequenceWidget, subwidget =
AS> MyImageWidget)

AS> I want to build a better input widget (MyImageWidget), so that we can do
AS> the following:
AS>    * If the field has no data, display a file input.
AS>    * If the field has data, display the image.

AS> I know that there are design shortcomings in this, but I need a simple
AS> example (and I only use the files in lists anyway, so I can delete images).

AS> Any pointers on how to go about this would be much appreciated; as always
AS>   
AS>   
AS> Hi Adam,

AS> I'm not exactly sure of your use case, but I've included a widget
AS> implementation (see below) which you may find useful.  I've used this
AS> widget for an attribute which is an IImage, e.g. 
AS>  
AS>  
AS>  <snip>
AS>  
AS>   
AS> class ImageInputWidget(FileWidget):

AS>     def _toFieldValue(self, input):
AS>         data = super(ImageWidget, self)._toFieldValue(input)
AS>         if data is not None:
AS>             img = Image(data)
AS>             notify(ObjectCreatedEvent(img))
AS>             return img

AS>     def __call__(self):
AS>         input_widget = super(ImageWidget, self).__call__()
AS>         try:
AS>             image = ImageDisplayWidget(self.context, self.request)()
AS>             info = self.info()
AS>             return u'<br />'.join([image, info, input_widget])
AS>         except AttributeError:
AS>             # This happens because the context is IAdding
AS>             return input_widge
AS>  
AS>  </snippet>
AS>  
AS>    


-- 
Best regards,
 Adam
--
Quote of the day:
Education helps earning capacity.  Ask any college professor.



More information about the Zope3-users mailing list