-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdatafilter.py
186 lines (154 loc) · 6.67 KB
/
datafilter.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
# -*- coding: utf-8 -*-
from userobject import UserObject
#For Security control and init
from AccessControl import ClassSecurityInfo
import Globals
import utility
import com.db
import BTrees.OOBTree
class DataFilter(UserObject):
"DataFilter apply this to an a list of dicts to change how it outputs"
meta_type = "DataFilter"
security = ClassSecurityInfo()
fieldMap = None
order = None
visible = None
updateReplaceList = ('order', 'visible')
security.declarePrivate('PrincipiaSearchSource')
def PrincipiaSearchSource(self):
"This is the basic search function"
return ""
security.declarePrivate('configAddition')
def configAddition(self):
"addendum to the default config screen"
temp = []
append = temp.append
if self.fieldMap is not None and self.order is not None:
fieldMapKeys = sorted(self.fieldMap.keys())
dictMap = "fieldMap.%s"
orderMap = "order.%s"
elements = [['id','name','order']]
for i in fieldMapKeys:
mappedEdit = self.input_text(dictMap % i, self.fieldMap[i])
orderEdit = self.input_float(orderMap % i, self.order[i])
elements.append([i,mappedEdit,orderEdit])
append(self.create_button('clear','Clear Visible Items'))
append('<br>')
append(self.option_select(self.fieldMap.keys(), 'visible',self.getVisible(), 1,5))
append(self.createTable(elements))
append(self.customEdit())
return ''.join(temp)
security.declarePrivate('customEdit')
def customEdit(self):
"This is an edit piece that can be overridden by various custom editing features needed by other filters"
return ""
security.declarePrivate('before_manage_edit')
def before_manage_edit(self, form):
"process the edits"
if 'clear' in form:
self.delObjects(('visible',))
if 'visible' in form:
del form['visible']
security.declarePrivate('getFieldMapKeys')
def getFieldMapKeys(self):
"Get the keys to the fieldMap object"
if self.fieldMap is not None:
return self.fieldMap.keys()
return []
security.declarePrivate('getVisible')
def getVisible(self):
"get the visible items"
if self.visible is not None:
return self.visible
return []
security.declarePrivate('getOrderKeys')
def getOrderKeys(self):
"Get the keys to the fieldMap object"
if self.order is not None:
return self.order.keys()
return []
security.declarePrivate('getFieldOrder')
def getFieldOrder(self):
"get the current field order"
path = self.aq_parent.getConfig('orderHeaderPath')
script = None
if path:
script = self.restrictedTraverse(path, None)
if script is not None:
return script(self.getCompoundDoc())
if self.order is not None and self.visible is not None and self.fieldMap is not None:
order = sorted((value, key) for key, value in self.order.items())
visible = set(self.visible)
fieldMap = self.fieldMap
return [(name, fieldMap[name]) for i,name in order if name in visible]
return []
def getDataRecords(self, order, archive=None, start=None, stop=None, header=None, query=None, merge=None, sliceStart=None, sliceStop=None, keys=None):
"get the records in this data recorder that match these constraints"
if not order:
return
if merge is not None:
records = BTrees.OOBTree.OOBTree()
if self.records is not None:
records.update(self.records)
if self.archive is not None:
records.update(self.archive)
elif archive:
records = self.archive
else:
records = self.records
if records is None:
return
if start is None and self.startTime is not None:
startTime = self.startTime(mode='view')
if startTime:
start = startTime.earliestTime()
if stop is None and self.stopTime is not None:
stopTime = self.stopTime(mode='view')
if stopTime:
stop = stopTime.latestTime()
recordOrder, recordNames = zip(*order)
allowed = None
if query is not None:
catalog = self.getCatalog(archive)
if catalog is not None:
allowed = BTrees.OOBTree.OOSet((float(record.record_id) for record in catalog(query)))
if header:
yield recordNames
recordsGen = None
if allowed is None:
if sliceStart is not None and sliceStop is not None:
recordsGen = records.items(start, stop)[sliceStart:sliceStop]
else:
recordsGen = records.items(start, stop)
else: #when searching with the catalog we need to seperate the Start,Stop case from not having those bounds
#the slice case probably needs to be massively updated also
if sliceStart is not None and sliceStop is not None:
recordsGen = ( (name,value) for name, value in records.items(start, stop)[sliceStart:sliceStop] if name in allowed)
elif start is not None or stop is not None:
recordsGen = ( (name,records[name]) for name in BTrees.OOBTree.intersection(BTrees.OOBTree.OOSet(records.keys(start,stop)), allowed))
else:
recordsGen = ( (name,records[name]) for name in BTrees.OOBTree.intersection(records, allowed))
if recordsGen is not None:
for key,record in com.db.subTransDeactivateKeyValue(recordsGen, 100, self.getPhysicalRoot()):
data = [str(record.get(name, '')) for name in recordOrder]
if keys:
yield key,data
else:
yield data
security.declarePrivate('classUpgrader')
def classUpgrader(self):
"upgrade this class"
self.removeNotUsed()
security.declarePrivate('removeNotUsed')
def removeNotUsed(self):
"remove visible, fieldMap, order if not being used"
toRemove = []
if not self.visible:
toRemove.append('visible')
if not self.fieldMap:
toRemove.append('fieldMap')
if not self.order:
toRemove.append('order')
self.delObjects(toRemove)
removeNotUsed = utility.upgradeLimit(removeNotUsed, 158)
Globals.InitializeClass(DataFilter)