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

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