[5974] | 1 | waeup.sirp.image fields |
---|
[6066] | 2 | ======================= |
---|
[5974] | 3 | |
---|
| 4 | The image file widget is built on top of the ImageFile object:: |
---|
| 5 | |
---|
[5979] | 6 | >>> from waeup.sirp.image import WAeUPImageFile |
---|
| 7 | >>> file = WAeUPImageFile('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 | |
---|
[5979] | 18 | We can also create WAeUPImageFile 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 | |
---|
[5979] | 35 | The WAeUPImageFile object normally stores the file data using ZODB |
---|
[5974] | 36 | persistence. Files can however also be stored by tramline. If |
---|
| 37 | tramline is installed in Apache, the Tramline takes care of generating |
---|
| 38 | ids for files and storing the file on the filesystem directly. The ids |
---|
| 39 | are then passed as file data to be stored in the ZODB. |
---|
| 40 | |
---|
| 41 | Let's first enable tramline. |
---|
| 42 | |
---|
| 43 | The tramline directory structure is a directory with two subdirectories, |
---|
| 44 | one 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 | |
---|
| 53 | We create a TramlineFileRetrieval object knowing about this directory, |
---|
| 54 | and 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 | |
---|
| 63 | Now 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 | |
---|
| 69 | The file with underlying name '1' (the data stored in the ZODB will be |
---|
| 70 | just '1') will now be created:: |
---|
| 71 | |
---|
[5979] | 72 | >>> file = WAeUPImageFile('foo.jpg', '1') |
---|
[5974] | 73 | |
---|
| 74 | The data is now '1', referring to the real file:: |
---|
| 75 | |
---|
| 76 | >>> file.data |
---|
| 77 | '1' |
---|
| 78 | |
---|
| 79 | Retrieving the file results in the real file:: |
---|
| 80 | |
---|
| 81 | >>> f = file.file |
---|
| 82 | >>> f.read() |
---|
| 83 | 'test data' |
---|
| 84 | |
---|
| 85 | We can also retrieve its size:: |
---|
| 86 | |
---|
| 87 | >>> file.size |
---|
[5997] | 88 | 9L |
---|
[5974] | 89 | |
---|
| 90 | Now 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 | |
---|
| 99 | We expect the same behavior as when tramline is not installed:: |
---|
| 100 | |
---|
[5979] | 101 | >>> file = WAeUPImageFile('foo.jpg', 'data') |
---|
[5974] | 102 | >>> f = file.file |
---|
| 103 | >>> f.read() |
---|
| 104 | 'data' |
---|
| 105 | >>> file.size |
---|
| 106 | 4 |
---|
| 107 | |
---|
[6066] | 108 | `ImageFile` field |
---|
| 109 | ----------------- |
---|
| 110 | |
---|
| 111 | As every other field (most of them are defined in :mod:`zope.schema`) |
---|
| 112 | `ImageFile` can be used in interfaces to tell, that the associated |
---|
| 113 | attribute should be -- an image file. |
---|
| 114 | |
---|
| 115 | The `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 | |
---|
| 125 | which means: `ImageFile` fields should better contain |
---|
| 126 | :class:`WAeUPImageFile` instances. |
---|
| 127 | |
---|
| 128 | We can store normal :class:`WAeUPImageFile` instances: |
---|
| 129 | |
---|
| 130 | >>> field.validate(WAeUPImageFile('bar.jpg', 'data')) is None |
---|
| 131 | True |
---|
| 132 | |
---|
| 133 | The `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 | |
---|
| 142 | By default both attributes are set to ``None``: |
---|
| 143 | |
---|
| 144 | >>> field.min_size is field.max_size is None |
---|
| 145 | True |
---|
| 146 | |
---|
| 147 | But we can set them: |
---|
| 148 | |
---|
| 149 | >>> field = ImageFile(__name__='bar', title=u'Bar', |
---|
| 150 | ... min_size=5, max_size=12) |
---|
| 151 | |
---|
| 152 | and 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 | |
---|
| 162 | the 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 | |
---|
[5974] | 177 | Clean up:: |
---|
| 178 | |
---|
| 179 | >>> import shutil |
---|
| 180 | >>> shutil.rmtree(dirpath) |
---|