forked from Immudzen/CompoundDoc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
base.py
415 lines (359 loc) · 16.5 KB
/
base.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
# -*- coding: utf-8 -*-
#This software is released under GNU public license. See details in the URL:
#http://www.gnu.org/copyleft/gpl.html
from getset import GetSet
from widgets import Widgets
import baseobject
import utility
import mixobjectloadstore
import mixconfigobject
import mixupgrader
import mixobserver
from OFS.SimpleItem import SimpleItem
from OFS.ObjectManager import ObjectManager
from mixaddregistered import MixAddRegistered
from mixobjectmanager import MixObjectManager
from AccessControl import getSecurityManager
from AccessControl import Unauthorized
from Acquisition import aq_base
#For Security control and init
from AccessControl import ClassSecurityInfo
import Globals
import manageEditForm
import urllib
import nestedlisturl as NestedListUrl
import pprint
import os.path
class Base(baseobject.BaseObject,
GetSet,
Widgets,
mixupgrader.MixUpgrader,
mixobjectloadstore.MixObjectLoadStore,
mixconfigobject.MixConfigObject,
mixobserver.MixObserver,
MixAddRegistered,
MixObjectManager,
SimpleItem):
"Abstract Base"
classVersion = 177 #run upgrader scripts 160
classUpdateVersion = 122 #add attributes
overwrite = 0
updateAlways = 0
meta_type = "Base"
security = ClassSecurityInfo()
drawDict = {'view':'view', 'edit':'edit', 'config':'config', 'debug':'debugObject'}
manage = manage_main = manage_edit_form = manage_workspace = manageEditForm.ManageEditForm('manage_edit_form')
attr_notified = None
manage_options = (
{'label': 'Edit', 'action': 'manage_workspace'},
{'label': 'View', 'action': 'index_html'},
{'label': 'Inline', 'action': 'inlineView'},)+ SimpleItem.manage_options
security.declareProtected("Access contents information", 'objectItems')
objectItems = ObjectManager.objectItems
security.declareProtected("Access contents information", 'objectValues')
objectValues = ObjectManager.objectValues
security.declareProtected("Access contents information", 'objectIds')
objectIds = ObjectManager.objectIds
security.declareProtected("Access contents information", 'objectIdsWithMetaType')
def objectIdsWithMetaType(self, spec=None):
# Returns a list of subobject ids of the current object.
# If 'spec' is specified, returns objects whose meta_type
# matches 'spec'.
if spec is not None:
if type(spec)==type('s'):
spec=[spec]
set = ((obj['id'], obj['meta_type']) for obj in self._objects)
return [(name, meta_type) for name,meta_type in set if meta_type in spec]
return [(obj['id'], obj['meta_type']) for obj in self._objects]
security.declareProtected('Change permissions', 'manage_access')
security.declareProtected('Take ownership', 'manage_takeOwnership','manage_changeOwnershipType')
security.declareProtected('View management screens', 'manage_owner',
'manage_UndoForm', 'manage_page_style_css', 'manage_page_header',
'manage_page_footer', 'manage', 'manage_edit_form', 'manage_main','manage_workspace')
security.declarePrivate('atomicSubmit')
def atomicSubmit(self):
"this is used in editing of subparts"
return self.submitChanges()
security.declareProtected('CompoundDoc: Manage Config', 'manage_config')
def manage_config(self):
"draw the config panel"
return ''.join([self.begin_manage(), self.submitChanges(), self.config(), self.manage_page_footer()])
security.declareProtected('View', "__str__")
def __str__(self):
"str rep of object"
return str(self.view())
def manage_tabs(self):
"return the tabbed interface for editing this object"
if 'Manager' in getSecurityManager().getUser().getRoles():
format = '%s<div class="ui-widget ui-widget-content ui-corner-all">%s at %s</div>'
return format % (self.manage_absolute_urls(), self.meta_type, self.tabs_path_default())
else:
return ''
def tabs_path_default(self, unquote=urllib.unquote):
"Custom tabs_path_default for creating the white on black interface"
#All forms items that compounddoc has to process consists of absolute urls to the db
#so if there are no / in the form keys then there is nothing to process
if self.REQUEST.form and not 'CompoundDocProcessed' in self.REQUEST.other:
self.manage_edit()
url = self.REQUEST.other.get('redirectTo', None)
if url is not None:
return self.REQUEST.RESPONSE.redirect(url)
linkpat = '<a href="%s/manage_main">%s</a>\n'
temp = ''
out = [linkpat % ('', '/')]
for i in self.absolute_url(1).split('/'):
temp = temp + '/' + i
out.append(linkpat % (temp, i))
out.append('/')
return ''.join(out)
def manage_absolute_urls(self, REQUEST={}):
"create an absolute url sequence for management"
url = self.absolute_url_path()
manageTab = self.REQUEST.URLPATH0.replace('%s/' % url, '').split('/')[0]
cssClasses = ''
otherAttributes = ''
queryDict = utility.getQueryDict(self.REQUEST.environ.get('QUERY_STRING', ''))
menu = []
for i in self.filtered_manage_options():
selected = i['action'] == manageTab
menu.append((os.path.join(url, i['action']),i['label'],selected,cssClasses,queryDict,otherAttributes))
return NestedListUrl.listRenderer('Themeroller Tabs',menu, None)
security.declareProtected('View', "__call__")
def __call__(self, client=None, REQUEST={}, RESPONSE=None, name=None, mode=None, **kw):
"call rep of object"
if mode is None:
mode = self.getDrawMode()
obj = self.draw(mode, **kw)
if mode == 'view':
return self.gen_html(obj)
return obj
security.declareProtected('View', 'inlineView')
def inlineView(self):
"Inline view of document"
return '%s%s%s' % (self.begin_manage(), self.gen_html(self.view()), self.manage_page_footer())
security.declarePrivate('eventProfileLoad')
def eventProfileLoad(self, id, content):
"Handle an ProfileLoad event"
if self.overwrite or (hasattr(aq_base(self), id) and not getattr(self, id) and content):
self.setObject(id, content)
else:
self.updateObject(id, content)
security.declarePrivate("removeUnNeeded")
def removeUnNeeded(self, keeplist, typekeeplist):
"Remove the UnNeeded items based on this list"
idsKeep = self.objectIds(typekeeplist) + keeplist
self.delObjects([id for id in self.objectIds() if id not in idsKeep])
security.declarePrivate('before_manage_edit')
def before_manage_edit(self, dict):
"before editing"
pass
security.declarePrivate('after_manage_edit')
def after_manage_edit(self, dict):
"before editing"
pass
security.declareProtected('Change CompoundDoc','manage_edit')
def manage_edit(self, dict=None):
"Process edits."
self.before_manage_edit(dict)
self.objectSpecificEdit(dict)
self.after_manage_edit(dict)
self.notifyObserversSyncDB()
self.add_delete_objects(dict)
security.declarePrivate('alternate_manage_edit')
def alternate_manage_edit(self, dict=None):
"Process edits."
alt = getattr(self, 'alternate_security_edit', None)
if alt is not None:
try:
if getSecurityManager().validate(None, self, 'alternate_security_edit', alt):
alt(dict)
except Unauthorized:
pass
for key, value in dict.iteritems():
if hasattr(aq_base(self), key):
object = getattr(self, key)
if hasattr(aq_base(object), 'alternate_manage_edit'):
object.alternate_manage_edit(dict = value)
security.declarePrivate('objectSpecificEdit')
def objectSpecificEdit(self, dict):
"This is the object specific edit stuff"
for i, j in dict.items():
if hasattr(aq_base(self), i):
object = getattr(self, i)
if hasattr(aq_base(object), 'manage_edit'):
object.manage_edit(dict = dict[i])
else:
self.setObject(i, j)
security.declarePrivate('notifyObserversSyncDB')
def notifyObserversSyncDB(self):
"notify all observers and set the object as changed"
if self.hasBeenModified():
self.updateBoboBaseModificationTime()
self.observerNotify()
if self.meta_type != 'CompoundDoc':
self.runChangeNotificationScript()
security.declarePrivate('runChangeNotificationScript')
def runChangeNotificationScript(self):
"run the script if we have one to notify of changes"
scriptPath = self.getConfig('scriptChangePath')
if scriptPath and self.REQUEST.other.get('okayToRunNotification', 1):
script = self.restrictedTraverse(scriptPath, None)
#self.log("I would have run")
if script is not None:
script(self)
security.declarePrivate('hasBeenModified')
def hasBeenModified(self):
"check if this item has been modified in the current transaction"
return hasattr(self, 'REQUEST') and aq_base(self) in self.REQUEST.other.get('modifiedPaths', [])
security.declarePrivate('performUpdate')
def performUpdate(self):
"Update object to newest version"
self.gen_vars()
self.delayed_gen_vars()
for item in self.__dict__.values():
if hasattr(aq_base(item), 'performUpdate'):
item.__of__(self).performUpdate()
security.declarePrivate('performUpgrade')
def performUpgrade(self):
"perform the upgrade on this object and its children"
self.upgrader()
for key in self.__dict__.keys():
item = getattr(self,key)
if hasattr(aq_base(item), 'performUpgrade'):
item.performUpgrade()
security.declareProtected('View', "index_html")
def index_html(self, mode='view'):
"Basic View Alone"
return self(mode=mode)
security.declarePrivate('gen_vars')
def gen_vars(self):
"Generate variables also used in update."
if hasattr(aq_base(self), 'instance'):
for name, value in self.instance:
if name not in self.__dict__:
self.updateObject(name, value)
security.declarePrivate('delayed_gen_vars')
def delayed_gen_vars(self):
"These are vars that can not be generated until after the first update pulse goes through"
if hasattr(aq_base(self), 'delayedInstance'):
for name, value in self.delayedInstance:
if name not in self.__dict__:
self.updateObject(name, value)
for i in self.aq_parent.objectValues(['CreationAssistant', 'CreationAssistantString', 'CreationAssistantDTML']):
if self.meta_type == i.metaType:
i.setupObject(self)
security.declarePrivate('gen_html')
def gen_html(self, data):
"Interpret DTML and convert to HTML"
if callable(data):
data = data.__of__(self)()
try:
if 'dtml' in data:
return Globals.HTML(data, globals())(self, self.REQUEST)
except TypeError:
pass
return data
security.declareProtected('View', 'getSecurityMode')
def getSecurityMode(self):
"select which mode we should be using between edit and view based on security"
if getSecurityManager().checkPermission('Change CompoundDoc', self):
return 'edit'
return 'view'
security.declarePublic('draw')
def draw(self, renderType='view', **kw):
"rendering dispatcher"
renderName = self.drawDict.get(renderType, None)
if renderName is not None:
render = self.restrictedTraverse(renderName, None)
if render is not None:
return render(**kw)
return ''
security.declareProtected('View', 'view')
def view(self):
"default view that just returns a blank string"
return ''
security.declareProtected('View management screens', 'edit')
def edit(self):
"default edit that just returns a blank string"
return ''
security.declarePrivate("getDictName")
def getDictName(self):
"Return the names of these items in a list but formatted for the form output filter"
#remove the path to this compounddoc from the path to the object in it
cdocPath = self.getCompoundDoc().getPhysicalPath()
myPath = list(self.getPhysicalPath()[len(cdocPath):])
return [cdocPath, myPath]
security.declareProtected('View management screens', 'debugObject')
def debugObject(self, **kw):
"Get the info for this object and stick it on this list"
self.REQUEST.RESPONSE.setHeader('Content-Type', 'text/html; charset=%s' % self.getEncoding())
temp = []
temp.append('<p>I am: %s</p>' % self.getId())
if hasattr(self, 'profile') and self.meta_type == 'CompoundDoc':
temp.append('<p>My profile is: %s</p>' % self.profile)
temp.append('<p>My type is: %s</p>' % self.meta_type)
#Security reasons not sure what to do with this entirely yet but I think it is mostly obsolete
temp.append('<pre>%s</pre>' % self.convert(pprint.pformat(self.__dict__)))
return ''.join(temp)
security.declarePrivate('getRemoteObject')
def getRemoteObject(self, path, meta_type = ''):
"Get the remote image"
if path:
item = self.restrictedTraverse(path, default=None)
if item is None:
item = self.getCompoundDoc().restrictedTraverse(path, default=None)
if item is None:
item = self.getCompoundDocContainer().restrictedTraverse(path, default=None)
if item is not None and item.meta_type == meta_type:
return item
security.declarePrivate('restrictedUserObject')
def restrictedUserObject(self):
"Return a list of the types that are allowed to be added or deleted from this object by a user"
available =[''] + self.availableObjects()
noremove = self.noremove()
return [i for i in available if i not in noremove]
security.declarePrivate('updateBoboBaseModificationTime')
def updateBoboBaseModificationTime(self):
"""Update the bobobase_modification_time of all parents up to the main compounddoc
of a change if we are newer then our parent"""
for i in self.aq_chain:
if self._p_mtime > i._p_mtime:
i._p_changed=1
if getattr(aq_base(i), 'meta_type', None) == 'CompoundDoc':
break
security.declarePrivate('noCacheHeader')
def noCacheHeader(self):
"set the headers so this item will not be cached needed becuase the same url can map to multiple objects"
#self.REQUEST.RESPONSE.setHeader('Expires', '-1')
self.REQUEST.RESPONSE.setHeader('Cache-Control', 'no-cache')
self.REQUEST.RESPONSE.setHeader('Pragma', 'no-cache')
#self.REQUEST.RESPONSE.setHeader('Cache-Disposition', 'private')
security.declarePrivate('issueEventProfileLast')
def issueEventProfileLast(self):
"call the eventProfileLast on every sub object"
temp = [object for object in self.objectValues() if hasattr(aq_base(object), 'eventProfileLast')]
for i in temp:
i.eventProfileLast()
for i in temp:
i.issueEventProfileLast()
security.declarePublic('getUniqueMetaTypes')
def getUniqueMetaTypes(self):
"return all the unique meta_types found in this object"
meta_types = set([self.meta_type])
for obj in self.objectValues():
if obj.hasObject('getUniqueMetaTypes'):
meta_types.add(obj.getUniqueMetaTypes())
return meta_types
security.declarePrivate('setSelectedDocument')
def setSelectedDocument(self, selected):
"set the selected document to redirect to"
baseURL = self.REQUEST.other['URL']
queryString = self.REQUEST.environ.get('QUERY_STRING', '')
if selected:
url = baseURL + '?' + utility.updateQueryString(queryString, {'selectedDocument': selected})
else:
url = baseURL + '?' + queryString
urlFrom = self.REQUEST.other.get('HTTP_REFERRER', '')
if urlFrom != url:
self.REQUEST.other['redirectTo'] = url
Globals.InitializeClass(Base)