-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcalculator.py
93 lines (79 loc) · 3.32 KB
/
calculator.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
###########################################################################
# Copyright (C) 2003 by kosh
#
# Copyright: See COPYING file that comes with this distribution
#
###########################################################################
#This software is released under GNU public license. See details in the URL:
#http://www.gnu.org/copyleft/gpl.html
#For Security control and init
from AccessControl import ClassSecurityInfo
import Globals
from userobject import UserObject
class Calculator(UserObject):
"Calculator class basic ops are defined for an object to be used elsewhere"
security = ClassSecurityInfo()
meta_type = "Calculator"
operator = ''
ammount = 0.00
objectPath = ''
operatorMapping = {'Add/Subtract Percent':'percentAddSubtract',
'Add/Subtract Ammount':'ammountAddSubtract',
'Multiply by Ammount':'ammountMultiply'}
security.declareProtected('View management screens', 'edit')
def edit(self, *args, **kw):
"Inline edit short object"
temp = ['<div>']
temp.append('ID: %s Path:' % self.getId())
temp.append(self.input_text('objectPath', self.objectPath))
temp.append(self.option_select(self.operatorList(), 'operator', [self.operator]))
temp.append('Ammount:')
temp.append(self.input_float('ammount', self.ammount))
temp.append('</div>')
return ''.join(temp)
security.declarePrivate('calculate')
def calculate(self, remote):
"calculate this object"
if self.operator and self.ammount and self.objectPath:
return getattr(self, self.operatorMapping[self.operator])(remote)
return 0.00
security.declarePrivate('operatorList')
def operatorList(self):
"return the list of operators sorted"
return sorted(self.operatorMapping.keys())
security.declarePrivate('percentAddSubtract')
def percentAddSubtract(self, remote):
"add/subtract a percent to the object and return it"
return self.getTraversedObjectValue(remote)*(self.ammount+100.0)/100.0
security.declarePrivate('ammountAddSubtract')
def ammountAddSubtract(self, remote):
"add/subtract an absolute ammount to this item"
return self.getTraversedObjectValue(remote) + self.ammount
security.declarePrivate('ammountMultiply')
def ammountMultiply(self, remote):
"multiply the item by this ammount"
return self.getTraversedObjectValue(remote) * self.ammount
security.declarePrivate('getTraversedObjectValue')
def getTraversedObjectValue(self, remote):
"return the floating point value of the returned object"
object = self.getTraversedObject(remote)
if object is None:
return 0.0
if object.hasObject('float'):
return object.float()
elif callable(object):
return float(object())
else:
return float(object)
security.declarePrivate('getTraversedObject')
def getTraversedObject(self, remote):
"get the remote object and return it"
if not self.objectPath:
return None
object = remote.restrictedTraverse(self.objectPath)
if object is not None:
return object
Globals.InitializeClass(Calculator)
import register
register.registerClass(Calculator)