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