Package Products :: Package ZenModel :: Module ZenMenuable |
[hide private]
[frames] | no frames]

# Source Code for Module Products.ZenModel.ZenMenuable

      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  from AccessControl import ClassSecurityInfo, Permissions
     14  from ZenMenu import ZenMenu
     15  from Globals import InitializeClass
     16  from Acquisition import aq_base, aq_chain
     17  from Products.ZenRelations.RelSchema import *
     18  from Products.ZenUtils.Utils import cmpClassNames
     19  from Products.ZenWidgets import messaging
     20
     21 -class ZenMenuable:
     22      """ ZenMenuable is a mixin providing menuing.
     23      """
     24
     25      security = ClassSecurityInfo()
     26
     27      security.declareProtected('Change Device', 'manage_addZenMenu')
     28 -    def manage_addZenMenu(self, id=None, desc='', REQUEST=None):
     29          """ Add a menu item to this device organizer """
     30          mu = None
     31          if id:
     32              mu = ZenMenu(id)
     33              self.zenMenus._setObject(id, mu)
     34              mu = self.zenMenus._getOb(id)
     35              if self.meta_type == 'Device':
     36                  self.setLastChange()
     37              mu.description = desc
     38          if REQUEST:
     39              if mu:
     40                  messaging.IMessageSender(self).sendToBrowser(
     41                      'Menu Added',
     42                      'The menu "%s" has been added.' % mu.id
     43                  )
     44                  url = '%s/zenMenus/%s' % (self.getPrimaryUrlPath(), mu.id)
     45                  return REQUEST['RESPONSE'].redirect(url)
     46              return self.callZenScreen(REQUEST)
     47          return mu
     48
     49
     50      security.declareProtected('Change Device', 'manage_addZenMenuItem')
     51 -    def manage_addZenMenuItem(self, menuid, id=None, description='', action='',
     52              permissions=(Permissions.view,), isdialog=False, isglobal=True,
     53              banned_classes=(), allowed_classes=(), banned_ids=(), ordering=0.0,
     54              REQUEST=None):
     55          """ Add ZenMenuItem
     56          """
     57          menu = getattr(self.zenMenus, menuid, None)
     58          if not menu: menu = self.manage_addZenMenu(menuid)
     59          menu.manage_addZenMenuItem(id, description, action,
     60                  permissions, isdialog, isglobal,
     61                  banned_classes, allowed_classes, banned_ids, ordering)
     62          if REQUEST:
     63              return self.callZenScreen(REQUEST)
     64
     65
     66      security.declareProtected('Change Device', 'manage_deleteZenMenuItem')
     67 -    def manage_deleteZenMenuItem(self, menuid, delids=(), REQUEST=None):
     68          """ Delete Menu Items """
     69          menu = getattr(self.zenMenus, menuid, None)
     70          if menu:
     71              menu.manage_deleteZenMenuItem(delids)
     72          if REQUEST:
     73              return self.callZenScreen(REQUEST)
     74
     75
     76      security.declareProtected('Change Device', 'manage_saveMenuItemOrdering')
     77 -    def manage_saveMenuItemOrdering(self, menuid, REQUEST=None):
     78          """ Delete Menu Items """
     79          menu = getattr(self.zenMenus, menuid, None)
     80          if menu and REQUEST:
     81              for menuitem in getattr(self.zenMenus, menuid).zenMenuItems():
     82                  ordering = REQUEST[menuitem.id]
     83                  menuitem.ordering = ordering
     84          if REQUEST:
     85              return self.callZenScreen(REQUEST)
     86
     87
     88      security.declareProtected('Change Device', 'manage_addItemsToZenMenu')
     89 -    def manage_addItemsToZenMenu(self, menuid, items=None):
     90          """ Add ZenMenuItems to a ZenMenu.
     91              Accepts a list of dictionaries.
     92              Available keyword args:
     93                id
     94                description
     95                action
     96                permissions
     97                isglobal
     98                isdialog
     99                banned_classes
    100                allowed_classes
    101                banned_ids
    102          """
    103          if not items:
    104              items = [{}]
    105          menu = getattr(self.zenMenus, menuid, None)
    106          if not menu: menu = self.manage_addZenMenu(menuid)
    107          if type(items)==type({}): items = [items]
    108          while items:
    109              menu.manage_addZenMenuItem(**items.pop())
    110          return menu
    111
    112      security.declareProtected('Change Device', 'buildMenus')
    113 -    def buildMenus(self, menudict={}):
    114          """ Build menus from a dictionary. """
    115          menus = menudict.keys()
    116          while menus:
    117              menu = menus.pop()
    118              self.manage_addItemsToZenMenu(menu, menudict[menu])
    119
    120      security.declareProtected('Change Device', 'manage_deleteZenMenu')
    121 -    def manage_deleteZenMenu(self, delids=(), REQUEST=None):
    122          """ Delete Menu Items from this object """
    123          if isinstance(delids,(str,unicode)): delids = [delids]
    124          for id in delids:
    125              self.zenMenus._delObject(id)
    126          if self.meta_type == 'Device':
    127              self.setLastChange()
    128          if REQUEST:
    129              messaging.IMessageSender(self).sendToBrowser(
    130                  'Menus Deleted',
    131                  'The menus %s have been deleted.' % (', '.join(delids))
    132              )
    133              return self.callZenScreen(REQUEST)
    134
    135      #security.declareProtected('View', 'getMenus')
    136 -    def getMenus(self, menuids=None, context=None):
    137          """ Build menus for this context, acquiring ZenMenus
    138              which in turn acquire ZenMenuItems.
    139
    140              Pass it a menuid for a list of menuitems,
    141              a sequence of menuids for a dict of lists of items,
    142              or nothing for a dict of all available menus.
    143          """
    144          if not context: context=self
    145          menus = {}
    146          if not isinstance(self, ZenMenuable): return None
    147          if isinstance(menuids, (str,unicode)): menuids=[menuids]
    148          mychain = aq_chain(context.primaryAq())
    149          mychain.reverse()
    150          for obj in mychain:
    151              if getattr(aq_base(obj), 'zenMenus', None):
    152                  mens = obj.zenMenus()
    153                  while mens:
    154                      c = mens.pop()
    155                      if menuids and c.id not in menuids: continue
    156                      menu = menus[c.id] = menus.get(c.id, {})
    157                      its = c.zenMenuItems()
    158                      while its:
    159                          i = its.pop()
    160                          def permfilter(p):
    161                              return self.checkRemotePerm(p,context)
    162                          permok = filter(permfilter,
    163                              getattr(i,'permissions',('',)))
    164                          if not permok \
    165                             or (not getattr(i, 'isglobal', True) and \
    166                                 context!=i.getMenuItemOwner())\
    167                             or (context.id in i.banned_ids) \
    168                             or (i.allowed_classes and not \
    169                                cmpClassNames(context, i.allowed_classes))\
    170                             or cmpClassNames(context, i.banned_classes):
    171                              continue
    172                          menu[i.id] = i
    173          keys = menus.keys()
    174          for key in keys:
    175              menus[key] = menus[key].values()
    176              if not menus[key]: del menus[key]
    177          if not menus:
    178              return None
    179          elif len(menus.keys())==1:
    180              return menus.values()[0]
    181          else:
    182              return menus
    183
    184  InitializeClass(ZenMenuable)
    185