Changeset 6533 for main/waeup.sirp


Ignore:
Timestamp:
23 Jul 2011, 02:36:45 (13 years ago)
Author:
uli
Message:

Many fixes to make widgets for WAeUPImage files behave as we would
expect it.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/waeup.sirp/trunk/src/waeup/sirp/image/browser/widget.py

    r6035 r6533  
    2222"""Image file widgets.
    2323"""
     24import os
    2425from waeup.sirp.image import WAeUPImageFile
    2526from hurry.file.browser.widget import (
    26     FileWidgetBase, EncodingFileWidget, DownloadWidget,
    27     )
     27    EncodingFileWidget, DownloadWidget, FakeFieldStorage)
     28from hurry.file.interfaces import IFileRetrieval
    2829from zope.app.form.browser.textwidgets import escape
    2930from zope.app.form.browser.widget import renderElement
    3031from zope.app.form.interfaces import ConversionError
     32from zope.component import queryUtility
     33from zope.publisher.browser import FileUpload
    3134
    3235
     
    4144
    4245    def _toFieldValue(self, (input, file_id)):
    43        # we got no file upload input
     46        # we got no file upload input
    4447        if not input:
    4548            # if we got a file_id, then retrieve file and return it
     
    5962
    6063        if data:
     64            retrieval = queryUtility(IFileRetrieval)
     65            if retrieval is not None:
     66                seek(0)
     67                return retrieval.createFile(input.filename, input)
    6168            return WAeUPImageFile(input.filename, data)
    6269        else:
    6370            return self.context.missing_value
    6471
     72    def _toFormValue(self, value):
     73        if value == self.context.missing_value:
     74            return self._missing
     75        data = value.file.read()
     76        return FileUpload(FakeFieldStorage(
     77                value.filename.encode('UTF-8'), data))
     78
    6579    def __call__(self):
    66         # The base widget renders the actual image as filename. We
    67         # want the image itself displayed as well.
    6880        value = self._getFormValue()
     81        if value:
     82            file_id = self._setFile(value)
     83        else:
     84            file_id = None
    6985        result = u''
     86        options = dict(
     87            type=self.type,
     88            name=self.name,
     89            id=self.name,
     90            cssClass=self.cssClass,
     91            size=self.displayWidth,
     92            extra=self.extra,)
     93        if self.displayMaxWidth:
     94            options.update(maxlength=self.displayMaxWidth)
    7095        if value:
    7196            filename = value.filename
     
    77102                )
    78103            result += renderElement('br')
    79         # Append the rendering as delivered from the base widget.
    80         result += super(EncodingImageFileWidget, self).__call__()
     104        result += renderElement(self.tag, **options)
     105        if file_id is not None:
     106            if value:
     107                result += ' (%s)' % value.filename
     108            result += renderElement(
     109                'input',
     110                type='hidden',
     111                name=self.name + '.file_id',
     112                id=self.name + '.file_id',
     113                value=file_id,
     114                )
    81115        return result
     116
     117    def _setFile(self, file):
     118        """Store away uploaded file (FileUpload object).
     119
     120        Returns file_id identifying file.
     121        """
     122        # if there was no file input and there was a file_id already in the
     123        # input, reuse this for next request
     124        if not self.request.get(self.name):
     125            file_id = self.request.get(self.name + '.file_id')
     126            if file_id is not None:
     127                return file_id
     128        # otherwise, stuff filedata away in session, making a new file_id
     129        if file == self.context.missing_value:
     130            return None
     131        return self._storeFile(file)
    82132
    83133    def _storeFile(self, file_upload):
    84134        # filenames are normally in unicode encoding, while the contents
    85135        # are byte streams. We turn the filename into a bytestream.
    86         data = '%s\n%s' % (str(file_upload.filename), file_upload.read())
     136        retrieval = queryUtility(IFileRetrieval)
     137        if retrieval is not None:
     138            file_upload.seek(0)
     139            file_obj = retrieval.createFile(file_upload.filename, file_upload)
     140            data = file_obj.data
     141        else:
     142            data = file_upload.read()
     143        data = '%s\n%s' % (str(file_upload.filename), data)
    87144        return data.encode('base64')[:-1]
    88145
Note: See TracChangeset for help on using the changeset viewer.