| 1 | ################################################################################
|
|---|
| 2 | # _zmi_actions_util.py
|
|---|
| 3 | #
|
|---|
| 4 | # This program is free software; you can redistribute it and/or
|
|---|
| 5 | # modify it under the terms of the GNU General Public License
|
|---|
| 6 | # as published by the Free Software Foundation; either version 2
|
|---|
| 7 | # of the License, or (at your option) any later version.
|
|---|
| 8 | #
|
|---|
| 9 | # This program is distributed in the hope that it will be useful,
|
|---|
| 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|---|
| 12 | # GNU General Public License for more details.
|
|---|
| 13 | #
|
|---|
| 14 | # You should have received a copy of the GNU General Public License
|
|---|
| 15 | # along with this program; if not, write to the Free Software
|
|---|
| 16 | # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|---|
| 17 | ################################################################################
|
|---|
| 18 |
|
|---|
| 19 | # Product imports.
|
|---|
| 20 | import _globals
|
|---|
| 21 |
|
|---|
| 22 | def zmi_actions(container, context, attr_id='e'):
|
|---|
| 23 | """
|
|---|
| 24 | """
|
|---|
| 25 | actions = []
|
|---|
| 26 |
|
|---|
| 27 | REQUEST = container.REQUEST
|
|---|
| 28 | if container.meta_id == 'ZMSTrashcan':
|
|---|
| 29 | objAttr = {}
|
|---|
| 30 | else:
|
|---|
| 31 | objAttr = container.getMetaobjAttr( container.meta_id, attr_id)
|
|---|
| 32 | objChildren = len(container.getObjChildren(attr_id,REQUEST))
|
|---|
| 33 | objPath = ''
|
|---|
| 34 | if context is not None and context != container:
|
|---|
| 35 | objPath = context.id+'/'
|
|---|
| 36 |
|
|---|
| 37 | #-- Action: Separator.
|
|---|
| 38 | actions.append(('----- %s -----'%container.getZMILangStr('ACTION_SELECT')%container.getZMILangStr('ATTR_ACTION'),''))
|
|---|
| 39 | actions.extend(zmi_basic_actions(container, context, objAttr, objChildren, objPath))
|
|---|
| 40 | actions.extend(zmi_insert_actions(container, context, objAttr, objChildren, objPath))
|
|---|
| 41 |
|
|---|
| 42 | # Return action list.
|
|---|
| 43 | return actions
|
|---|
| 44 |
|
|---|
| 45 |
|
|---|
| 46 | def zmi_basic_actions(container, context, objAttr, objChildren, objPath=''):
|
|---|
| 47 | """
|
|---|
| 48 | Returns sorted list of basic actions (undo, delete, cut, copy, paste,
|
|---|
| 49 | move up/down) and custom commands.
|
|---|
| 50 | """
|
|---|
| 51 | actions = []
|
|---|
| 52 |
|
|---|
| 53 | REQUEST = container.REQUEST
|
|---|
| 54 | lang = REQUEST['lang']
|
|---|
| 55 | auth_user = REQUEST['AUTHENTICATED_USER']
|
|---|
| 56 |
|
|---|
| 57 | repetitive = objAttr.get('repetitive',0)==1
|
|---|
| 58 | mandatory = objAttr.get('mandatory',0)==1
|
|---|
| 59 |
|
|---|
| 60 | #-- Action: Edit.
|
|---|
| 61 | if context is not None:
|
|---|
| 62 | actions.append((container.getZMILangStr('BTN_EDIT'),objPath+'manage_main'))
|
|---|
| 63 | if context.getLevel() > 0:
|
|---|
| 64 | if repetitive or not mandatory:
|
|---|
| 65 | #-- Action: Undo.
|
|---|
| 66 | can_undo = context.inObjStates( [ 'STATE_NEW', 'STATE_MODIFIED', 'STATE_DELETED'], REQUEST)
|
|---|
| 67 | if can_undo:
|
|---|
| 68 | actions.append((container.getZMILangStr('BTN_UNDO'),'manage_undoObjs'))
|
|---|
| 69 | #-- Action: Delete.
|
|---|
| 70 | can_delete = not context.inObjStates( [ 'STATE_DELETED'], REQUEST) and context.getAutocommit() or context.getDCCoverage(REQUEST).endswith('.'+lang)
|
|---|
| 71 | if can_delete:
|
|---|
| 72 | ob_access = context.getObjProperty('manage_access',REQUEST)
|
|---|
| 73 | can_delete = can_delete and ((not type(ob_access) is dict) or (ob_access.get( 'delete') is None) or (len( container.intersection_list( ob_access.get( 'delete'), context.getUserRoles(auth_user))) > 0))
|
|---|
| 74 | metaObj = container.getMetaobj( context.meta_id)
|
|---|
| 75 | can_delete = can_delete and ((metaObj.get( 'access') is None) or (metaObj.get( 'access', {}).get( 'delete') is None) or (len( container.intersection_list( metaObj.get( 'access').get( 'delete'), context.getUserRoles(auth_user))) > 0))
|
|---|
| 76 | if can_delete:
|
|---|
| 77 | actions.append((container.getZMILangStr('BTN_DELETE'),'manage_deleteObjs'))
|
|---|
| 78 | #-- Action: Cut.
|
|---|
| 79 | can_cut = not context.inObjStates( [ 'STATE_DELETED'], REQUEST) and context.getAutocommit() or context.getDCCoverage(REQUEST).endswith('.'+lang)
|
|---|
| 80 | if can_cut:
|
|---|
| 81 | actions.append((container.getZMILangStr('BTN_CUT'),'manage_cutObjects'))
|
|---|
| 82 | #-- Action: Copy.
|
|---|
| 83 | actions.append((container.getZMILangStr('BTN_COPY'),'manage_copyObjects'))
|
|---|
| 84 | #-- Actions: Move.
|
|---|
| 85 | can_move = objChildren > 1
|
|---|
| 86 | if can_move:
|
|---|
| 87 | actions.append((container.getZMILangStr('ACTION_MOVEUP'),objPath+'manage_moveObjUp'))
|
|---|
| 88 | actions.append((container.getZMILangStr('ACTION_MOVEDOWN'),objPath+'manage_moveObjDown'))
|
|---|
| 89 |
|
|---|
| 90 | #-- Action: Paste.
|
|---|
| 91 | if repetitive or objChildren==0:
|
|---|
| 92 | if container.cb_dataValid():
|
|---|
| 93 | if objAttr['type']=='*':
|
|---|
| 94 | meta_ids = objAttr['keys']
|
|---|
| 95 | else:
|
|---|
| 96 | meta_ids = [objAttr['type']]
|
|---|
| 97 | append = True
|
|---|
| 98 | try:
|
|---|
| 99 | for ob in container.cp_get_obs( REQUEST):
|
|---|
| 100 | metaObj = ob.getMetaobj( ob.meta_id)
|
|---|
| 101 | append = append and (ob.meta_id in meta_ids or 'type(%s)'%metaObj['type'] in meta_ids)
|
|---|
| 102 | except:
|
|---|
| 103 | append = False
|
|---|
| 104 | if append:
|
|---|
| 105 | actions.append((container.getZMILangStr('BTN_PASTE'),'manage_pasteObjs'))
|
|---|
| 106 |
|
|---|
| 107 | #-- Custom Commands.
|
|---|
| 108 | actions.extend(zmi_command_actions(context, insert_actions=False, objPath=objPath))
|
|---|
| 109 |
|
|---|
| 110 | # Return action list.
|
|---|
| 111 | return actions
|
|---|
| 112 |
|
|---|
| 113 |
|
|---|
| 114 | def zmi_insert_actions(container, context, objAttr, objChildren, objPath=''):
|
|---|
| 115 | """
|
|---|
| 116 | Returns sorted list of insert actions.
|
|---|
| 117 | """
|
|---|
| 118 | actions = []
|
|---|
| 119 | if container.meta_id == 'ZMSTrashcan':
|
|---|
| 120 | return actions
|
|---|
| 121 |
|
|---|
| 122 | REQUEST = container.REQUEST
|
|---|
| 123 | auth_user = REQUEST['AUTHENTICATED_USER']
|
|---|
| 124 | absolute_url = '/'.join(list(container.getPhysicalPath())+[''])
|
|---|
| 125 |
|
|---|
| 126 | repetitive = objAttr.get('repetitive',0)==1
|
|---|
| 127 | mandatory = objAttr.get('mandatory',0)==1
|
|---|
| 128 |
|
|---|
| 129 | #-- Objects.
|
|---|
| 130 | if repetitive or len(container.getObjChildren(objAttr['id'],REQUEST))==0:
|
|---|
| 131 | metaObjIds = container.getMetaobjIds()
|
|---|
| 132 | meta_ids = []
|
|---|
| 133 | if objAttr['type']=='*':
|
|---|
| 134 | for meta_id in objAttr['keys']:
|
|---|
| 135 | if meta_id.startswith('type(') and meta_id.endswith(')'):
|
|---|
| 136 | for metaObjId in metaObjIds:
|
|---|
| 137 | metaObj = container.getMetaobj( metaObjId)
|
|---|
| 138 | if metaObj['type'] == meta_id[5:-1] and metaObj['enabled'] == 1:
|
|---|
| 139 | meta_ids.append( metaObj['id'])
|
|---|
| 140 | elif objAttr['id']=='e' and meta_id in metaObjIds:
|
|---|
| 141 | metaObj = container.getMetaobj( meta_id)
|
|---|
| 142 | if metaObj['enabled'] == 1:
|
|---|
| 143 | meta_ids.append( meta_id)
|
|---|
| 144 | elif meta_id in metaObjIds:
|
|---|
| 145 | meta_ids.append( meta_id)
|
|---|
| 146 | else:
|
|---|
| 147 | _globals.writeError( container, '[zmi_insert_actions]: %s.%s contains invalid meta_id \'%s\''%(container.meta_id,objAttr['id'],meta_id))
|
|---|
| 148 | else:
|
|---|
| 149 | meta_ids.append( objAttr['type'])
|
|---|
| 150 | for meta_id in meta_ids:
|
|---|
| 151 | metaObj = container.getMetaobj(meta_id)
|
|---|
| 152 | ob_access = True
|
|---|
| 153 | ob_manage_access = container.getMetaobjAttr(meta_id,'manage_access')
|
|---|
| 154 | if ob_manage_access is not None:
|
|---|
| 155 | try:
|
|---|
| 156 | ob_access = _globals.dt_html(container,ob_manage_access['custom'],REQUEST)
|
|---|
| 157 | except:
|
|---|
| 158 | _globals.writeError( container, '[zmi_insert_actions]: can\'t get manage_access from %s'%meta_id)
|
|---|
| 159 | can_insert = True
|
|---|
| 160 | if objAttr['type']=='*':
|
|---|
| 161 | can_insert = can_insert and ((type(ob_access) is not dict) or (ob_access.get( 'insert') is None) or (len( container.intersection_list( ob_access.get( 'insert'), container.getUserRoles(auth_user))) > 0))
|
|---|
| 162 | mo_access = metaObj.get( 'access')
|
|---|
| 163 | if type(mo_access) is dict:
|
|---|
| 164 | mo_access_insert_roles = mo_access.get('insert')
|
|---|
| 165 | if type(mo_access_insert_roles) is list:
|
|---|
| 166 | can_insert = can_insert and len( container.intersection_list( mo_access_insert_roles, container.getUserRoles(auth_user))) > 0
|
|---|
| 167 | mo_access_insert_nodes = container.string_list(mo_access.get('insert_custom','{$}'))
|
|---|
| 168 | sl = []
|
|---|
| 169 | sl.extend(map( lambda x: (container.getHome().id+'/content/'+x[2:-1]+'/').replace('//','/'),filter(lambda x: x.find('@')<0,mo_access_insert_nodes)))
|
|---|
| 170 | sl.extend(map( lambda x: (x[2:-1].replace('@','/content/')+'/').replace('//','/'),filter(lambda x: x.find('@')>0,mo_access_insert_nodes)))
|
|---|
| 171 | can_insert = can_insert and len( filter( lambda x: absolute_url.find(x)>=0, sl)) > 0
|
|---|
| 172 | if can_insert:
|
|---|
| 173 | if meta_id in container.dGlobalAttrs.keys() and container.dGlobalAttrs[meta_id].has_key('constructor'):
|
|---|
| 174 | value = 'manage_addProduct/zms/%s'%container.dGlobalAttrs[meta_id]['constructor']
|
|---|
| 175 | elif metaObj['type']=='ZMSModule':
|
|---|
| 176 | value = 'manage_addZMSModule'
|
|---|
| 177 | elif objAttr['type'] in meta_ids and repetitive and objAttr.get('custom'):
|
|---|
| 178 | value = 'manage_addZMSCustomDefault'
|
|---|
| 179 | else:
|
|---|
| 180 | value = 'manage_addProduct/zms/manage_addzmscustomform'
|
|---|
| 181 | action = (container.display_type(REQUEST,meta_id),value)
|
|---|
| 182 | if action not in actions:
|
|---|
| 183 | actions.append( action)
|
|---|
| 184 |
|
|---|
| 185 | #-- Insert Commands.
|
|---|
| 186 | actions.extend(zmi_command_actions(container, insert_actions=True))
|
|---|
| 187 |
|
|---|
| 188 | #-- Sort.
|
|---|
| 189 | actions.sort()
|
|---|
| 190 |
|
|---|
| 191 | #-- Headline.
|
|---|
| 192 | if len(actions) > 0:
|
|---|
| 193 | actions.insert(0,('----- %s -----'%container.getZMILangStr('ACTION_INSERT')%container.display_type(REQUEST),''))
|
|---|
| 194 |
|
|---|
| 195 | # Return action list.
|
|---|
| 196 | return actions
|
|---|
| 197 |
|
|---|
| 198 |
|
|---|
| 199 | def zmi_command_actions(context, insert_actions=False, objPath=''):
|
|---|
| 200 | """
|
|---|
| 201 | Returns list of custom commands.
|
|---|
| 202 | """
|
|---|
| 203 | actions = []
|
|---|
| 204 | if context is None:
|
|---|
| 205 | return actions
|
|---|
| 206 |
|
|---|
| 207 | REQUEST = context.REQUEST
|
|---|
| 208 | auth_user = REQUEST['AUTHENTICATED_USER']
|
|---|
| 209 | absolute_url = '/'.join(list(context.getPhysicalPath())+[''])
|
|---|
| 210 |
|
|---|
| 211 | for metaCmdId in context.getMetaCmdIds():
|
|---|
| 212 | metaCmd = context.getMetaCmd(metaCmdId)
|
|---|
| 213 | if (insert_actions and metaCmd['id'].startswith('manage_add')) or \
|
|---|
| 214 | (not insert_actions and not metaCmd['id'].startswith('manage_add')):
|
|---|
| 215 | canExecute = True
|
|---|
| 216 | if canExecute:
|
|---|
| 217 | hasCustomPermission = _globals.dt_html(context,metaCmd.get('custom',''),REQUEST) != False
|
|---|
| 218 | canExecute = canExecute and hasCustomPermission
|
|---|
| 219 | if canExecute:
|
|---|
| 220 | hasMetaType = context.meta_id in metaCmd['meta_types'] or 'type(%s)'%context.getType() in metaCmd['meta_types']
|
|---|
| 221 | canExecute = canExecute and hasMetaType
|
|---|
| 222 | if canExecute:
|
|---|
| 223 | hasRole = False
|
|---|
| 224 | hasRole = hasRole or len(context.intersection_list(context.getUserRoles(auth_user),metaCmd['roles'])) > 0
|
|---|
| 225 | hasRole = hasRole or auth_user.has_role('Manager')
|
|---|
| 226 | canExecute = canExecute and hasRole
|
|---|
| 227 | if canExecute:
|
|---|
| 228 | nodes = context.string_list(metaCmd.get('nodes','{$}'))
|
|---|
| 229 | sl = []
|
|---|
| 230 | sl.extend(map( lambda x: (context.getHome().id+'/content/'+x[2:-1]+'/').replace('//','/'),filter(lambda x: x.find('@')<0,nodes)))
|
|---|
| 231 | sl.extend(map( lambda x: (x[2:-1].replace('@','/content/')+'/').replace('//','/'),filter(lambda x: x.find('@')>0,nodes)))
|
|---|
| 232 | hasNode = len( filter( lambda x: absolute_url.find(x)>=0, sl)) > 0
|
|---|
| 233 | canExecute = canExecute and hasNode
|
|---|
| 234 | if canExecute:
|
|---|
| 235 | actions.append((metaCmd['name'],objPath+'manage_executeMetacmd'))
|
|---|
| 236 |
|
|---|
| 237 | # Return action list.
|
|---|
| 238 | return actions
|
|---|
| 239 |
|
|---|
| 240 | ################################################################################ |
|---|