Ignore:
Timestamp:
13 May 2011, 02:12:58 (14 years ago)
Author:
uli
Message:

Support restricting file size for images.

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  
    11waeup.sirp.image fields
    2 ================
     2=======================
    33
    44The image file widget is built on top of the ImageFile object::
     
    106106  4
    107107
     108`ImageFile` field
     109-----------------
     110
     111As every other field (most of them are defined in :mod:`zope.schema`)
     112`ImageFile` can be used in interfaces to tell, that the associated
     113attribute should be -- an image file.
     114
     115The `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
     125which means: `ImageFile` fields should better contain
     126:class:`WAeUPImageFile` instances.
     127
     128We can store normal :class:`WAeUPImageFile` instances:
     129
     130  >>> field.validate(WAeUPImageFile('bar.jpg', 'data')) is None
     131  True
     132
     133The `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
     142By default both attributes are set to ``None``:
     143
     144  >>> field.min_size is field.max_size is None
     145  True
     146
     147But we can set them:
     148
     149  >>> field = ImageFile(__name__='bar', title=u'Bar',
     150  ...                   min_size=5, max_size=12)
     151
     152and 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
     162the 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
    108177Clean up::
    109178
  • main/waeup.sirp/trunk/src/waeup/sirp/image/schema.py

    r5979 r6066  
    2323"""
    2424from zope.interface import implements
     25from zope.schema import MinMaxLen
     26from zope.schema.interfaces import TooBig, TooSmall
    2527from hurry.file.schema import File
    2628from waeup.sirp.image.interfaces import IImageFile
    2729from waeup.sirp.image.image import WAeUPImageFile
    2830
    29 class ImageFile(File):
     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):
    3066    """An image file field.
    3167
    3268    Suitable for interfaces that wish to store image files in an
    3369    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.
    3485    """
    3586    implements(IImageFile)
Note: See TracChangeset for help on using the changeset viewer.