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 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 -    def keys(self):
     79          """
     80          Behave like a REQUEST, for report plugins that use REQUEST to pull
     81          objects (for example, the interface plugin)
     82          """
     83          return self.__dict__.keys()
     84
     85 -    def values(self):
     86          """
     87          Behave like a REQUEST, for report plugins that use REQUEST to
     88          pull objects (for example, the interface plugin).
     89          """
     90          return self.__dict__.values()
     91
     92 -    def setTableStateFromKeys(self, keys):
     93          self.__dict__.update(keys)
     94          for key in keys.keys():
     95              if key not in self.requestAtts:
     96                  self.requestAtts.append(key)
     97
     98
     99 -    def updateFromRequest(self, request):
    100          """update table state based on request"""
    101          if self.URL != request.URL:
    102              self.batchSize = self.defaultBatchSize
    103              self.start=0
    104              self.filter = ''
    105          if request.get('tableName', None) != self.tableName:
    106              return
    107          for attname in self.requestAtts:
    108              if request.has_key(attname):
    109                  self.setTableState(attname, request[attname])
    110          if not request.has_key('onlyMonitored'):
    111              self.setTableState('onlyMonitored', 0)
    112          if request.get("first",False):
    113              self.resetStart = True
    114          elif request.get("last", False):
    115              self.start=self.lastindex
    116          elif request.get("next", False):
    117              np = self.start + self.batchSize
    118              if np > self.lastindex: self.start = self.lastindex
    119              else: self.start = np
    120          elif request.get("prev", False):
    121              pp = self.start - self.batchSize
    122              if pp < 0: self.start = 0
    123              else: self.start = pp
    124          elif request.get("showAll", False):
    125              self.showAll = True
    126              self.start = 0
    127              self.batchSize = 0
    128              # the batch size needs to be set to the total object/result count.
    129              # we don't have the objects here, so we will set the batchSize
    130              # where we do have the objects -- see buildPageNavigation() below.
    131          ourl = "/".join((request.URL,request.get("zenScreenName","")))
    132          if self.resetStart or (self.URL != request.URL and self.URL != ourl):
    133              self.start = 0
    134              self.resetStart = False
    135
    136
    137 -    def getPageNavigation(self):
    138          return self.pagenav
    139
    140
    141 -    def buildPageNavigation(self, objects):
    142          self.pagenav = []
    143          # this conditional is for setting the batchSize on a "showAll"
    144          #if self.showAll:
    145          #    self.batchSize = len(objects)
    146          #    self.start = 0
    147          #    self.showAll = False
    148          if self.batchSize == 0:
    149              return self.pagenav
    150          lastindex=0
    151          for index in range(0, self.totalobjs, self.batchSize):
    152              pg = {}
    153              pg['label'] = self._pageLabel(objects, index)
    154              pg['index'] = index
    155              self.pagenav.append(pg)
    156              lastindex=index
    157          self.lastindex = lastindex
    158
    159
    160 -    def _pageLabel(self, objects, index):
    161          """make label for page navigation if field isn't sorted use page #"""
    162          pageLabel = ""
    163          if self.sortedHeader:
    164              pageLabel = self._buildTextLabel(objects[index])
    165          elif self.batchSize:
    166              pageLabel = str(1+index/self.batchSize)
    167          else:
    168              pageLabel = '1'
    169          return pageLabel
    170
    171
    172 -    def _buildTextLabel(self, item):
    173          startAbbr = ""
    174          endAbbr = ""
    175          attr = getattr(item, self.sortedHeader, "")
    176          if callable(attr): attr = attr()
    177          if type(attr) in types.StringTypes:
    178              label = attr
    179          else:
    180              label = str(attr)
    181          if len(label) > self.abbrThresh:
    182              startAbbr = label[:self.abbrStartLabel]
    183              if self.abbrEndLabel > 0:
    184                  endAbbr = label[-self.abbrEndLabel:]
    185              label = "".join((startAbbr, self.abbrSeparator, endAbbr))
    186          return label
    187
    188
    189 -    def setTableState(self, attname, value, default=None, reset=False):
    190          if attname == 'batchSize':
    191              if value in ['', '0']:
    192                  value = 0
    193              else:
    194                  value = int(value)
    195          if not hasattr(self, attname) and default != None:
    196              setattr(self, attname, default)
    197              if reset and attname not in self.changesThatResetStart:
    198                  self.changesThatResetStart.append(attname)
    199              if attname not in self.requestAtts:
    200                  self.requestAtts.append(attname)
    201          if value != None and getattr(self,attname, None) != value:
    202              setattr(self, attname, value)
    203              if attname in self.changesThatResetStart:
    204                  self.resetStart = True
    205          return getattr(self,attname)
    206
    207
    208 -    def addFilterField(self, fieldName):
    209          """make sure we only add non-dup filterfields"""
    210          if fieldName and fieldName not in self.filterFields:
    211              self.filterFields.append(fieldName)
    212
    213
    214  InitializeClass(ZenTableState)
    215