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

# Source Code for Module Products.ZenModel.ZenMenuable

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