Back to index

plone3  3.1.7
FormAction.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 FormActionType(SimpleItem):
00012 
00013     security = ClassSecurityInfo()
00014     security.setDefaultAccess('allow')
00015 
00016     def __init__(self, id, factory, description):
00017         self.id = id
00018         self.factory = factory
00019         self.description = description
00020 
00021     def getId(self):
00022         return self.id
00023 
00024     def getFactory(self):
00025         return self.factory
00026 
00027     def getDescription(self):
00028         return self.description
00029 
00030 InitializeClass(FormActionType)
00031 
00032 
00033 class FormActionKey(Key):
00034 
00035     security = ClassSecurityInfo()
00036     security.setDefaultAccess('allow')
00037 
00038     def __init__(self, object_id, status, context_type, button, controller=None):
00039         object_id = object_id.strip()
00040         if controller:
00041             s = controller._checkId(object_id)
00042             if s:
00043                 raise ValueError, 'Illegal template id: %s' % s
00044 
00045         status = status.strip()
00046         if not status:
00047             raise ValueError, 'Illegal status %s' % (status)
00048 
00049         if not context_type:
00050             context_type = ANY_CONTEXT
00051 
00052         if button is not None:
00053             button = button.strip()
00054         if not button:
00055             button = ANY_BUTTON
00056 
00057         Key.__init__(self, (object_id, status, context_type, button))
00058 
00059     def getObjectId(self):
00060         return self.key[0]
00061 
00062     def getStatus(self):
00063         return self.key[1]
00064 
00065     def getContextType(self):
00066         return self.key[2]
00067 
00068     def getButton(self):
00069         return self.key[3]
00070 
00071 InitializeClass(FormActionKey)
00072 
00073 
00074 class FormAction(SimpleItem):
00075 
00076     security = ClassSecurityInfo()
00077     security.setDefaultAccess('allow')
00078 
00079     def __init__(self, object_id, status, context_type, button,
00080                  action_type, action_arg, controller=None):
00081         from FormController import form_action_types
00082 
00083         self.key = FormActionKey(object_id, status, context_type, button,
00084                                  controller)
00085         self.action_type = action_type
00086         self.action_arg = action_arg
00087 
00088         if not form_action_types.has_key(action_type):
00089             raise ValueError, 'Illegal action type %s for %s' % (action_type, object_id)
00090         if action_arg is not None:
00091             action_arg = action_arg.strip()
00092         self.action = form_action_types[action_type].getFactory()(action_arg)
00093 
00094     def __copy__(self):
00095         return FormAction(self.getObjectId(), self.getStatus(), self.getContextType(),
00096             self.getButton(), self.getActionType(), self.getActionArg())
00097 
00098     def getKey(self):
00099         return self.key
00100 
00101     def getObjectId(self):
00102         return self.key.getObjectId()
00103 
00104     def getStatus(self):
00105         return self.key.getStatus()
00106 
00107     def getContextType(self):
00108         return self.key.getContextType()
00109 
00110     def getButton(self):
00111         return self.key.getButton()
00112 
00113     def getActionType(self):
00114         return self.action_type
00115 
00116     def getActionArg(self):
00117         return self.action_arg
00118 
00119     def getAction(self):
00120         return self.action
00121 
00122     def __str__(self):
00123         return '%s.%s.%s.%s=%s:%s' % (str(self.getObjectId()), str(self.getStatus()), \
00124             str(self.getContextType()), str(self.getButton()), str(self.getActionType()), \
00125             str(self.getActionArg()))
00126 
00127 InitializeClass(FormAction)
00128 
00129 
00130 class FormActionContainer(SimpleItem):
00131 
00132     security = ClassSecurityInfo()
00133     security.setDefaultAccess('deny')
00134 
00135     def __init__(self):
00136         self.actions = {}
00137 
00138     def __copy__(self):
00139         newobj = FormActionContainer()
00140         for a in self.actions.values():
00141             newobj.set(a.__copy__())
00142         return newobj
00143 
00144     def set(self, action):
00145         self.actions[aq_base(action.getKey())] = aq_base(action)
00146         self._p_changed = 1
00147 
00148     def get(self, key):
00149         return self.actions[key]
00150 
00151     def delete(self, key):
00152         del self.actions[key]
00153         self._p_changed = 1
00154 
00155     def match(self, object_id, status, context_type, button):
00156         controller = getToolByName(self, 'portal_form_controller')
00157         action = None
00158         try:
00159             action = self.actions.get(FormActionKey(object_id, status, \
00160                                         context_type, button, controller), None)
00161             if action:
00162                 return action
00163         except ValueError:
00164             pass
00165         try:
00166             action = self.actions.get(FormActionKey(object_id, status, \
00167                                             ANY_CONTEXT, button, controller), None)
00168             if action:
00169                 return action
00170         except ValueError:
00171             pass
00172         try:
00173             action = self.actions.get(FormActionKey(object_id, status, \
00174                                             context_type, ANY_BUTTON, controller), None)
00175             if action:
00176                 return action
00177         except ValueError:
00178             pass
00179         try:
00180             return self.actions.get(FormActionKey(object_id, status, \
00181                                             ANY_CONTEXT, ANY_BUTTON, controller), None)
00182         except ValueError:
00183             pass
00184         return None
00185 
00186     def getFiltered(self, object_id=_marker, status=_marker, context_type=_marker,
00187                     button=_marker, action_type=_marker, action_arg=_marker):
00188         filtered = []
00189         keys = self.actions.keys()
00190         keys.sort()
00191         for key in keys:
00192             action = self.actions[key]
00193             if object_id != _marker and not action.getObjectId() == object_id:
00194                 continue
00195             if status and not action.getStatus() == status:
00196                 continue
00197             if context_type != _marker and not action.getContextType() == context_type:
00198                 continue
00199             if button != _marker and not action.getButton() == button:
00200                 continue
00201             if action_type != _marker and not action.getActionType() == action_type:
00202                 continue
00203             if action_arg != _marker and not action.getActionArg() == action_arg:
00204                 continue
00205             filtered.append(action)
00206         return filtered
00207 
00208 InitializeClass(FormActionContainer)