[Zope3-Users] Custom Image Widget - Solution
Adam Summers
adam at 4js.com.au
Sun Dec 17 05:18:15 EST 2006
An HTML attachment was scrubbed...
URL: http://mail.zope.org/pipermail/zope3-users/attachments/20061217/5a73d1ce/attachment.htm
-------------- next part --------------
from zope.app.form import CustomWidgetFactory
from zope.app.form.browser.widget import DisplayWidget, SimpleInputWidget, renderElement
from zope.app.form.browser.sequencewidget import ListSequenceWidget, SequenceDisplayWidget
from zope.app.file.image import Image
from zope.app.form.interfaces import ConversionError
from base64 import b64encode, b64decode
class MyImageDisplayWidget(DisplayWidget):
def __call__(self):
mycontent = u"(No Image)"
if self._renderedValueSet() and self._data is not None:
mycontent = buildHTMLImg(self._data.data)
return mycontent
MyImageListDisplayWidget = CustomWidgetFactory(SequenceDisplayWidget, subwidget = MyImageDisplayWidget)
class MyImageInputWidget(SimpleInputWidget):
"""File Widget"""
type = 'file'
def _toFieldValue(self, input):
if (input is None or input == ''):
try:
imgData = b64decode(self.request.form[self.name + '.data'])
return Image(imgData)
except AttributeError, e:
return self.context.missing_value
else:
try:
seek = input.seek
read = input.read
except AttributeError, e:
raise ConversionError(_('Form input is not a file object'), e)
else:
seek(0)
data = read()
if data or getattr(input, 'filename', ''):
return Image(data)
else:
return self.context.missing_value
def __call__(self):
elem = renderElement(self.tag,
type=self.type,
name=self.name,
id=self.name,
extra=self.extra)
input_widget = elem
try:
img_widget = buildHTMLImg(self._data.data)
elem = renderElement(self.tag,
type='hidden',
name=self.name+'.data',
id=self.name,
extra='value="' + b64encode(self._data.data) + '"')
input_widget = img_widget + input_widget +elem
return input_widget
except AttributeError:
return input_widget
MyImageInputListWidget = CustomWidgetFactory(ListSequenceWidget, subwidget = MyImageInputWidget)
def buildHTMLImg(data):
return "<img src=\"data:image/gif;base64, " + b64encode(data) + " \" />"
-------------- next part --------------
from zope.formlib import form
from as.claim.interfaces import Iclaim
from as.claim.claim import claim
from as.widget.browser.widgets import MyImageListDisplayWidget, MyImageInputListWidget
def _(arg): #yes this is a hack. to be modified
return arg
class claimAddForm(form.AddForm):
form_fields = form.Fields(Iclaim).omit('__name__', '__parent__')
form_fields['supDoc'].custom_widget = MyImageInputListWidget
def create(self, data):
return claim(**data)
class claimEditForm(form.EditForm):
form_fields = form.Fields(Iclaim).omit('__name__', '__parent__')
form_fields['supDoc'].custom_widget = MyImageInputListWidget
#@form.action(_("Apply"), condition=form.haveInputWidgets)
#def handle_edit_action(self, action, data):
# if self.context.modify(data):
# self.status = _(u"Object Updated")
# else:
# self.status = _(u"No Changes")
class claimDisplayForm(form.DisplayForm):
form_fields = form.Fields(Iclaim).omit('__name__', '__parent__')
#form_fields['img'].custom_widget = MyImageDisplayWidget
form_fields['supDoc'].custom_widget = MyImageListDisplayWidget
More information about the Zope3-users
mailing list