-
Notifications
You must be signed in to change notification settings - Fork 0
/
_metacmdmanager.py
504 lines (444 loc) · 18.2 KB
/
_metacmdmanager.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
################################################################################
# _metacmdmanager.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.
################################################################################
# Imports.
from App.special_dtml import HTMLFile
from Products.PythonScripts import PythonScript
import copy
import string
import urllib
# Product Imports.
import _globals
import _objattrs
# Example code.
# -------------
dtmlMethodWithExecExampleCode = \
'<dtml-comment>\n' + \
'# Example code:\n' + \
'</dtml-comment>\n' + \
'\n' + \
'<dtml-call expr="REQUEST.set(\'message\',\'This is %s\'%meta_type)">\n' + \
'<dtml-return message>\n' + \
''
dtmlMethodWithoutExecExampleCode = \
'<dtml-comment>\n' + \
'# Example code:\n' + \
'</dtml-comment>\n' + \
'\n' + \
'<dtml-var manage_page_header>\n' + \
'<head>\n' + \
' <title>$$NAME$$</title>\n' + \
'</head>\n' + \
'<dtml-var "manage_tabs(_,_,my_manage_options=[{\'label\':\'$$NAME$$\',\'action\':\'\'}])">\n' + \
'<dtml-var manage_page_header>\n' + \
''
pageTemplateExampleCode = \
''
pyScriptExampleCode = \
'# Example code:\n' + \
'\n' + \
'# Import a standard function, and get the HTML request and response objects.\n' + \
'from Products.PythonScripts.standard import html_quote\n' + \
'request = container.REQUEST\n' + \
'RESPONSE = request.RESPONSE\n' + \
'\n' + \
'# Return a string identifying this script.\n' + \
'print "This is the", script.meta_type, \'"%s"\' % script.getId(),\n' + \
'if script.title:\n' + \
' print "(%s)" % html_quote(script.title),\n' + \
'print "in", container.absolute_url()\n' + \
'return printed\n' + \
''
"""
################################################################################
#
# K E Y S
#
################################################################################
"""
CONF_METACMDS = "ZMS.custom.commands"
"""
################################################################################
#
# X M L I M / E X P O R T
#
################################################################################
"""
# ------------------------------------------------------------------------------
# _metacmdmanager.importXml
# ------------------------------------------------------------------------------
def _importXml(self, item, zms_system=0, createIfNotExists=1):
id = item['id']
metaCmds = getRawMetacmds(self)
metaCmds = filter(lambda x: x.get('zms_system',0)==1,metaCmds)
ids = map(lambda x: x['id'], metaCmds)
if createIfNotExists == 1 or id in ids:
# Delete existing object.
try: delMetacmd(self, id)
except: pass
# Initialize attributes of new object.
newId = id
newAcquired = 0
newName = item['name']
newMethod = item['meta_type']
newExec = item.has_key('exec') and item['exec']
newDescription = item['description']
newIconClazz = item.get('icon_clazz','')
newMetaTypes = item['meta_types']
newRoles = item['roles']
newNodes = item.get('nodes','{$}')
newData = item['data']
# Return with new id.
return setMetacmd(self, None, newId, newAcquired, newName, newMethod, \
newData, newExec, newDescription, newIconClazz, newMetaTypes, newRoles, \
newNodes, zms_system)
def importXml(self, xml, REQUEST=None, zms_system=0, createIfNotExists=1):
v = self.parseXmlString(xml)
if type(v) is list:
for item in v:
id = _importXml(self,item,zms_system,createIfNotExists)
else:
id = _importXml(self,v,zms_system,createIfNotExists)
# ------------------------------------------------------------------------------
# _metacmdmanager.getRawMetacmds:
#
# Returns raw list of actions.
# ------------------------------------------------------------------------------
def getRawMetacmds(self):
return self.getConfProperty(CONF_METACMDS,[])
# ------------------------------------------------------------------------------
# _metacmdmanager.delMetacmd:
#
# Delete Action specified by given Id.
# ------------------------------------------------------------------------------
def delMetacmd(self, id):
# Catalog.
obs = copy.deepcopy(getRawMetacmds(self))
old = filter(lambda x: x['id']==id, obs)
if len(old) > 0:
obs.remove(old[0])
self.setConfProperty( CONF_METACMDS, obs)
# Remove Template.
self.manage_delObjects(ids=[id])
# Return with empty id.
return ''
# ------------------------------------------------------------------------------
# _metacmdmanager.setMetacmd:
#
# Set/add Action specified by given Id.
# ------------------------------------------------------------------------------
def setMetacmd(self, id, newId, newAcquired, newName='', newMethod=None, \
newData=None, newExec=0, newDescription='', newIconClazz='', newMetaTypes=[], \
newRoles=['ZMSAdministrator'], newNodes='{$}', zms_system=0):
obs = copy.deepcopy(getRawMetacmds(self))
# Catalog.
old = filter(lambda x: x['id'] in [id, newId], obs)
if len(old) > 0:
obs.remove(old[0])
# Values.
new = {}
new['id'] = newId
new['acquired'] = newAcquired
new['name'] = newName
new['description'] = newDescription
new['icon_clazz'] = newIconClazz
new['meta_types'] = newMetaTypes
new['roles'] = newRoles
new['nodes'] = newNodes
new['exec'] = newExec
new['zms_system'] = zms_system
obs.append(new)
self.setConfProperty( CONF_METACMDS, obs)
# Insert Template.
if id is None:
newTitle = '*** DO NOT DELETE OR MODIFY ***'
if newAcquired:
portalMaster = self.getPortalMaster()
if portalMaster is not None:
newMethod = getattr(portalMaster,newId).meta_type
if newId in self.objectIds():
self.manage_delObjects(ids=[newId])
if newMethod == 'DTML Method':
self.manage_addDTMLMethod(newId,newTitle)
if newData is None:
if newExec:
newData = dtmlMethodWithExecExampleCode
else:
newData = dtmlMethodWithoutExecExampleCode
newData = newData.replace('$$NAME$$',newName)
elif newMethod == 'DTML Document':
self.manage_addDTMLDocument(newId,newTitle)
if newData is None: newData = dtmlMethodExampleCode
elif newMethod == 'Page Template':
self.manage_addProduct['PageTemplates'].manage_addPageTemplate(id=newId,title=newTitle,text=pageTemplateExampleCode)
elif newMethod == 'Script (Python)':
PythonScript.manage_addPythonScript(self,newId)
if newData is None: newData = pyScriptExampleCode
# Rename Template.
elif id != newId:
self.manage_renameObject(id=id,new_id=newId)
# Update Template.
ob = getattr(self,newId,None)
if ob is not None:
if newAcquired:
newData = ''
if newData is not None:
if ob.meta_type in ['DTML Method','DTML Document']:
ob.manage_edit(title=ob.title,data=newData)
elif ob.meta_type == 'Page Template':
ob.pt_edit(newData,content_type=ob.content_type)
elif ob.meta_type == 'Script (Python)':
ob.write(newData)
# Return with new id.
return newId
################################################################################
################################################################################
###
### class MetacmdObject
###
################################################################################
################################################################################
class MetacmdObject:
############################################################################
# MetacmdObject.manage_executeMetacmd:
#
# Execute Meta-Command.
############################################################################
def manage_executeMetacmd(self, custom, lang, REQUEST, RESPONSE):
""" MetacmdObject.manage_executeMetacmd """
message = ''
target = self
# Execute.
# --------
for metaCmdId in self.getMetaCmdIds():
metaCmd = self.getMetaCmd(metaCmdId)
if metaCmd['name'] == custom:
# Acquire from parent.
if metaCmd.get('acquired',0) == 1:
portalMaster = self.getPortalMaster()
if portalMaster is not None:
masterDtmlMthd = getattr(portalMaster,metaCmd['id'])
ob = getattr(self,metaCmd['id'])
if ob.meta_type in [ 'DTML Method', 'DTML Document']:
newData = masterDtmlMthd.raw
ob.manage_edit(title=ob.title,data=newData)
elif ob.meta_type in [ 'Page Template']:
newData = masterDtmlMthd.read()
newContentType = masterDtmlMthd.content_type
ob.pt_edit(newData,content_type=newContentType)
elif ob.meta_type in [ 'Script (Python)']:
newData = masterDtmlMthd.read()
ob.ZPythonScript_setTitle( ob.title)
ob.write(newData)
# Execute directly.
if metaCmd.get('exec',0) == 1:
ob = getattr(self,metaCmd['id'],None)
if ob.meta_type in ['DTML Method','DTML Document']:
value = ob(self,REQUEST,RESPONSE)
elif ob.meta_type == 'Page Template':
value = ob()
elif ob.meta_type == 'Script (Python)':
value = ob()
if type(value) is str:
message = value
elif type(value) is tuple:
target = value[0]
message = value[1]
# Execute redirect.
else:
params = {'lang':REQUEST.get('lang'),'id_prefix':REQUEST.get('id_prefix'),'ids':REQUEST.get('ids',[])}
return RESPONSE.redirect(self.url_append_params(metaCmd['id'],params,sep='&'))
# Return with message.
message = urllib.quote(message)
return RESPONSE.redirect('%s/manage_main?lang=%s&manage_tabs_message=%s'%(target.absolute_url(),lang,message))
################################################################################
################################################################################
###
### class MetacmdManager
###
################################################################################
################################################################################
class MetacmdManager:
def getMetaCmdDescription(self, id=None, name=None):
"""
Returns description of meta-command specified by ID.
"""
return self.getMetaCmd(id,name).get('description','')
# --------------------------------------------------------------------------
# MetacmdManager.getMetaCmd
# --------------------------------------------------------------------------
def getMetaCmd(self, id=None, name=None):
obs = []
for x in getRawMetacmds(self):
# Acquire from parent.
if x.get('acquired',0)==1:
portalMaster = self.getPortalMaster()
if portalMaster is not None:
x = portalMaster.getMetaCmd(x['id'])
x['acquired'] = 1
else:
x = x.copy()
obs.append(x)
# Filter by Id.
if id is not None:
obs = filter(lambda x: x['id']==id, obs)
# Filter by Name.
if name is not None:
obs = filter(lambda x: x['name']==name, obs)
# Not found!
if len(obs) == 0:
return None
ob = obs[0]
ob['meta_type'] = getattr(self,ob['id']).meta_type
return ob
# --------------------------------------------------------------------------
# MetacmdManager.getMetaCmdIds
#
# Returns list of action-Ids.
# --------------------------------------------------------------------------
def getMetaCmdIds(self, sort=1):
obs = getRawMetacmds(self)
if sort:
obs = map(lambda x: self.getMetaCmd(x['id']), obs)
obs = filter( lambda x: x is not None, obs)
obs = map(lambda x: (x['name'],x), obs)
obs.sort()
obs = map(lambda x: x[1], obs)
ids = map(lambda x: x['id'], obs)
return ids
############################################################################
# MetacmdManager.manage_changeMetacmds:
#
# Change Meta-Commands.
############################################################################
def manage_changeMetacmds(self, btn, lang, REQUEST, RESPONSE):
""" MetacmdManager.manage_changeMetacmds """
message = ''
id = REQUEST.get('id','')
# Acquire.
# --------
if btn == self.getZMILangStr('BTN_ACQUIRE'):
newId = REQUEST['aq_id']
newAcquired = 1
id = setMetacmd(self, None, newId, newAcquired)
message = self.getZMILangStr('MSG_INSERTED')%id
# Change.
# -------
elif btn == self.getZMILangStr('BTN_SAVE'):
id = REQUEST['id']
newId = REQUEST['el_id']
newAcquired = 0
newName = REQUEST.get('el_name','').strip()
newMethod = None
newData = REQUEST.get('el_data','').strip()
newExec = REQUEST.get('el_exec',0)
newDescription = REQUEST.get('el_description','').strip()
newIconClazz = REQUEST.get('el_icon_clazz','')
newMetaTypes = REQUEST.get('el_meta_types',[])
newRoles = REQUEST.get('el_roles',[])
newNodes = REQUEST.get('el_nodes','')
id = setMetacmd(self, id, newId, newAcquired, newName, newMethod, \
newData, newExec, newDescription, newIconClazz, \
newMetaTypes, newRoles, newNodes)
message = self.getZMILangStr('MSG_CHANGED')
# Copy.
# -----
elif btn == self.getZMILangStr('BTN_COPY'):
metaOb = self.getMetaCmd(id)
if metaOb.get('acquired',0) == 1:
portalMaster = self.getPortalMaster()
if portalMaster is not None:
REQUEST.set('ids',[id])
xml = portalMaster.manage_changeMetacmds(self.getZMILangStr('BTN_EXPORT'), lang, REQUEST, RESPONSE)
importXml(self,xml=xml)
message = self.getZMILangStr('MSG_IMPORTED')%('<i>%s</i>'%id)
# Delete.
# -------
elif btn == self.getZMILangStr('BTN_DELETE'):
if id:
ids = [id]
else:
ids = REQUEST.get('ids',[])
for id in ids:
delMetacmd(self,id)
id = ''
message = self.getZMILangStr('MSG_DELETED')%len(ids)
# Export.
# -------
elif btn == self.getZMILangStr('BTN_EXPORT'):
value = []
ids = REQUEST.get('ids',[])
for metaCmdId in self.getMetaCmdIds():
if metaCmdId in ids or len(ids) == 0:
metaCmd = self.getMetaCmd(metaCmdId)
# Catalog.
el_id = metaCmdId
el_name = metaCmd['name']
el_description = metaCmd['description']
el_icon_clazz = metaCmd.get('icon_clazz','')
el_meta_types = metaCmd['meta_types']
el_roles = metaCmd['roles']
el_exec = metaCmd['exec']
# Object.
ob = getattr(self,metaCmdId)
el_meta_type = ob.meta_type
if ob.meta_type in ['DTML Method','DTML Document']:
el_data = ob.raw
elif ob.meta_type in ['Page Template']:
el_data = ob.read()
elif ob.meta_type in ['Script (Python)']:
el_data = ob.body()
# Value.
value.append({'id':el_id,'name':el_name,'description':el_description,'meta_types':el_meta_types,'roles':el_roles,'exec':el_exec,'icon_clazz':el_icon_clazz,'meta_type':el_meta_type,'data':el_data})
# XML.
if len(value)==1:
value = value[0]
filename = '%s.metacmd.xml'%value['id']
else:
filename = 'export.metacmd.xml'
content_type = 'text/xml; charset=utf-8'
export = self.getXmlHeader() + self.toXmlString(value,1)
RESPONSE.setHeader('Content-Type',content_type)
RESPONSE.setHeader('Content-Disposition','inline;filename="%s"'%filename)
return export
# Import.
# -------
elif btn == self.getZMILangStr('BTN_IMPORT'):
f = REQUEST['file']
if f:
filename = f.filename
importXml(self,xml=f)
else:
filename = REQUEST['init']
createIfNotExists = 1
self.importConf(filename, REQUEST, createIfNotExists)
message = self.getZMILangStr('MSG_IMPORTED')%('<i>%s</i>'%f.filename)
# Insert.
# -------
elif btn == self.getZMILangStr('BTN_INSERT'):
newId = REQUEST.get('_id').strip()
newAcquired = 0
newName = REQUEST.get('_name').strip()
newMethod = REQUEST.get('_type','DTML Method')
newData = None
newExec = REQUEST.get('_exec',0)
id = setMetacmd(self, None, newId, newAcquired, newName, newMethod, newData, newExec)
message = self.getZMILangStr('MSG_INSERTED')%id
# Return with message.
message = urllib.quote(message)
return RESPONSE.redirect('manage_customizeMetacmdForm?lang=%s&manage_tabs_message=%s&id=%s'%(lang,message,id))
################################################################################