[7811] | 1 | waeup.kofa.image -- handling image files |
---|
[6310] | 2 | ======================================== |
---|
[5974] | 3 | |
---|
[7819] | 4 | The image file widget is built on top of the :class:`KofaImageFile` object:: |
---|
[5974] | 5 | |
---|
[7819] | 6 | >>> from waeup.kofa.image import KofaImageFile |
---|
| 7 | >>> file = KofaImageFile('foo.jpg', 'mydata') |
---|
[5974] | 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 | |
---|
[7819] | 18 | We can also create KofaImageFile objects from file-like objects:: |
---|
[5974] | 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 | |
---|
[6525] | 35 | Tramline Support |
---|
| 36 | ---------------- |
---|
| 37 | |
---|
[7819] | 38 | The KofaImageFile object normally stores the file data using ZODB |
---|
[6525] | 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 | |
---|
[7819] | 75 | >>> file = KofaImageFile('foo.jpg', '1') |
---|
[6525] | 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 | |
---|
[7495] | 90 | >>> int(file.size) |
---|
| 91 | 9 |
---|
[6525] | 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 | |
---|
[7819] | 104 | >>> file = KofaImageFile('foo.jpg', 'data') |
---|
[6525] | 105 | >>> f = file.file |
---|
| 106 | >>> f.read() |
---|
| 107 | 'data' |
---|
| 108 | >>> file.size |
---|
| 109 | 4 |
---|
| 110 | |
---|
[6526] | 111 | Clean up: |
---|
| 112 | |
---|
| 113 | >>> import shutil |
---|
| 114 | >>> shutil.rmtree(dirpath) |
---|
| 115 | |
---|
[7811] | 116 | Support for :mod:`waeup.kofa.imagestorage` |
---|
[6525] | 117 | ------------------------------------------ |
---|
| 118 | |
---|
| 119 | The behaviour shown above can be used for any Zope3 application. With |
---|
[7811] | 120 | :mod:`waeup.kofa` we use a special file retrieval utility defined in |
---|
| 121 | :mod:`waeup.kofa.imagestorage`. As this utility is based on |
---|
| 122 | :mod:`waeup.kofa` internal stuff we do not show it here but in the |
---|
[6525] | 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 |
---|
[6066] | 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 | |
---|
[7811] | 137 | >>> from waeup.kofa.image.schema import ImageFile |
---|
[6066] | 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 | ... |
---|
[7819] | 143 | WrongType: ('asd', <class 'waeup.kofa.image.image.KofaImageFile'>, 'foo') |
---|
[6066] | 144 | |
---|
| 145 | which means: `ImageFile` fields should better contain |
---|
[7819] | 146 | :class:`KofaImageFile` instances. |
---|
[6066] | 147 | |
---|
[7819] | 148 | We can store normal :class:`KofaImageFile` instances: |
---|
[6066] | 149 | |
---|
[7819] | 150 | >>> field.validate(KofaImageFile('bar.jpg', 'data')) is None |
---|
[6066] | 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 | |
---|
[6310] | 169 | >>> field = ImageFile(__name__='bar', title=u'Bar', |
---|
[6066] | 170 | ... min_size=5, max_size=12) |
---|
| 171 | |
---|
| 172 | and while these values are okay then: |
---|
| 173 | |
---|
| 174 | >>> field.validate( |
---|
[7819] | 175 | ... KofaImageFile('bar.jpg', '123456789012')) is None |
---|
[6066] | 176 | True |
---|
| 177 | |
---|
| 178 | >>> field.validate( |
---|
[7819] | 179 | ... KofaImageFile('bar.jpg', '12345')) is None |
---|
[6066] | 180 | True |
---|
| 181 | |
---|
| 182 | the following are not: |
---|
| 183 | |
---|
| 184 | >>> field.validate( |
---|
[7819] | 185 | ... KofaImageFile('bar.jpg', '1234567890123')) |
---|
[6066] | 186 | Traceback (most recent call last): |
---|
| 187 | ... |
---|
| 188 | TooBig: ('bar.jpg', '13 bytes (max: 12 bytes)') |
---|
| 189 | |
---|
| 190 | >>> field.validate( |
---|
[7819] | 191 | ... KofaImageFile('bar.jpg', '1234')) |
---|
[6066] | 192 | Traceback (most recent call last): |
---|
| 193 | ... |
---|
| 194 | TooSmall: ('bar.jpg', '4 bytes (min: 5 bytes)') |
---|
| 195 | |
---|
[6584] | 196 | |
---|
| 197 | Broken Unequality Comparison |
---|
| 198 | ---------------------------- |
---|
| 199 | |
---|
[7819] | 200 | KofaImageFile does not reproduce the broken unequal comparison from |
---|
[6584] | 201 | its base: |
---|
| 202 | |
---|
[7819] | 203 | >>> f1 = KofaImageFile('bar.jpg', '123456789') |
---|
| 204 | >>> f2 = KofaImageFile('bar.jpg', '123456789') |
---|
| 205 | >>> f3 = KofaImageFile('baz.jpg', '1234') |
---|
[6584] | 206 | >>> f1 == f2 |
---|
| 207 | True |
---|
| 208 | |
---|
| 209 | >>> f1 != f2 |
---|
| 210 | False |
---|
| 211 | |
---|
| 212 | >>> f1 == f3 |
---|
| 213 | False |
---|
| 214 | |
---|
| 215 | >>> f1 != f3 |
---|
| 216 | True |
---|