Changeset 6533 for main/waeup.sirp/trunk/src/waeup/sirp
- Timestamp:
- 23 Jul 2011, 02:36:45 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
main/waeup.sirp/trunk/src/waeup/sirp/image/browser/widget.py
r6035 r6533 22 22 """Image file widgets. 23 23 """ 24 import os 24 25 from waeup.sirp.image import WAeUPImageFile 25 26 from hurry.file.browser.widget import ( 26 FileWidgetBase, EncodingFileWidget, DownloadWidget,27 ) 27 EncodingFileWidget, DownloadWidget, FakeFieldStorage) 28 from hurry.file.interfaces import IFileRetrieval 28 29 from zope.app.form.browser.textwidgets import escape 29 30 from zope.app.form.browser.widget import renderElement 30 31 from zope.app.form.interfaces import ConversionError 32 from zope.component import queryUtility 33 from zope.publisher.browser import FileUpload 31 34 32 35 … … 41 44 42 45 def _toFieldValue(self, (input, file_id)): 43 # we got no file upload input46 # we got no file upload input 44 47 if not input: 45 48 # if we got a file_id, then retrieve file and return it … … 59 62 60 63 if data: 64 retrieval = queryUtility(IFileRetrieval) 65 if retrieval is not None: 66 seek(0) 67 return retrieval.createFile(input.filename, input) 61 68 return WAeUPImageFile(input.filename, data) 62 69 else: 63 70 return self.context.missing_value 64 71 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 65 79 def __call__(self): 66 # The base widget renders the actual image as filename. We67 # want the image itself displayed as well.68 80 value = self._getFormValue() 81 if value: 82 file_id = self._setFile(value) 83 else: 84 file_id = None 69 85 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) 70 95 if value: 71 96 filename = value.filename … … 77 102 ) 78 103 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 ) 81 115 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) 82 132 83 133 def _storeFile(self, file_upload): 84 134 # filenames are normally in unicode encoding, while the contents 85 135 # 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) 87 144 return data.encode('base64')[:-1] 88 145
Note: See TracChangeset for help on using the changeset viewer.