source: main/waeup.kofa/trunk/src/waeup/kofa/image/schema.py @ 14395

Last change on this file since 14395 was 7819, checked in by Henrik Bettermann, 13 years ago

KOFA -> Kofa

  • Property svn:keywords set to Id
File size: 2.9 KB
RevLine 
[7196]1## $Id: schema.py 7819 2012-03-08 22:28:46Z henrik $
[5693]2##
[7196]3## Copyright (C) 2011 Uli Fouquet & Henrik Bettermann
[5693]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.
[7196]8##
[5693]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.
[7196]13##
[5693]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"""
20from zope.interface import implements
[6066]21from zope.schema import MinMaxLen
22from zope.schema.interfaces import TooBig, TooSmall
[5693]23from hurry.file.schema import File
[7811]24from waeup.kofa.image.interfaces import IImageFile
[7819]25from waeup.kofa.image.image import KofaImageFile
[5693]26
[6066]27class 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   
61class ImageFile(MinMaxSize, File):
[5979]62    """An image file field.
63
64    Suitable for interfaces that wish to store image files in an
65    attribute.
[6066]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.
[5693]81    """
82    implements(IImageFile)
[5972]83
[7819]84    _type = KofaImageFile
Note: See TracBrowser for help on using the repository browser.