-
Notifications
You must be signed in to change notification settings - Fork 0
/
ZMSMetadictManager.py
401 lines (365 loc) · 15.6 KB
/
ZMSMetadictManager.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
# -*- coding: utf-8 -*-
################################################################################
# ZMSMetadictManager.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.
import copy
import sys
import time
# Product Imports.
import standard
################################################################################
################################################################################
###
### Class
###
################################################################################
################################################################################
class ZMSMetadictManager:
############################################################################
#
# IRepositoryProvider
#
############################################################################
"""
@see IRepositoryProvider
"""
def provideRepositoryMetas(self, r, ids=None):
self.writeBlock("[provideRepositoryMetas]: ids=%s"%str(ids))
valid_ids = ['__metas__']
if ids is None:
ids = valid_ids
for id in filter(lambda x:x in valid_ids, ids):
metas = copy.deepcopy(self.metas)
metas = map(lambda x:metas[x*2+1],range(len(metas)/2))
map(lambda x:self.operator_delitem(x,'acquired'),filter(lambda x:x.has_key('acquired'),metas))
d = {'id':id,'__filename__':['__metas__.py'],'Metas':metas}
r[id] = d
"""
@see IRepositoryProvider
"""
def updateRepositoryMetas(self, r):
id = r['id']
if id == '__metas__':
self.writeBlock("[updateRepositoryMetas]: id=%s"%id)
self.metas = []
for attr in r.get('Metas',[]):
self.metas.extend([attr['id'],attr])
# Make persistent.
self.metas = copy.deepcopy(self.metas)
return id
############################################################################
#
# XML IM/EXPORT
#
############################################################################
# --------------------------------------------------------------------------
# ZMSMetadictManager.importMetadictXml
# --------------------------------------------------------------------------
def _importMetadictXml(self, item, createIfNotExists=1):
id = item['id']
if createIfNotExists == 1:
newId = id
newAcquired = 0
newName = item['name']
newType = item['type']
newMandatory = item.get('mandatory',0)
newMultilang = item.get('multilang',1)
newRepetitive = item.get('repetitive',0)
newKeys = item.get('keys',[])
newCustom = item.get('custom','')
newDefault = item.get('default','')
self.setMetadictAttr( None, newId, newAcquired, newName, newType, \
newMandatory, newMultilang, newRepetitive, newCustom, newKeys, newDefault)
for meta_id in item.get('dst_meta_types',[]):
metaObj = self.getMetaobj( meta_id)
if metaObj is not None and id not in self.getMetadictAttrs(meta_id):
self.setMetaobjAttr(meta_id,None,newId=id,newType=id)
def importMetadictXml(self, xml, createIfNotExists=1):
v = self.parseXmlString(xml)
if type(v) is list:
for item in v:
self._importMetadictXml(item,createIfNotExists)
else:
self._importMetadictXml(v,createIfNotExists)
# --------------------------------------------------------------------------
# ZMSMetadictManager.getMetadictAttrs:
#
# Returns list of attributes of DC.Metadictionaries.
# --------------------------------------------------------------------------
def getMetadictAttrs(self, meta_type=None):
obs = self.metas
if meta_type is not None:
attrs = []
metaObj = self.getMetaobj( meta_type)
for attr in metaObj.get('attrs',[]):
if attr['type'] in obs:
attrs.append(attr['type'])
else:
attrs = map( lambda x: obs[x*2], range(len(obs)/2))
# Return attributes.
return attrs
# --------------------------------------------------------------------------
# ZMSMetadictManager.getMetadictAttr:
#
# Get Attribute for Meta-Dictionary specified by key.
# --------------------------------------------------------------------------
def getMetadictAttr(self, key):
obs = self.metas
if key in obs:
ob = obs[obs.index(key)+1].copy()
# Not found!
else:
return None
# Acquire from parent.
if ob.get('acquired',0)==1:
portalMaster = self.getPortalMaster()
if portalMaster is not None:
portalMasterOb = portalMaster.metaobj_manager.getMetadictAttr(key)
if portalMasterOb is not None:
ob = portalMasterOb
ob = ob.copy()
ob['acquired'] = 1
else:
ob = ob.copy()
ob['errors'] = 'Not found in master!'
ob['mandatory'] = ob.get('mandatory',0)
ob['multilang'] = ob.get('multilang',1)
ob['repetitive'] = ob.get('repetitive',0)
ob['keys'] = ob.get('keys',[])
ob['custom'] = ob.get('custom','')
ob['default'] = ob.get('default','')
ob['errors'] = ob.get('errors','')
return ob
# --------------------------------------------------------------------------
# ZMSMetadictManager.delMetadictAttr:
#
# Delete Meta-Attribute specified by ID.
# --------------------------------------------------------------------------
def delMetadictAttr(self, id):
# Delete.
obs = self.metas
i = obs.index(id)
# Update attribute.
del obs[i]
del obs[i]
# Make persistent.
self.metas = copy.deepcopy(self.metas)
# Return with empty ID.
return ''
# --------------------------------------------------------------------------
# ZMSMetadictManager.setMetadictAttr:
# --------------------------------------------------------------------------
def setMetadictAttr(self, oldId, newId, newAcquired, newName='', newType='', \
newMandatory=0, newMultilang=1, newRepetitive=0, newCustom='', \
newKeys=[], newDefault=''):
"""
Set/add meta-attribute with specified values.
@param oldId: Old id
@type oldId: C{string}
@param newId: New id
@type newId: C{string}
@param newAcquired: Acquired
@type newAcquired: C{int}: 0 or 1
@param newName: (Display-)Name
@type newName: C{string}
@param newType: Type
@type newType: C{string}
@return: New id
@rtype: C{string}
"""
obs = self.metas
# Remove exisiting entry.
if oldId is None:
oldId = newId
if oldId in obs:
i = obs.index(oldId)
del obs[i]
del obs[i]
else:
i = len(obs)
# Values.
newValues = {}
newValues['id'] = newId
newValues['acquired'] = newAcquired
newValues['name'] = newName
newValues['type'] = newType
newValues['mandatory'] = newMandatory
newValues['multilang'] = newMultilang
newValues['repetitive'] = newRepetitive
newValues['keys'] = newKeys
newValues['custom'] = newCustom
newValues['default'] = newDefault
# Update attribute.
obs.insert(i,newValues)
obs.insert(i,newId)
# Make persistent.
self.metas = copy.deepcopy(self.metas)
# Return with new attr.
return newId
# --------------------------------------------------------------------------
# ZMSMetadictManager.moveMetadictAttr:
#
# Moves Meta-Attribute specified by given attr to specified position.
# --------------------------------------------------------------------------
def moveMetadictAttr(self, attr, pos):
# Move.
obs = self.metas
i = obs.index(attr)
attr = obs[i]
values = obs[i+1]
del obs[i]
del obs[i]
obs.insert(pos*2, values)
obs.insert(pos*2, attr)
# Make persistent.
self.metas = copy.deepcopy(self.metas)
# Return with empty attr.
return ''
############################################################################
# ZMSMetadictManager.manage_changeMetaProperties:
#
# Change Meta-Attributes.
############################################################################
def manage_changeMetaProperties(self, btn, lang, REQUEST, RESPONSE=None):
""" MetadictManager.manage_changeMetaProperties """
message = ''
extra = {}
t0 = time.time()
id = REQUEST.get('id','')
target = 'manage_metas'
try:
# Acquire.
# --------
if btn == self.getZMILangStr('BTN_ACQUIRE'):
ids = REQUEST.get('aq_ids',[])
for newId in ids:
newAcquired = 1
id = self.setMetadictAttr( None, newId, newAcquired)
message = self.getZMILangStr('MSG_INSERTED')%str(len(ids))
# Change.
# -------
elif btn == self.getZMILangStr('BTN_SAVE'):
for oldId in REQUEST.get('old_ids',[]):
if REQUEST.has_key('attr_id_%s'%oldId):
newId = REQUEST['attr_id_%s'%oldId].strip()
newAcquired = 0
newName = REQUEST['attr_name_%s'%oldId].strip()
newType = REQUEST['attr_type_%s'%oldId].strip()
newMandatory = REQUEST.get('attr_mandatory_%s'%oldId, 0)
newMultilang = REQUEST.get('attr_multilang_%s'%oldId, 0)
newRepetitive = REQUEST.get('attr_repetitive_%s'%oldId, 0)
newKeys = standard.string_list(REQUEST.get('attr_keys_%s'%oldId,''), '\n')
newCustom = REQUEST.get('attr_custom_%s'%oldId, '')
newDefault = REQUEST.get('attr_default_%s'%oldId, '')
self.setMetadictAttr( oldId, newId, newAcquired, newName, newType, newMandatory, newMultilang, newRepetitive, newCustom, newKeys, newDefault)
message += self.getZMILangStr('MSG_CHANGED')
newId = REQUEST['_id'].strip()
newAcquired = 0
newName = REQUEST['_name'].strip()
newType = REQUEST['_type'].strip()
newMandatory = REQUEST.get('_mandatory',0)
newMultilang = REQUEST.get('_multilang',0)
newRepetitive = REQUEST.get('_repetitive',0)
newCustom = ''
if len(newId) > 0 and len(newName) > 0 and len(newType) > 0:
if newType == 'method':
newCustom += '<dtml-comment>--// BO '+ newId + ' //--</dtml-comment>\n'
newCustom += '\n'
newCustom += '<dtml-comment>--// EO '+ newId + ' //--</dtml-comment>\n'
self.setMetadictAttr( None, newId, newAcquired, newName, newType, newMandatory, newMultilang, newRepetitive, newCustom)
message += self.getZMILangStr('MSG_INSERTED')%newId
# Copy.
# -----
elif btn == self.getZMILangStr('BTN_COPY'):
metaOb = self.getMetadictAttr(id)
if metaOb.get('acquired',0) == 1:
masterRoot = getattr(self,self.getConfProperty('Portal.Master'))
masterDocElmnt = masterRoot.content
REQUEST.set('ids',[id])
xml = masterDocElmnt.manage_changeMetaProperties(self.getZMILangStr('BTN_EXPORT'), lang, REQUEST, RESPONSE)
self.importMetadictXml(xml=xml)
message = self.getZMILangStr('MSG_IMPORTED')%('<i>%s</i>'%id)
# Delete.
# -------
elif btn in ['delete',self.getZMILangStr('BTN_DELETE')]:
oldId = id
self.delMetadictAttr( oldId)
for portalClient in self.getPortalClients():
pcmm = portalClient.metaobj_manager
if oldId in pcmm.getMetadictAttrs() and pcmm.getMetadictAttr(oldId).get('acquired',0)==1:
pcmm.delMetadictAttr( oldId)
message = self.getZMILangStr('MSG_DELETED')%int(1)
# Export.
# -------
elif btn == self.getZMILangStr('BTN_EXPORT'):
value = []
ids = REQUEST.get('ids',[])
metadicts = self.metas
for i in range(len(metadicts)/2):
id = metadicts[i*2]
dict = metadicts[i*2+1].copy()
if id in ids or len(ids) == 0:
dst_meta_types = []
for meta_id in self.getMetaobjIds():
if id in self.getMetadictAttrs( meta_id):
dst_meta_types.append( meta_id)
dict['dst_meta_types'] = dst_meta_types
value.append(dict)
if len(value) == 1:
value = value[0]
content_type = 'text/xml; charset=utf-8'
filename = 'export.metadict.xml'
export = self.getXmlHeader() + self.toXmlString(value,1)
RESPONSE.setHeader('Content-Type',content_type)
RESPONSE.setHeader('Content-Disposition','attachment;filename="%s"'%filename)
return export
# Import.
# -------
elif btn == self.getZMILangStr('BTN_IMPORT'):
f = REQUEST['file']
if f:
filename = f.filename
self.importMetadictXml(xml=f)
else:
filename = REQUEST['init']
self.importConf(filename, createIfNotExists=1)
message = self.getZMILangStr('MSG_IMPORTED')%('<i>%s</i>'%filename)
# Move to.
# --------
elif btn == 'move_to':
pos = REQUEST['pos']
oldId = id
id = self.moveMetadictAttr( oldId, pos)
message = self.getZMILangStr('MSG_MOVEDOBJTOPOS')%(("<i>%s</i>"%oldId),(pos+1))
##### SYNCHRONIZE ####
self.synchronizeObjAttrs()
# Handle exception.
except:
standard.writeError(self,"[manage_changeMetaProperties]")
error = str( sys.exc_type)
if sys.exc_value:
error += ': ' + str( sys.exc_value)
target = self.url_append_params( target, { 'manage_tabs_error_message':error})
# Return with message.
target = self.url_append_params( target, { 'lang':lang, 'id':id})
target = self.url_append_params( target, extra)
if len( message) > 0:
message += ' (in '+str(int((time.time()-t0)*100.0)/100.0)+' secs.)'
target = self.url_append_params( target, { 'manage_tabs_message':message})
return RESPONSE.redirect( target)
################################################################################