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