- Timestamp:
- 23 Jul 2011, 02:40:55 (13 years ago)
- Location:
- main/waeup.sirp/trunk/src/waeup/sirp/image/browser/tests
- Files:
-
- 1 added
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
main/waeup.sirp/trunk/src/waeup/sirp/image/browser/tests/image.txt
r6311 r6537 9 9 10 10 In order to do this, we have a special way to store file data along with 11 its filename: :11 its filename: 12 12 13 13 >>> import os 14 14 >>> from waeup.sirp.image import WAeUPImageFile 15 15 >>> testimage = os.path.join(os.path.dirname(__file__), 'sample.jpg') 16 >>> testimage2 = os.path.join(os.path.dirname(__file__), 'sample2.jpg') 16 17 >>> some_file = WAeUPImageFile('foo.jpg', open(testimage, 'rb').read()) 17 18 >>> some_file.filename … … 21 22 22 23 We can provide a download widget. In this case, there's nothing 23 to download: :24 to download: 24 25 25 26 >>> from hurry.file.browser import DownloadWidget … … 34 35 u'<div>Download not available</div>' 35 36 36 Even if there were data in the request, there'd be nothing to download: :37 Even if there were data in the request, there'd be nothing to download: 37 38 38 39 >>> from zope.publisher.browser import FileUpload … … 42 43 u'<div>Download not available</div>' 43 44 44 Now set a value: :45 Now set a value: 45 46 46 47 >>> widget.setRenderedValue(some_file) … … 58 59 59 60 Now on to an edit widget. First the case in an add form with no 60 data already available, and no data in request:: 61 62 >>> #from hurry.file.browser import EncodingFileWidget 61 data already available, and no data in request: 62 63 63 >>> from waeup.sirp.image.browser import EncodingImageFileWidget 64 64 >>> field = ImageFile(__name__='foo', title=u'Foo', required=False) … … 80 80 81 81 Now let's try a situation where data is available in the request, but 82 it's an empty string for the file: :82 it's an empty string for the file: 83 83 84 84 >>> request = TestRequest(form={'field.foo': u''}) … … 98 98 99 99 Now let's render again when there's already available data. What should show 100 up is an extra, hidden field which contains the file_id: :100 up is an extra, hidden field which contains the file_id: 101 101 102 102 >>> widget.setRenderedValue(some_file) … … 119 119 120 120 Now let's render again, this time with file data available in the request 121 instead. The same should happen: :121 instead. The same should happen: 122 122 123 123 >>> request = TestRequest(form={'field.foo': FileUpload(some_file)}) … … 141 141 142 142 Now let's render again, this time not with file data available in the 143 request, but an id. Again, we should see the same: :143 request, but an id. Again, we should see the same: 144 144 145 145 >>> request = TestRequest(form={'field.foo.file_id': … … 164 164 165 165 If there is both file data and an id, something else happens. First, let's 166 prepare some new file: :166 prepare some new file: 167 167 168 168 >>> another_file = WAeUPImageFile('bar.txt', 'bar contents') … … 171 171 EncodingImageFileWidget, that the file_id is going to be 172 172 "Zm9vLnR4dAp0aGUgY29udGVudHM=". Let's make a request with the original 173 id, but a new file upload: :173 id, but a new file upload: 174 174 175 175 >>> request = TestRequest(form={'field.foo': FileUpload(another_file), … … 177 177 ... 'Zm9vLnR4dAp0aGUgY29udGVudHM='}) 178 178 179 We expect the new file to be the one that's uploaded: :179 We expect the new file to be the one that's uploaded: 180 180 181 181 >>> widget = EncodingImageFileWidget(field, request) … … 197 197 /> 198 198 199 Support for File Retrievals 200 --------------------------- 201 202 As :class:`waeup.sirp.image.WAeUPImageFile` objects support storing 203 image data by using external 'storages', also our widgets should do 204 so. 205 206 We create a simple IFileRetrieval utility and enable it: 207 208 >>> import hashlib 209 >>> from zope.component import provideUtility 210 >>> from hurry.file.interfaces import IFileRetrieval 211 >>> from StringIO import StringIO 212 >>> class MyFileRetrieval(object): 213 ... storage = dict() 214 ... def getFile(self, data): 215 ... entry = self.storage.get(data, None) 216 ... if entry is None: 217 ... return None 218 ... return StringIO(entry) 219 ... def createFile(self, filename, f): 220 ... contents = f.read() 221 ... id_string = hashlib.md5(contents).hexdigest() 222 ... result = WAeUPImageFile(filename, id_string) 223 ... self.storage[id_string] = contents 224 ... return result 225 226 >>> retrieval = MyFileRetrieval() 227 >>> provideUtility(retrieval, IFileRetrieval) 228 229 With this utility in place we can post requests. When no data was 230 posted and the field also contains no data, we will get a simple 231 input as appropriate for add forms: 232 233 >>> field = ImageFile(__name__='foo', title=u'Foo', required=False) 234 >>> field = field.bind(None) 235 >>> request = TestRequest() 236 >>> widget = EncodingImageFileWidget(field, request) 237 >>> print normalize(widget()) 238 <input 239 class="fileType" 240 id="field.foo" 241 name="field.foo" 242 size="20" 243 type="file" 244 /> 245 246 If the request contains empty data but it is only an empty string, the 247 result will be the same: 248 249 >>> request = TestRequest(form={'field.foo': u''}) 250 >>> widget = EncodingImageFileWidget(field, request) 251 >>> print normalize(widget()) 252 <input 253 class="fileType" 254 id="field.foo" 255 name="field.foo" 256 size="20" 257 type="file" 258 /> 259 260 We now want to simulate, that the field contains already data, 261 identified by some `file_id`. To do so, we first store the data in our 262 file retrieval and then create a WAeUPImageFile object with that 263 file_id stored: 264 265 >>> from waeup.sirp.image import createWAeUPImageFile 266 >>> image = createWAeUPImageFile( 267 ... 'sample.jpg', open(testimage, 'rb')) 268 >>> file_id = image.data 269 >>> file_id # MD5 sum of the file contents 270 '9feac4265077922000aa8b88748e25be' 271 272 >>> import hashlib 273 >>> hashlib.md5(open(testimage, 'rb').read()).hexdigest() 274 '9feac4265077922000aa8b88748e25be' 275 276 The new file was stored by our utility, as createWAeUPImageFile looks 277 up IFileRetrieval utilities and uses them: 278 279 >>> retrieval.storage.keys() 280 ['9feac4265077922000aa8b88748e25be'] 281 282 We set this image as value of the widget: 283 284 >>> widget.setRenderedValue(image) 285 >>> print normalize(widget()) 286 <img 287 src="sample.jpg" 288 /><br 289 /><input 290 class="fileType" 291 id="field.foo" 292 name="field.foo" 293 size="20" 294 type="file" 295 /> 296 (sample.jpg)<input 297 class="hiddenType" 298 id="field.foo.file_id" 299 name="field.foo.file_id" 300 type="hidden" 301 value="c2FtcGxlLmpwZwo5ZmVhYzQyNjUwNzc5MjIwMDBhYThiODg3NDhlMjViZQ==" 302 /> 303 304 >>> retrieval.storage.keys() 305 ['9feac4265077922000aa8b88748e25be'] 306 307 The stored hidden value contains the filename and our file_id: 308 309 >>> "c2FtcGxlLmpwZwo5ZmVhYzQyNjUwNzc5MjIwMDBhYThiODg3NDhlMjViZQ==".decode( 310 ... 'base64') 311 'sample.jpg\n9feac4265077922000aa8b88748e25be' 312 313 Now, we want the the widget rendered again but this time with the data 314 coming from the request. To do so, we have to create a FileUpload 315 object that wraps the real file: 316 317 >>> class UploadedFile(object): 318 ... headers = {} 319 ... def __init__(self, filename, f): 320 ... self.filename = filename 321 ... self.file = f 322 323 Now we can 'post' the request and will get the same result as above: 324 325 >>> upload = FileUpload(UploadedFile('sample.jpg', open(testimage, 'rb'))) 326 >>> request = TestRequest(form={'field.foo': upload}) 327 >>> widget = EncodingImageFileWidget(field, request) 328 >>> print normalize(widget()) 329 <img... />... 330 ...<input 331 class="fileType" 332 id="field.foo" 333 name="field.foo" 334 size="20" 335 type="file" 336 /> 337 (sample.jpg)<input 338 class="hiddenType" 339 id="field.foo.file_id" 340 name="field.foo.file_id" 341 type="hidden" 342 value="c2FtcGxlLmpwZwo5ZmVhYzQyNjUwNzc5MjIwMDBhYThiODg3NDhlMjViZQ==" 343 /> 344 345 Now let's render again, this time not with file data available in the 346 request, but an id. Again, we should see the same: 347 348 >>> request = TestRequest(form={ 349 ... 'field.foo.file_id': 350 ... 'c2FtcGxlLmpwZwo5ZmVhYzQyNjUwNzc5MjIwMDBhYThiODg3NDhlMjViZQ=='}) 351 >>> widget = EncodingImageFileWidget(field, request) 352 >>> print normalize(widget()) 353 <img... />... 354 ...<input 355 class="fileType" 356 id="field.foo" 357 name="field.foo" 358 size="20" 359 type="file" 360 /> 361 (sample.jpg)<input 362 class="hiddenType" 363 id="field.foo.file_id" 364 name="field.foo.file_id" 365 type="hidden" 366 value="c2FtcGxlLmpwZwo5ZmVhYzQyNjUwNzc5MjIwMDBhYThiODg3NDhlMjViZQ==" 367 /> 368 369 If there is both file data and an id, something else happens. First, let's 370 prepare some new file: 371 372 >>> upload = FileUpload(UploadedFile('sample2.jpg', open(testimage2, 'rb'))) 373 374 We happen to know, due to the implementation of 375 EncodingImageFileWidget, that the file_id is going to be 376 "Zm9vLnR4dAp0aGUgY29udGVudHM=". Let's make a request with the original 377 id, but a new file upload: 378 379 >>> request = TestRequest(form={'field.foo': upload, 380 ... 'field.foo.file_id': 381 ... 'Z2FtcGxlLmpwZwr/2P/gAA=='}) 382 383 We expect the new file to be the one that's uploaded: 384 385 >>> field = ImageFile(__name__='foo', title=u'Foo', required=False) 386 >>> field = field.bind(None) 387 >>> widget = EncodingImageFileWidget(field, request) 388 >>> print normalize(widget()) 389 <img... />... 390 ...<input 391 class="fileType" 392 id="field.foo" 393 name="field.foo" 394 size="20" 395 type="file" 396 /> 397 (sample2.jpg)<input 398 class="hiddenType" 399 id="field.foo.file_id" 400 name="field.foo.file_id" 401 type="hidden" 402 value="c2FtcGxlMi5qcGcKZDk2MDkwZWRlMmRjODlkZDdkZWM5ZDU3MmFkNThjNzQ=" 403 /> 404 405 The value displayed above again means the filename and md5 sum of the 406 stored file: 407 408 >>> "c2FtcGxlMi5qcGcKZDk2MDkwZWRlMmRjODlkZDdkZWM5ZDU3MmFkNThjNzQ=".decode( 409 ... 'base64') 410 'sample2.jpg\nd96090ede2dc89dd7dec9d572ad58c74' 411 412 where the md5 sum is in fact correct: 413 414 >>> import hashlib 415 >>> hashlib.md5(open(testimage2, 'rb').read()).hexdigest() 416 'd96090ede2dc89dd7dec9d572ad58c74' 417 418 Our file retrieval utility now contains two files: 419 420 >>> retrieval.storage.keys() 421 ['9feac4265077922000aa8b88748e25be', 'd96090ede2dc89dd7dec9d572ad58c74'] -
main/waeup.sirp/trunk/src/waeup/sirp/image/browser/tests/test_image.py
r5923 r6537 46 46 ) 47 47 ) 48
Note: See TracChangeset for help on using the changeset viewer.