-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcalculatorwatcher.py
80 lines (71 loc) · 3.52 KB
/
calculatorwatcher.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
###########################################################################
# Copyright (C) 2003 by kosh
#
# Copyright: See COPYING file that comes with this distribution
#
###########################################################################
from base import Base
#For Security control and init
from AccessControl import ClassSecurityInfo
import Globals
class CalculatorWatcher(Base):
"watch a calculator container for changes and apply them internally when they do"
meta_type = "CalculatorWatcher"
security = ClassSecurityInfo()
overwrite=1
calculatorPath = ''
creationType = 'Money'
classConfig = {}
classConfig['creationType'] = {'name':'creationType', 'type':'list', 'values': ['Money','InputInt','InputFloat']}
classConfig['calculatorPath'] = {'name':'Location of CalculatorContainer object:', 'type':'string'}
#TEST: See if calculator picks up changes in its config
def after_manage_edit(self, dict):
"Process edits."
object = self.getRemoteObject(self.calculatorPath, 'CalculatorContainer')
if object is not None:
self.observerUpdate()
object.observerAttached(self)
security.declarePrivate('observerUpdate')
def observerUpdate(self, object=None):
"Process what you were observing"
calculatorcontainer = self.getRemoteObject(self.calculatorPath, 'CalculatorContainer')
if calculatorcontainer is not None:
creationType = self.creationType
order = calculatorcontainer.objectOrder
items = self.objectItems(['Money', 'InputFloat', 'InputInt'])
list = [id for id, object in items if id not in order or object.meta_type != creationType]
self.delObjects(list)
for id in calculatorcontainer.objectOrder:
if not self.hasObject(id):
self.addRegisteredObject(id, self.creationType)
for id in calculatorcontainer.objectOrder:
calculator = getattr(calculatorcontainer, id)
calculated = getattr(self, id)
calculated.setCalculationValue(calculator.calculate(self))
paths = [i.getPhysicalPath() for i in self.objectValues(self.creationType)]
for id in calculatorcontainer.objectOrder:
traversedObject = getattr(calculatorcontainer, id).getTraversedObject(self)
if traversedObject is not None and traversedObject.getPhysicalPath() not in paths:
traversedObject.observerAttached(self)
security.declareProtected('View management screens', 'edit')
def edit(self, *args, **kw):
"Inline edit view"
temp = []
format = "<p>ID: %s %s</p>"
if self.calculatorPath:
calculatorContainer = self.getRemoteObject(self.calculatorPath, 'CalculatorContainer')
if calculatorContainer is not None:
for id in calculatorContainer.objectOrder:
object = getattr(self, id, None)
if object is not None:
temp.append(format % (object.getId(), object.edit()))
return ''.join(temp)
security.declarePrivate('eventProfileLast')
def eventProfileLast(self):
"run this event as the last thing the object will do before the profile is returned"
self.delObjects([id for id in self.objectIds(self.creationType)]+['observed'])
self.observerUpdate()
Globals.InitializeClass(CalculatorWatcher)
import register
register.registerClass(CalculatorWatcher)