source: main/waeup.sirp/trunk/src/waeup/sirp/image/README.txt @ 6509

Last change on this file since 6509 was 6310, checked in by uli, 14 years ago

Remove IFileRetrieval-based parts of test. We do not support
IFileRetrieval currently.

Update docs a bit.

File size: 2.7 KB
Line 
1waeup.sirp.image -- handling image files
2========================================
3
4The image file widget is built on top of the :class:`WAeUPImageFile` object::
5
6  >>> from waeup.sirp.image import WAeUPImageFile
7  >>> file = WAeUPImageFile('foo.jpg', 'mydata')
8  >>> file.filename
9  'foo.jpg'
10  >>> file.data
11  'mydata'
12  >>> file.size
13  6
14  >>> f = file.file
15  >>> f.read()
16  'mydata'
17
18We can also create WAeUPImageFile objects from file-like objects::
19
20  >>> from StringIO import StringIO
21  >>> from zope import component
22  >>> from hurry.file.interfaces import IFileRetrieval
23  >>> fileretrieval = component.getUtility(IFileRetrieval)
24  >>> file = fileretrieval.createFile('bar.jpg', StringIO('test data'))
25  >>> file.filename
26  'bar.jpg'
27  >>> file.size
28  9
29  >>> file.data
30  'test data'
31  >>> f = file.file
32  >>> f.read()
33  'test data'
34
35The WAeUPImageFile object normally stores the file data using ZODB
36persistence. For better efficiency, each file is stored in a Blob.
37`ImageFile` field
38-----------------
39
40As every other field (most of them are defined in :mod:`zope.schema`)
41`ImageFile` can be used in interfaces to tell, that the associated
42attribute should be -- an image file.
43
44The `ImageFile` field accepts only certain content types:
45
46  >>> from waeup.sirp.image.schema import ImageFile
47  >>> from zope.publisher.browser import TestRequest
48  >>> field = ImageFile(__name__='foo', title=u'Foo')
49  >>> field.validate('asd')
50  Traceback (most recent call last):
51  ...
52  WrongType: ('asd', <class 'waeup.sirp.image.image.WAeUPImageFile'>, 'foo')
53
54which means: `ImageFile` fields should better contain
55:class:`WAeUPImageFile` instances.
56
57We can store normal :class:`WAeUPImageFile` instances:
58
59  >>> field.validate(WAeUPImageFile('bar.jpg', 'data')) is None
60  True
61
62The `ImageFile` field supports min and max values:
63
64  >>> field = ImageFile(__name__='foo', title=u'Foo')
65  >>> hasattr(field, 'min_size')
66  True
67
68  >>> hasattr(field, 'max_size')
69  True
70
71By default both attributes are set to ``None``:
72
73  >>> field.min_size is field.max_size is None
74  True
75
76But we can set them:
77
78  >>> field = ImageFile(__name__='bar', title=u'Bar',
79  ...                   min_size=5, max_size=12)
80
81and while these values are okay then:
82
83  >>> field.validate(
84  ...     WAeUPImageFile('bar.jpg', '123456789012')) is None
85  True
86
87  >>> field.validate(
88  ...     WAeUPImageFile('bar.jpg', '12345')) is None
89  True
90
91the following are not:
92
93  >>> field.validate(
94  ...     WAeUPImageFile('bar.jpg', '1234567890123'))
95  Traceback (most recent call last):
96  ...
97  TooBig: ('bar.jpg', '13 bytes (max: 12 bytes)')
98
99  >>> field.validate(
100  ...     WAeUPImageFile('bar.jpg', '1234'))
101  Traceback (most recent call last):
102  ...
103  TooSmall: ('bar.jpg', '4 bytes (min: 5 bytes)')
104
Note: See TracBrowser for help on using the repository browser.