1 | ## $Id: schema.py 7196 2011-11-25 07:44:52Z henrik $ |
---|
2 | ## |
---|
3 | ## Copyright (C) 2011 Uli Fouquet & Henrik Bettermann |
---|
4 | ## This program is free software; you can redistribute it and/or modify |
---|
5 | ## it under the terms of the GNU General Public License as published by |
---|
6 | ## the Free Software Foundation; either version 2 of the License, or |
---|
7 | ## (at your option) any later version. |
---|
8 | ## |
---|
9 | ## This program is distributed in the hope that it will be useful, |
---|
10 | ## but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
11 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
12 | ## GNU General Public License for more details. |
---|
13 | ## |
---|
14 | ## You should have received a copy of the GNU General Public License |
---|
15 | ## along with this program; if not, write to the Free Software |
---|
16 | ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
17 | ## |
---|
18 | """Image file schemas. |
---|
19 | """ |
---|
20 | from zope.interface import implements |
---|
21 | from zope.schema import MinMaxLen |
---|
22 | from zope.schema.interfaces import TooBig, TooSmall |
---|
23 | from hurry.file.schema import File |
---|
24 | from waeup.sirp.image.interfaces import IImageFile |
---|
25 | from waeup.sirp.image.image import WAeUPImageFile |
---|
26 | |
---|
27 | class MinMaxSize(object): |
---|
28 | """Expresses constraints on the size of an object. |
---|
29 | |
---|
30 | The 'size' of an object is determined by its `size` method or |
---|
31 | attribute. If an object has no such 'size' then it cannot be |
---|
32 | validated by this mixin. |
---|
33 | |
---|
34 | Please do not confuse `MinMaxSize` with `MinMaxLen`, for instance |
---|
35 | supported by ordinary text fields. These test on ``len(obj)`` |
---|
36 | which is not necessary possible for file-like objects. |
---|
37 | |
---|
38 | Therefore we distinguish 'size' from 'len' here. |
---|
39 | """ |
---|
40 | min_size = None |
---|
41 | max_size = None |
---|
42 | |
---|
43 | def __init__(self, min_size=None, max_size=None, **kw): |
---|
44 | self.min_size = min_size |
---|
45 | self.max_size = max_size |
---|
46 | super(MinMaxSize, self).__init__(**kw) |
---|
47 | |
---|
48 | def _validate(self, value): |
---|
49 | super(MinMaxSize, self)._validate(value) |
---|
50 | if self.max_size is not None and value.size > self.max_size: |
---|
51 | raise TooBig( |
---|
52 | value.filename, |
---|
53 | "%s bytes (max: %s bytes)" % (value.size, self.max_size)) |
---|
54 | |
---|
55 | if self.min_size is not None and value.size < self.min_size: |
---|
56 | raise TooSmall( |
---|
57 | value.filename, |
---|
58 | "%s bytes (min: %s bytes)" % (value.size, self.min_size)) |
---|
59 | |
---|
60 | |
---|
61 | class ImageFile(MinMaxSize, File): |
---|
62 | """An image file field. |
---|
63 | |
---|
64 | Suitable for interfaces that wish to store image files in an |
---|
65 | attribute. |
---|
66 | |
---|
67 | This field type supports `MinMaxSize` so that you can set |
---|
68 | `min_size` or `max_size` for all `ImageFile` fields in your |
---|
69 | interfaces like this: |
---|
70 | |
---|
71 | class MyInterface(Interface): |
---|
72 | image = ImageFile( |
---|
73 | title = u'The image', |
---|
74 | description = u'The nice image', |
---|
75 | max_size = 1024 * 10, |
---|
76 | ) |
---|
77 | |
---|
78 | to restrict the file size of stored images to 10 KBytes. |
---|
79 | |
---|
80 | By default no such restriction is set. |
---|
81 | """ |
---|
82 | implements(IImageFile) |
---|
83 | |
---|
84 | _type = WAeUPImageFile |
---|