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

Last change on this file since 6177 was 6066, checked in by uli, 14 years ago

Support restricting file size for images.

File size: 4.7 KB
Line 
1waeup.sirp.image fields
2=======================
3
4The image file widget is built on top of the ImageFile 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. Files can however also be stored by tramline.  If
37tramline is installed in Apache, the Tramline takes care of generating
38ids for files and storing the file on the filesystem directly. The ids
39are then passed as file data to be stored in the ZODB.
40
41Let's first enable tramline.
42
43The tramline directory structure is a directory with two subdirectories,
44one called 'repository' and the other called 'upload'::
45
46  >>> import tempfile, os
47  >>> dirpath = tempfile.mkdtemp()
48  >>> repositorypath = os.path.join(dirpath, 'repository')
49  >>> uploadpath = os.path.join(dirpath, 'upload')
50  >>> os.mkdir(repositorypath)
51  >>> os.mkdir(uploadpath)
52
53We create a TramlineFileRetrieval object knowing about this directory,
54and register it as a utility::
55
56  >>> from hurry.file.file import TramlineFileRetrievalBase
57  >>> class TramlineFileRetrieval(TramlineFileRetrievalBase):
58  ...    def getTramlinePath(self):
59  ...        return dirpath
60  >>> retrieval = TramlineFileRetrieval()
61  >>> component.provideUtility(retrieval, IFileRetrieval)
62
63Now let's store a file the way tramline would during upload::
64
65  >>> f = open(os.path.join(repositorypath, '1'), 'wb')
66  >>> f.write('test data')
67  >>> f.close()
68
69The file with underlying name '1' (the data stored in the ZODB will be
70just '1') will now be created::
71
72  >>> file = WAeUPImageFile('foo.jpg', '1')
73
74The data is now '1', referring to the real file::
75
76  >>> file.data
77  '1'
78
79Retrieving the file results in the real file::
80 
81  >>> f = file.file
82  >>> f.read()
83  'test data'
84
85We can also retrieve its size::
86
87  >>> file.size
88  9L
89
90Now let's disable tramline in our utility::
91
92  >>> class TramlineFileRetrieval(TramlineFileRetrievalBase):
93  ...     def getTramlinePath(self):
94  ...        return dirpath
95  ...     def isTramlineEnabled(self):
96  ...        return False
97  >>> component.provideUtility(TramlineFileRetrieval(), IFileRetrieval)
98
99We expect the same behavior as when tramline is not installed::
100
101  >>> file = WAeUPImageFile('foo.jpg', 'data')
102  >>> f = file.file
103  >>> f.read()
104  'data'
105  >>> file.size
106  4
107
108`ImageFile` field
109-----------------
110
111As every other field (most of them are defined in :mod:`zope.schema`)
112`ImageFile` can be used in interfaces to tell, that the associated
113attribute should be -- an image file.
114
115The `ImageFile` field accepts only certain content types:
116
117  >>> from waeup.sirp.image.schema import ImageFile
118  >>> from zope.publisher.browser import TestRequest
119  >>> field = ImageFile(__name__='foo', title=u'Foo')
120  >>> field.validate('asd')
121  Traceback (most recent call last):
122  ...
123  WrongType: ('asd', <class 'waeup.sirp.image.image.WAeUPImageFile'>, 'foo')
124
125which means: `ImageFile` fields should better contain
126:class:`WAeUPImageFile` instances.
127
128We can store normal :class:`WAeUPImageFile` instances:
129
130  >>> field.validate(WAeUPImageFile('bar.jpg', 'data')) is None
131  True
132
133The `ImageFile` field supports min and max values:
134
135  >>> field = ImageFile(__name__='foo', title=u'Foo')
136  >>> hasattr(field, 'min_size')
137  True
138
139  >>> hasattr(field, 'max_size')
140  True
141
142By default both attributes are set to ``None``:
143
144  >>> field.min_size is field.max_size is None
145  True
146
147But we can set them:
148
149  >>> field = ImageFile(__name__='bar', title=u'Bar',
150  ...                   min_size=5, max_size=12)
151
152and while these values are okay then:
153
154  >>> field.validate(
155  ...     WAeUPImageFile('bar.jpg', '123456789012')) is None
156  True
157
158  >>> field.validate(
159  ...     WAeUPImageFile('bar.jpg', '12345')) is None
160  True
161
162the following are not:
163
164  >>> field.validate(
165  ...     WAeUPImageFile('bar.jpg', '1234567890123'))
166  Traceback (most recent call last):
167  ...
168  TooBig: ('bar.jpg', '13 bytes (max: 12 bytes)')
169
170  >>> field.validate(
171  ...     WAeUPImageFile('bar.jpg', '1234'))
172  Traceback (most recent call last):
173  ...
174  TooSmall: ('bar.jpg', '4 bytes (min: 5 bytes)')
175
176
177Clean up::
178
179  >>> import shutil
180  >>> shutil.rmtree(dirpath)
Note: See TracBrowser for help on using the repository browser.