Back to index

plone3  3.1.7
IdValidator.py
Go to the documentation of this file.
00001 from Acquisition import aq_parent
00002 from Acquisition import aq_inner
00003 from Acquisition import aq_base
00004 from Acquisition import aq_get
00005 from zExceptions import BadRequest
00006 from OFS import ObjectManager
00007 from Products.validation.interfaces import ivalidator
00008 
00009 
00010 class IdValidator:
00011     __implements__ = (ivalidator,)
00012 
00013     def __init__( self, name, title='', description=''):
00014         self.name = name
00015         self.title = title or name
00016         self.description = description
00017     
00018     def __call__(self, id, instance, *args, **kwargs):
00019         try:
00020             # try to use the check_id script of CMFPlone
00021             check_id = aq_get(instance, 'check_id', None, 1)
00022             if check_id is None:
00023                 raise AttributeError('check_id script not found')
00024             return check_id(id, required=kwargs.get('required', 0)) or 1
00025         except AttributeError:
00026             # space test
00027             if ' ' in id:
00028                 return 'Spaces are not allowed in ids'
00029             
00030             # in parent test
00031             parent = aq_parent(aq_inner(instance))
00032             # If the id is given to a different object already
00033             if id in parent.objectIds() and getattr(aq_base(parent), id) is not aq_base(instance):
00034                 return 'Id %s is already in use' % id
00035             
00036             # objet manager test
00037             # XXX: This is f***ed
00038             try:
00039                 ObjectManager.checkValidId(self, id, allow_dup=1)
00040             except BadRequest, m:
00041                 return str(m)
00042             return 1
00043 
00044 validatorList = [
00045     IdValidator('isValidId', title='', description=''),
00046     ]
00047 
00048 __all__ = ('validatorList', )