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 = "" 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.contextimage = 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 Again, any help is much appreciated. Regards, Adam Tom Dossis wrote: Adam Summers wrote:Hi, I have the following widgets. class MyImageDisplayWidget(DisplayWidget): class_ = Image def __call__(self): mycontent = u"oops! no img" if self._renderedValueSet(): mycontent = "<img src="" class="moz-txt-link-rfc2396E" href="data:image/gif;base64,">"data:image/gif;base64, " + b64encode(self._data.data) + " \" />" return mycontent MyImageListDisplayWidget = CustomWidgetFactory(SequenceDisplayWidget, subwidget = MyImageDisplayWidget) class MyImageWidget(FileWidget): class_ = Image def _toFieldValue(self, input): value = super(MyImageWidget, self)._toFieldValue(input) return self.class_(value) MyImageListWidget = CustomWidgetFactory(ListSequenceWidget, subwidget = MyImageWidget) I want to build a better input widget (MyImageWidget), so that we can do the following: * If the field has no data, display a file input. * If the field has data, display the image. I know that there are design shortcomings in this, but I need a simple example (and I only use the files in lists anyway, so I can delete images). Any pointers on how to go about this would be much appreciated; as alwaysHi Adam, I'm not exactly sure of your use case, but I've included a widget implementation (see below) which you may find useful. I've used this widget for an attribute which is an IImage, e.g. <snip> </snippet>class ImageInputWidget(FileWidget): def _toFieldValue(self, input): data = "" self)._toFieldValue(input) if data is not None: img = Image(data) notify(ObjectCreatedEvent(img)) return img def __call__(self): input_widget = super(ImageWidget, self).__call__() try: image = ImageDisplayWidget(self.context, self.request)() info = self.info() return u'<br />'.join([image, info, input_widget]) except AttributeError: # This happens because the context is IAdding return input_widge |
_______________________________________________ Zope3-users mailing list Zope3-users@zope.org http://mail.zope.org/mailman/listinfo/zope3-users