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 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  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 isinstance(items, dict): items = [items]
    108          for item in items:
    109              menu.manage_addZenMenuItem(**item)
    110          return menu
    111
    112      security.declareProtected('Change Device', 'buildMenus')
    113 -    def buildMenus(self, menudict={}):
    114          """ Build menus from a dictionary. """
    115          for k,v in menudict.iteritems():
    116              self.manage_addItemsToZenMenu(k, v)
    117
    118      security.declareProtected('Change Device', 'manage_deleteZenMenu')
    119 -    def manage_deleteZenMenu(self, delids=(), REQUEST=None):
    120          """ Delete Menu Items from this object """
    121          if isinstance(delids,(str,unicode)): delids = [delids]
    122          for id in delids:
    123              self.zenMenus._delObject(id)
    124          if self.meta_type == 'Device':
    125              self.setLastChange()
    126          if REQUEST:
    127              messaging.IMessageSender(self).sendToBrowser(
    128                  'Menus Deleted',
    129                  'The menus %s have been deleted.' % (', '.join(delids))
    130              )
    131              return self.callZenScreen(REQUEST)
    132
    133      #security.declareProtected('View', 'getMenus')
    134 -    def getMenus(self, menuids=None, context=None):
    135          """ Build menus for this context, acquiring ZenMenus
    136              which in turn acquire ZenMenuItems.
    137
    138              Pass it a menuid for a list of menuitems,
    139              a sequence of menuids for a dict of lists of items,
    140              or nothing for a dict of all available menus.
    141          """
    142          if not context: context=self
    143          menus = {}
    144          if not isinstance(self, ZenMenuable): return None
    145          if isinstance(menuids, (str,unicode)): menuids=[menuids]
    146          mychain = aq_chain(context.primaryAq())
    147          mychain.reverse()
    148          for obj in mychain:
    149              if getattr(aq_base(obj), 'zenMenus', None):
    150                  mens = obj.zenMenus()
    151                  while mens:
    152                      c = mens.pop()
    153                      if menuids and c.id not in menuids: continue
    154                      menu = menus[c.id] = menus.get(c.id, {})
    155                      its = c.zenMenuItems()
    156                      while its:
    157                          i = its.pop()
    158                          def permfilter(p):
    159                              return self.checkRemotePerm(p,context)
    160                          permok = filter(permfilter,
    161                              getattr(i,'permissions',('',)))
    162                          if not permok \
    163                             or (not getattr(i, 'isglobal', True) and \
    164                                 context!=i.getMenuItemOwner())\
    165                             or (context.id in i.banned_ids) \
    166                             or (i.allowed_classes and not \
    167                                cmpClassNames(context, i.allowed_classes))\
    168                             or cmpClassNames(context, i.banned_classes):
    169                              continue
    170                          menu[i.id] = i
    171          keys = menus.keys()
    172          for key in keys:
    173              menus[key] = menus[key].values()
    174              if not menus[key]: del menus[key]
    175          if not menus:
    176              return None
    177          elif len(menus.keys())==1:
    178              return menus.values()[0]
    179          else:
    180              return menus
    181
    182  InitializeClass(ZenMenuable)
    183