-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcatalogtraverse.py
More file actions
163 lines (145 loc) · 6.4 KB
/
catalogtraverse.py
File metadata and controls
163 lines (145 loc) · 6.4 KB
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
import basecatalog
#for type checking with isinstance
import types
import nestedlisturl as NestedListURL
#For Security control and init
from AccessControl import ClassSecurityInfo
import Globals
import com.catalog
class CatalogTraverse(basecatalog.BaseCatalog):
"traversal capabilities for a catalog transparently and with dynamic menu and output support"
meta_type = "CatalogTraverse"
security = ClassSecurityInfo()
navigationIndexes = None
navigationOrder = None
useCatalog = ''
security.declarePrivate('getNavigationOrder')
def getNavigationOrder(self):
"get the navigationOrder list structure"
if self.navigationOrder is not None:
return self.navigationOrder
return []
security.declarePrivate('getNavigationIndexes')
def getNavigationIndexes(self):
"get the navigationIndexes list structure"
if self.navigationIndexes is not None:
return self.navigationIndexes
return {}
security.declarePublic('__bobo_traverse__')
def __bobo_traverse__(self, REQUEST, name):
"bobo method"
if name:
obj = getattr(self, name, None)
if obj is not None:
return obj
stack = self.REQUEST.TraversalRequestNameStack
stack.reverse()
stack.insert(0, name)
catalog = getattr(self.getCompoundDocContainer(), self.getRegisteredCatalog())
list = []
index = 0
navigationOrder = self.getNavigationOrder()
for i in stack:
item = navigationOrder[index]
if i in catalog.uniqueValuesFor(item):
list.append(i)
index = index + 1
for i in list:
if i in stack:
stack.remove(i)
self.REQUEST.other['catalogFields'] = list
return self.getCompoundDoc()
security.declarePrivate('performUpdate')
def performUpdate(self):
"update this object"
basecatalog.BaseCatalog.performUpdate(self)
if not self.getRegisteredCatalog():
self.setRegisteredCatalog(self.getDefaultCatalog())
security.declarePrivate('getRegisteredCatalog')
def getRegisteredCatalog(self):
"Return the name of the current registered catalog"
return self.useCatalog
security.declarePrivate('setRegisteredCatalog')
def setRegisteredCatalog(self, name):
"set the registered catalog"
self.setObject('useCatalog', name)
security.declareProtected('View', 'view')
def view(self):
"Inline draw view"
catalog = getattr(self.getCompoundDocContainer(), self.getRegisteredCatalog())
output = []
fields = self.REQUEST.other.get('catalogFields', [])
(ids, brains) = self.getCatalogResults(fields, catalog=catalog)
fields = self.createUrlIdRecursiveList(ids, base=self.absolute_url())
output.append(NestedListURL.drawNestedList(fields))
if brains:
output = [cdoc.getObject().render(mode='view') for cdoc in com.catalog.catalogIter(brains)]
return ''.join(output)
def createUrlIdRecursiveList(self, list, base):
"convert the recursive list into a set of full (url, id) pairs that can be used"
temp = []
for i, item in enumerate(list):
if isinstance(item, types.StringType):
temp.append((base+'/'+item, item))
if isinstance(item, types.ListType):
temp.append(self.createUrlIdRecursiveList(item, base+'/'+list[i-1]))
return temp
def getCatalogResults(self, list, location=0, catalog=None):
"return the items at this index recursively"
navigationOrder = self.getNavigationOrder()
item = navigationOrder[location]
temp = []
results = []
custom = {}
for i in xrange(location):
local = navigationOrder[i]
custom[local] = list[i]
catalogOutput = catalog(custom)
uniqueList = list(set([getattr(i, item) for i in catalogOutput]))
for i in uniqueList:
temp.append(i)
if location < len(list) and list[location] == i:
(output, catalogResults) = self.getCatalogResults(list, location+1, catalog)
temp.append(output)
if catalogResults:
results = catalogResults
navigationIndexes = self.getNavigationIndexes()
if location == len(list) and navigationIndexes[navigationOrder[location]]['returnResults']:
return (temp, catalogOutput)
return (temp, results)
security.declareProtected('View management screens', 'edit')
def edit(self, *args, **kw):
"Inline edit view"
temp = []
temp.append("<div>Available Catalogs : %s</div>\n" % self.option_select(self.getAvailableCatalogs(),
'useCatalog',
[self.useCatalog]))
catalog = getattr(self.getCompoundDocContainer(), self.getRegisteredCatalog())
indexes = [i.getId() for i in catalog.index_objects() if i.meta_type == "FieldIndex"]
typeformat = '<tr><td>%s</td><td>%s</td><td>%s</td></tr>'
temp.append('<table>')
temp.append(typeformat % ('ID:', 'Order:', 'Return Results'))
navigationIndexes = self.getNavigationIndexes()
for i in indexes:
index = navigationIndexes.get(i, {})
temp.append(typeformat % (i,
self.input_float('order', index.get('order',0), containers=('navigationIndexes',i)),
self.true_false('returnResults', index.get('returnResults',0), prefix=0, containers=('navigationIndexes',i))))
temp.append('</table>')
return ''.join(temp)
def after_manage_edit(self, form):
"create a cached copy of the items we want in the order we want them to speed up output"
navigationIndexes = self.getNavigationIndexes()
temp = [(j.get('order'), i) for i, j in navigationIndexes.items()]
temp.sort()
navigationOrder = tuple([i[1] for i in temp])
if not len(navigationOrder):
navigationOrder = None
self.setObject('navigationOrder', navigationOrder)
security.declarePrivate('PrincipiaSearchSource')
def PrincipiaSearchSource(self):
"Return what can be search which is nothing"
return ''
Globals.InitializeClass(CatalogTraverse)
import register
register.registerClass(CatalogTraverse)