source: main/waeup.sirp/trunk/src/waeup/sirp/image/schema.py @ 7137

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

Set value Id for property svn:keywords in all Python files.

  • Property svn:keywords set to Id
File size: 2.9 KB
Line 
1##
2## schema.py
3## Login : <uli@pu.smp.net>
4## Started on  Mon Sep 13 10:31:43 2010 Uli Fouquet
5## $Id: schema.py 7137 2011-11-19 08:37:08Z henrik $
6##
7## Copyright (C) 2010 Uli Fouquet
8## This program is free software; you can redistribute it and/or modify
9## it under the terms of the GNU General Public License as published by
10## the Free Software Foundation; either version 2 of the License, or
11## (at your option) any later version.
12##
13## This program is distributed in the hope that it will be useful,
14## but WITHOUT ANY WARRANTY; without even the implied warranty of
15## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16## GNU General Public License for more details.
17##
18## You should have received a copy of the GNU General Public License
19## along with this program; if not, write to the Free Software
20## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21##
22"""Image file schemas.
23"""
24from zope.interface import implements
25from zope.schema import MinMaxLen
26from zope.schema.interfaces import TooBig, TooSmall
27from hurry.file.schema import File
28from waeup.sirp.image.interfaces import IImageFile
29from waeup.sirp.image.image import WAeUPImageFile
30
31class 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   
65class ImageFile(MinMaxSize, File):
66    """An image file field.
67
68    Suitable for interfaces that wish to store image files in an
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.
85    """
86    implements(IImageFile)
87
88    _type = WAeUPImageFile
Note: See TracBrowser for help on using the repository browser.