1
2
3
4
5
6
7
8
9
10
11
12
13
14 __doc__="""ZenTableState
15
16 Track the state of a given table.
17
18 $Id: ZenTableState.py,v 1.3 2004/01/17 04:56:13 edahl Exp $"""
19
20 __revision__ = "$Revision: 1.3 $"[11:-2]
21
22 import types
23
24 from Globals import InitializeClass
25 from AccessControl import ClassSecurityInfo
26
28
29 changesThatResetStart = [
30 "batchSize",
31 "filter",
32 "sortedHeader",
33 "sortedSence",
34 "onlyMonitored"
35 ]
36
37 requestAtts = [
38 "batchSize",
39 "filter",
40 "filterFields",
41 "sortedHeader",
42 "sortedSence",
43 "sortRule",
44 "start",
45 "URL",
46 "onlyMonitored"
47 ]
48
49 security = ClassSecurityInfo()
50
51 security.setDefaultAccess('allow')
52
53 - def __init__(self, request, tableName, defaultBatchSize, **keys):
54 self.URL = request.URL
55 self.tableName = tableName
56 self.sortedHeader = "primarySortKey"
57 self.sortedSence="asc"
58 self.sortRule = "cmp"
59 self.onlyMonitored = 0
60 self.defaultBatchSize = defaultBatchSize
61 self.batchSize = defaultBatchSize
62 self.start = 0
63 self.lastindex = 0
64 self.filter = ""
65 self.filterFields = []
66 self.totalobjs = 0
67 self.abbrStartLabel = 15
68 self.abbrEndLabel = 5
69 self.abbrPadding = 5
70 self.abbrSeparator = ".."
71 self.abbrThresh = self.abbrStartLabel + \
72 self.abbrEndLabel + self.abbrPadding
73 self.tableClass = "tableheader"
74 self.resetStart = False
75 self.showAll = False
76 self.setTableStateFromKeys(keys)
77
78
84
85
87 """update table state based on request"""
88 if self.URL != request.URL:
89 self.batchSize = self.defaultBatchSize
90 self.start=0
91 self.filter = ''
92 if request.get('tableName', None) != self.tableName:
93 return
94 for attname in self.requestAtts:
95 if request.has_key(attname):
96 self.setTableState(attname, request[attname])
97 if not request.has_key('onlyMonitored'):
98 self.setTableState('onlyMonitored', 0)
99 if request.get("first",False):
100 self.resetStart = True
101 elif request.get("last", False):
102 self.start=self.lastindex
103 elif request.get("next", False):
104 np = self.start + self.batchSize
105 if np > self.lastindex: self.start = self.lastindex
106 else: self.start = np
107 elif request.get("prev", False):
108 pp = self.start - self.batchSize
109 if pp < 0: self.start = 0
110 else: self.start = pp
111 elif request.get("showAll", False):
112 self.showAll = True
113 self.start = 0
114 self.batchSize = 0
115
116
117
118 ourl = "/".join((request.URL,request.get("zenScreenName","")))
119 if self.resetStart or (self.URL != request.URL and self.URL != ourl):
120 self.start = 0
121 self.resetStart = False
122
123
126
127
128 - def buildPageNavigation(self, objects):
129 self.pagenav = []
130
131
132
133
134
135 if self.batchSize == 0:
136 return self.pagenav
137 lastindex=0
138 for index in range(0, self.totalobjs, self.batchSize):
139 pg = {}
140 pg['label'] = self._pageLabel(objects, index)
141 pg['index'] = index
142 self.pagenav.append(pg)
143 lastindex=index
144 self.lastindex = lastindex
145
146
147 - def _pageLabel(self, objects, index):
148 """make label for page navigation if field isn't sorted use page #"""
149 pageLabel = ""
150 if self.sortedHeader:
151 pageLabel = self._buildTextLabel(objects[index])
152 elif self.batchSize:
153 pageLabel = str(1+index/self.batchSize)
154 else:
155 pageLabel = '1'
156 return pageLabel
157
158
159 - def _buildTextLabel(self, item):
160 startAbbr = ""
161 endAbbr = ""
162 attr = getattr(item, self.sortedHeader, "")
163 if callable(attr): attr = attr()
164 if type(attr) in types.StringTypes:
165 label = attr
166 else:
167 label = str(attr)
168 if len(label) > self.abbrThresh:
169 startAbbr = label[:self.abbrStartLabel]
170 if self.abbrEndLabel > 0:
171 endAbbr = label[-self.abbrEndLabel:]
172 label = "".join((startAbbr, self.abbrSeparator, endAbbr))
173 return label
174
175
193
194
196 """make sure we only add non-dup filterfields"""
197 if fieldName and fieldName not in self.filterFields:
198 self.filterFields.append(fieldName)
199
200
201 InitializeClass(ZenTableState)
202