Package ZenWidgets :: Module ZenTableState
[hide private]
[frames] | no frames]

Source Code for Module ZenWidgets.ZenTableState

  1  ########################################################################### 
  2  # 
  3  # This program is part of Zenoss Core, an open source monitoring platform. 
  4  # Copyright (C) 2007, Zenoss Inc. 
  5  # 
  6  # This program is free software; you can redistribute it and/or modify it 
  7  # under the terms of the GNU General Public License version 2 as published by 
  8  # the Free Software Foundation. 
  9  # 
 10  # For complete information please visit: http://www.zenoss.com/oss/ 
 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   
27 -class ZenTableState:
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 #this session info isn't anything worth protecting 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
79 - def setTableStateFromKeys(self, keys):
80 self.__dict__.update(keys) 81 for key in keys.keys(): 82 if key not in self.requestAtts: 83 self.requestAtts.append(key)
84 85
86 - def updateFromRequest(self, request):
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 # the batch size needs to be set to the total object/result count. 116 # we don't have the objects here, so we will set the batchSize 117 # where we do have the objects -- see buildPageNavigation() below. 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
124 - def getPageNavigation(self):
125 return self.pagenav
126 127
128 - def buildPageNavigation(self, objects):
129 self.pagenav = [] 130 # this conditional is for setting the batchSize on a "showAll" 131 #if self.showAll: 132 # self.batchSize = len(objects) 133 # self.start = 0 134 # self.showAll = False 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
176 - def setTableState(self, attname, value, default=None, reset=False):
177 if attname == 'batchSize': 178 if value in ['', '0']: 179 value = 0 180 else: 181 value = int(value) 182 if not hasattr(self, attname) and default != None: 183 setattr(self, attname, default) 184 if reset and attname not in self.changesThatResetStart: 185 self.changesThatResetStart.append(attname) 186 if attname not in self.requestAtts: 187 self.requestAtts.append(attname) 188 if value != None and getattr(self,attname, None) != value: 189 setattr(self, attname, value) 190 if attname in self.changesThatResetStart: 191 self.resetStart = True 192 return getattr(self,attname)
193 194
195 - def addFilterField(self, fieldName):
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