Changeset 6066 for main/waeup.sirp/trunk/src/waeup/sirp/image
- Timestamp:
- 13 May 2011, 02:12:58 (14 years ago)
- Location:
- main/waeup.sirp/trunk/src/waeup/sirp/image
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
main/waeup.sirp/trunk/src/waeup/sirp/image/README.txt
r5997 r6066 1 1 waeup.sirp.image fields 2 ================ 2 ======================= 3 3 4 4 The image file widget is built on top of the ImageFile object:: … … 106 106 4 107 107 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 108 177 Clean up:: 109 178 -
main/waeup.sirp/trunk/src/waeup/sirp/image/schema.py
r5979 r6066 23 23 """ 24 24 from zope.interface import implements 25 from zope.schema import MinMaxLen 26 from zope.schema.interfaces import TooBig, TooSmall 25 27 from hurry.file.schema import File 26 28 from waeup.sirp.image.interfaces import IImageFile 27 29 from waeup.sirp.image.image import WAeUPImageFile 28 30 29 class ImageFile(File): 31 class MinMaxSize(object): 32 """Expresses constraints on the size of an object. 33 34 The 'size' of an object is determined by its `size` method or 35 attribute. If an object has no such 'size' then it cannot be 36 validated by this mixin. 37 38 Please do not confuse `MinMaxSize` with `MinMaxLen`, for instance 39 supported by ordinary text fields. These test on ``len(obj)`` 40 which is not necessary possible for file-like objects. 41 42 Therefore we distinguish 'size' from 'len' here. 43 """ 44 min_size = None 45 max_size = None 46 47 def __init__(self, min_size=None, max_size=None, **kw): 48 self.min_size = min_size 49 self.max_size = max_size 50 super(MinMaxSize, self).__init__(**kw) 51 52 def _validate(self, value): 53 super(MinMaxSize, self)._validate(value) 54 if self.max_size is not None and value.size > self.max_size: 55 raise TooBig( 56 value.filename, 57 "%s bytes (max: %s bytes)" % (value.size, self.max_size)) 58 59 if self.min_size is not None and value.size < self.min_size: 60 raise TooSmall( 61 value.filename, 62 "%s bytes (min: %s bytes)" % (value.size, self.min_size)) 63 64 65 class ImageFile(MinMaxSize, File): 30 66 """An image file field. 31 67 32 68 Suitable for interfaces that wish to store image files in an 33 69 attribute. 70 71 This field type supports `MinMaxSize` so that you can set 72 `min_size` or `max_size` for all `ImageFile` fields in your 73 interfaces like this: 74 75 class MyInterface(Interface): 76 image = ImageFile( 77 title = u'The image', 78 description = u'The nice image', 79 max_size = 1024 * 10, 80 ) 81 82 to restrict the file size of stored images to 10 KBytes. 83 84 By default no such restriction is set. 34 85 """ 35 86 implements(IImageFile)
Note: See TracChangeset for help on using the changeset viewer.