<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
  <meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
Hi Tom &amp; Widget Afficionados.<br>
<br>
Thanks for the help so far.<br>
<br>
My problem is now this:<br>
<br>
&nbsp;&nbsp;&nbsp; From this code (which Tom supplied), how do I code the logic (in
bold) &nbsp;&nbsp; &nbsp;&nbsp; <br>
<pre wrap="">
    def _toFieldValue(self, input):
        data = super(ImageWidget, self)._toFieldValue(input)
        if data is not None:
            img = Image(data)
            notify(ObjectCreatedEvent(img))
            return img
        <b>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 

</b></pre>
I can't rely on <br>
<br>
<blockquote>
  <pre>        field = self.context</pre>
  <pre>        image = field.get(field.context)</pre>
</blockquote>
<br>
logic to find the data, because my schema can contain:<br>
<br>
<pre>class Iclaim(IContained):</pre>
<pre>        """Claim"""</pre>
<pre>        supDoc = List(title=_(u"Supporting Docs List"), value_type=Object(IImage, __name__='ImgItem', title=_(u"Image")))</pre>
<pre>        img = Object(IImage, title=_(u"Single img"), required=False)</pre>
<br>
&nbsp;&nbsp;&nbsp; And hence, the self.context.context points to the claim object, not
the list inside when rendering supDoc<br>
Again, any help is much appreciated.<br>
Regards,<br>
Adam<br>
&nbsp;<br>
Tom Dossis wrote:
<blockquote cite="mid457DEF84.9030209@yoma.com.au" type="cite">
  <pre wrap="">Adam Summers wrote:
  </pre>
  <blockquote type="cite">
    <pre wrap="">Hi,

I have the following widgets.


class MyImageDisplayWidget(DisplayWidget):

   class_ = Image

   def __call__(self):
       mycontent = u"oops! no img"
       if self._renderedValueSet():
           mycontent = "&lt;img src=\<a class="moz-txt-link-rfc2396E" href="data:image/gif;base64,">"data:image/gif;base64, "</a> +
b64encode(self._data.data) + " \" /&gt;"
       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 always
    </pre>
  </blockquote>
  <pre wrap=""><!---->

Hi 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.

  </pre>
</blockquote>
<br>
&lt;snip&gt;<br>
<blockquote cite="mid457DEF84.9030209@yoma.com.au" type="cite">
  <pre wrap="">
class ImageInputWidget(FileWidget):

    def _toFieldValue(self, input):
        data = super(ImageWidget, 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'&lt;br /&gt;'.join([image, info, input_widget])
        except AttributeError:
            # This happens because the context is IAdding
            return input_widge</pre>
</blockquote>
&lt;/snippet&gt;<br>
</body>
</html>