Back to index

plone3  3.1.7
FormValidator.py
Go to the documentation of this file.
00001 from AccessControl import ClassSecurityInfo
00002 from Globals import InitializeClass
00003 from OFS.SimpleItem import SimpleItem
00004 from Products.CMFCore.utils import getToolByName
00005 from Key import Key
00006 from globalVars import ANY_CONTEXT, ANY_BUTTON
00007 from Acquisition import aq_base
00008 
00009 _marker = []
00010 
00011 class FormValidatorKey(Key):
00012 
00013     security = ClassSecurityInfo()
00014     security.setDefaultAccess('allow')
00015 
00016     def __init__(self, object_id, context_type, button, controller=None):
00017         object_id = object_id.strip()
00018         if controller:
00019             path_elements = object_id.split('/')
00020             for p in path_elements:
00021                 s = controller._checkId(p)
00022                 if s:
00023                     raise ValueError, 'Illegal template id: %s' % s
00024 
00025         if not context_type:
00026             context_type = ANY_CONTEXT
00027 
00028         if button is not None:
00029             button = button.strip()
00030         if not button:
00031             button = ANY_BUTTON
00032 
00033         Key.__init__(self, (object_id, context_type, button))
00034 
00035     def getObjectId(self):
00036         return self.key[0]
00037 
00038     def getContextType(self):
00039         return self.key[1]
00040 
00041     def getButton(self):
00042         return self.key[2]
00043 
00044 InitializeClass(FormValidatorKey)
00045 
00046 
00047 class FormValidator(SimpleItem):
00048 
00049     security = ClassSecurityInfo()
00050     security.setDefaultAccess('allow')
00051 
00052     def __init__(self, object_id, context_type, button, validators, controller=None):
00053         self.key = FormValidatorKey(object_id, context_type, button, controller)
00054 
00055         if isinstance(validators, basestring):
00056             validators = validators.split(',')
00057         validators = [v.strip() for v in validators if v]
00058         if controller:
00059             for v in validators:
00060                 path_elements = v.split('/')
00061                 for p in path_elements:
00062                     s = controller._checkId(p)
00063                     if s:
00064                         raise ValueError, 'Illegal template id: %s' % s
00065         self.validators = validators
00066 
00067     def __copy__(self):
00068         return FormValidator(self.getObjectId(), self.getContextType(), self.getButton(), self.getValidators())
00069 
00070     def getObjectId(self):
00071         return self.key.getObjectId()
00072 
00073     def getContextType(self):
00074         return self.key.getContextType()
00075 
00076     def getButton(self):
00077         return self.key.getButton()
00078 
00079     def getValidators(self):
00080         return self.validators
00081 
00082     def getKey(self):
00083         return self.key
00084 
00085 InitializeClass(FormValidator)
00086 
00087 
00088 class FormValidatorContainer(SimpleItem):
00089 
00090     security = ClassSecurityInfo()
00091     security.setDefaultAccess('deny')
00092 
00093     def __init__(self):
00094         self.validators = {}
00095 
00096     def __copy__(self):
00097         newobj = FormValidatorContainer()
00098         for v in self.validators.values():
00099             newobj.set(v.__copy__())
00100         return newobj
00101 
00102     def set(self, validator):
00103         self.validators[aq_base(validator.getKey())] = aq_base(validator)
00104         self._p_changed = 1
00105 
00106     def get(self, key):
00107         return self.validators[key]
00108 
00109     def delete(self, key):
00110         del self.validators[key]
00111         self._p_changed = 1
00112 
00113     def match(self, object_id, context_type, button):
00114         controller = getToolByName(self, 'portal_form_controller')
00115         validator = None
00116         try:
00117             validator = self.validators.get(FormValidatorKey(object_id, context_type, button, controller), None)
00118             if validator:
00119                 return validator
00120         except ValueError:
00121             pass
00122         try:
00123             validator = self.validators.get(FormValidatorKey(object_id, ANY_CONTEXT, button, controller))
00124             if validator:
00125                 return validator
00126         except ValueError:
00127             pass
00128         try:
00129             validator = self.validators.get(FormValidatorKey(object_id, context_type, ANY_BUTTON, controller))
00130             if validator:
00131                 return validator
00132         except ValueError:
00133             pass
00134         try:
00135             return self.validators.get(FormValidatorKey(object_id, ANY_CONTEXT, ANY_BUTTON, controller), None)
00136         except ValueError:
00137             pass
00138         return None
00139 
00140     def getFiltered(self, object_id=_marker, context_type=_marker,
00141                     button=_marker, validators=_marker):
00142         filtered = []
00143         keys = self.validators.keys()
00144         keys.sort()
00145         for key in keys:
00146             validator = self.validators[key]
00147             if object_id != _marker and not validator.getObjectId() == object_id:
00148                 continue
00149             if context_type != _marker and not validator.getContextType() == context_type:
00150                 continue
00151             if button != _marker and not validator.getButton() == button:
00152                 continue
00153             if validators != _marker and not validator.getValidators() == validators:
00154                 continue
00155             filtered.append(validator)
00156         return filtered