Back to index

plone3  3.1.7
Public Member Functions | Public Attributes | Static Private Attributes
validation.validators.SupplValidators.MaxSizeValidator Class Reference

List of all members.

Public Member Functions

def __init__
def __call__

Public Attributes

 name
 title
 description
 maxsize

Static Private Attributes

 __implements__ = IValidator

Detailed Description

Tests if an upload, file or something supporting len() is smaller than a 
   given max size value
   
If it's a upload or file like thing it is using seek(0, 2) which means go
to the end of the file and tell() to get the size in bytes otherwise it is
trying to use len()

The maxsize can be acquired from the kwargs in a call, a 
getMaxSizeFor(fieldname) on the instance, a maxsize attribute on the field
or a given maxsize at validator initialization.

Definition at line 24 of file SupplValidators.py.


Constructor & Destructor Documentation

def validation.validators.SupplValidators.MaxSizeValidator.__init__ (   self,
  name,
  title = '',
  description = '',
  maxsize = 0 
)

Definition at line 38 of file SupplValidators.py.

00038 
00039     def __init__(self, name, title='', description='', maxsize=0):
00040         self.name = name
00041         self.title = title or name
00042         self.description = description
00043         self.maxsize= maxsize


Member Function Documentation

def validation.validators.SupplValidators.MaxSizeValidator.__call__ (   self,
  value,
  args,
  kwargs 
)

Definition at line 44 of file SupplValidators.py.

00044 
00045     def __call__(self, value, *args, **kwargs):
00046         instance = kwargs.get('instance', None)
00047         field    = kwargs.get('field', None)
00048 
00049         # get max size
00050         if kwargs.has_key('maxsize'):
00051             maxsize = kwargs.get('maxsize')
00052         elif hasattr(aq_base(instance), 'getMaxSizeFor'):
00053             maxsize = instance.getMaxSizeFor(field.getName())
00054         elif hasattr(field, 'maxsize'):
00055             maxsize = field.maxsize
00056         else:
00057             # set to given default value (default defaults to 0)
00058             maxsize = self.maxsize
00059         
00060         if not maxsize:
00061             return True
00062         
00063         # calculate size
00064         elif isinstance(value, FileUpload) or type(value) is FileType \
00065           or hasattr(aq_base(value), 'tell'):
00066             value.seek(0, 2) # eof
00067             size = value.tell()
00068             value.seek(0)
00069         else:
00070             try:
00071                 size = len(value)
00072             except TypeError:
00073                 size = 0
00074         size = float(size)
00075         sizeMB = (size / (1024 * 1024))
00076 
00077         if sizeMB > maxsize:
00078             return ("Validation failed(%(name)s: Uploaded data is too large: %(size).3fMB (max %(max)fMB)" %
00079                      { 'name' : self.name, 'size' : sizeMB, 'max' : maxsize })
00080         else:
00081             return True
00082 
00083 
try:

Member Data Documentation

Definition at line 36 of file SupplValidators.py.

Definition at line 41 of file SupplValidators.py.

Definition at line 42 of file SupplValidators.py.

Definition at line 39 of file SupplValidators.py.

Definition at line 40 of file SupplValidators.py.


The documentation for this class was generated from the following file: