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

# Source Code for Module Products.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 or (at your
      8  # option) any later version as published by 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  from Globals import InitializeClass
     23  from AccessControl import ClassSecurityInfo
     24
     25 -class ZenTableState:
     26
     27      changesThatResetStart = [
     28          "batchSize",
     29          "filter",
     30          "sortedHeader",
     31          "sortedSence",
     32          "onlyMonitored"
     33          ]
     34
     35      requestAtts = [
     36          "batchSize",
     37          "filter",
     38          "filterFields",
     39          "sortedHeader",
     40          "sortedSence",
     41          "sortRule",
     42          "start",
     43          "URL",
     44          "onlyMonitored"
     45          ]
     46
     47      security = ClassSecurityInfo()
     48      #this session info isn't anything worth protecting
     49      security.setDefaultAccess('allow')
     50
     51 -    def __init__(self, request, tableName, defaultBatchSize, **keys):
     52          self.URL = request.URL
     53          self.tableName = tableName
     54          self.sortedHeader = "primarySortKey"
     55          self.sortedSence="asc"
     56          self.sortRule = "cmp"
     57          self.onlyMonitored = 0
     58          self.defaultBatchSize = defaultBatchSize
     59          self.batchSize = defaultBatchSize
     60          self.start = 0
     61          self.lastindex = 0
     62          self.filter = ""
     63          self.filterFields = []
     64          self.totalobjs = 0
     65          self.abbrStartLabel = 15
     66          self.abbrEndLabel = 5
     67          self.abbrPadding = 5
     68          self.abbrSeparator = ".."
     69          self.abbrThresh = self.abbrStartLabel + \
     70                          self.abbrEndLabel + self.abbrPadding
     71          self.tableClass = "tableheader"
     72          self.resetStart = False
     73          self.showAll = False
     74          self.setTableStateFromKeys(keys)
     75
     76 -    def keys(self):
     77          """
     78          Behave like a REQUEST, for report plugins that use REQUEST to pull
     79          objects (for example, the interface plugin)
     80          """
     81          return self.__dict__.keys()
     82
     83 -    def values(self):
     84          """
     85          Behave like a REQUEST, for report plugins that use REQUEST to
     86          pull objects (for example, the interface plugin).
     87          """
     88          return self.__dict__.values()
     89
     90 -    def setTableStateFromKeys(self, keys):
     91          self.__dict__.update(keys)
     92          for key in keys.keys():
     93              if key not in self.requestAtts:
     94                  self.requestAtts.append(key)
     95
     96
     97 -    def updateFromRequest(self, request):
     98          """update table state based on request"""
     99          if self.URL != request.URL:
    100              self.batchSize = self.defaultBatchSize
    101              self.start=0
    102              self.filter = ''
    103          if request.get('tableName', None) != self.tableName:
    104              return
    105          for attname in self.requestAtts:
    106              if request.has_key(attname):
    107                  self.setTableState(attname, int(request[attname]) if attname == 'start' else request[attname])
    108          if not request.has_key('onlyMonitored'):
    109              self.setTableState('onlyMonitored', 0)
    110          if request.get("first",False):
    111              self.resetStart = True
    112          elif request.get("last", False):
    113              self.start=self.lastindex
    114          elif request.get("next", False):
    115              np = self.start + self.batchSize
    116              if np > self.lastindex: self.start = self.lastindex
    117              else: self.start = np
    118          elif request.get("prev", False):
    119              pp = self.start - self.batchSize
    120              if pp < 0: self.start = 0
    121              else: self.start = pp
    122          elif request.get("showAll", False):
    123              self.showAll = True
    124              self.start = 0
    125              self.batchSize = 0
    126              # the batch size needs to be set to the total object/result count.
    127              # we don't have the objects here, so we will set the batchSize
    128              # where we do have the objects -- see buildPageNavigation() below.
    129          ourl = "/".join((request.URL,request.get("zenScreenName","")))
    130          if self.resetStart or (self.URL != request.URL and self.URL != ourl):
    131              self.start = 0
    132              self.resetStart = False
    133
    134
    135 -    def getPageNavigation(self):
    136          return self.pagenav
    137
    138
    139 -    def buildPageNavigation(self, objects):
    140          self.pagenav = []
    141          # this conditional is for setting the batchSize on a "showAll"
    142          #if self.showAll:
    143          #    self.batchSize = len(objects)
    144          #    self.start = 0
    145          #    self.showAll = False
    146          if self.batchSize == 0:
    147              return self.pagenav
    148          lastindex=0
    149          for index in range(0, self.totalobjs, self.batchSize):
    150              pg = {}
    151              pg['label'] = self._pageLabel(objects, index)
    152              pg['index'] = index
    153              self.pagenav.append(pg)
    154              lastindex=index
    155          self.lastindex = lastindex
    156
    157
    158 -    def _pageLabel(self, objects, index):
    159          """make label for page navigation if field isn't sorted use page #"""
    160          pageLabel = ""
    161          if self.sortedHeader:
    162              pageLabel = self._buildTextLabel(objects[index])
    163          elif self.batchSize:
    164              pageLabel = str(1+index/self.batchSize)
    165          else:
    166              pageLabel = '1'
    167          return pageLabel
    168
    169
    170 -    def _buildTextLabel(self, item):
    171          startAbbr = ""
    172          endAbbr = ""
    173          attr = getattr(item, self.sortedHeader, "")
    174          if callable(attr): attr = attr()
    175          if isinstance(attr, basestring):
    176              label = attr
    177          else:
    178              label = str(attr)
    179          if len(label) > self.abbrThresh:
    180              startAbbr = label[:self.abbrStartLabel]
    181              if self.abbrEndLabel > 0:
    182                  endAbbr = label[-self.abbrEndLabel:]
    183              label = "".join((startAbbr, self.abbrSeparator, endAbbr))
    184          return label
    185
    186
    187 -    def setTableState(self, attname, value, default=None, reset=False):
    188          if attname == 'batchSize':
    189              if value in ['', '0']:
    190                  value = 0
    191              else:
    192                  value = int(value)
    193          if not hasattr(self, attname) and default != None:
    194              setattr(self, attname, default)
    195              if reset and attname not in self.changesThatResetStart:
    196                  self.changesThatResetStart.append(attname)
    197              if attname not in self.requestAtts:
    198                  self.requestAtts.append(attname)
    199          if value != None and getattr(self,attname, None) != value:
    200              setattr(self, attname, value)
    201              if attname in self.changesThatResetStart:
    202                  self.resetStart = True
    203          return getattr(self,attname)
    204
    205
    206 -    def addFilterField(self, fieldName):
    207          """make sure we only add non-dup filterfields"""
    208          if fieldName and fieldName not in self.filterFields:
    209              self.filterFields.append(fieldName)
    210
    211
    212  InitializeClass(ZenTableState)
    213