Changeset 6035 for main/waeup.sirp


Ignore:
Timestamp:
10 May 2011, 22:24:37 (14 years ago)
Author:
uli
Message:

Fix image widget: file setters/getters should return
WAeUPImageFile objects and not HurryFiles?.

File:
1 edited

Legend:

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

    r5693 r6035  
    2222"""Image file widgets.
    2323"""
    24 from hurry.file import HurryFile
     24from waeup.sirp.image import WAeUPImageFile
    2525from hurry.file.browser.widget import (
    2626    FileWidgetBase, EncodingFileWidget, DownloadWidget,
     
    2828from zope.app.form.browser.textwidgets import escape
    2929from zope.app.form.browser.widget import renderElement
     30from zope.app.form.interfaces import ConversionError
     31
    3032
    3133class EncodingImageFileWidget(EncodingFileWidget):
    3234    """A hurry.file widget that stores images.
     35
     36    This is an upload widget suitable for edit forms and the like.
    3337    """
    3438    #: We can set a basename for the image to be rendered.
    3539    #: This will result in <img> links like <img src='.../<basename>.jpg' />
    3640    image_basename = None
    37    
     41
     42    def _toFieldValue(self, (input, file_id)):
     43       # we got no file upload input
     44        if not input:
     45            # if we got a file_id, then retrieve file and return it
     46            if file_id:
     47                return self._retrieveFile(file_id)
     48            # no file upload input nor file id, so return missing value
     49            return self.context.missing_value
     50        # read in file from input
     51        try:
     52            seek = input.seek
     53            read = input.read
     54        except AttributeError, e:
     55            raise ConversionError('Form input is not a file object', e)
     56
     57        seek(0)
     58        data = read()
     59
     60        if data:
     61            return WAeUPImageFile(input.filename, data)
     62        else:
     63            return self.context.missing_value
     64
    3865    def __call__(self):
    3966        # The base widget renders the actual image as filename. We
     
    5279        # Append the rendering as delivered from the base widget.
    5380        result += super(EncodingImageFileWidget, self).__call__()
    54         #import pdb; pdb.set_trace()
    5581        return result
    5682
     
    6187        return data.encode('base64')[:-1]
    6288
     89    def _retrieveFile(self, file_id):
     90        data = file_id.decode('base64')
     91        filename, filedata = data.split('\n', 1)
     92        return WAeUPImageFile(filename, filedata)
     93
    6394class ThumbnailWidget(DownloadWidget):
    6495    """An image file widget that displays the data as thumbnail.
     96
     97    XXX: give some reason to name this a _thumbnail_ widget.
    6598    """
    6699
     
    79112            src=filename,
    80113            contents=None)
    81 
Note: See TracChangeset for help on using the changeset viewer.