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

# Source Code for Module Products.ZenWidgets.ZenTableState

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