1
2
3
4
5
6
7
8
9
10
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
22 """ ZenMenuable is a mixin providing menuing.
23 """
24
25 security = ClassSecurityInfo()
26
27 security.declareProtected('Change Device', 'manage_addZenMenu')
45
46
47 security.declareProtected('Change Device', 'manage_addZenMenuItem')
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')
70
71
72 security.declareProtected('Change Device', 'manage_saveMenuItemOrdering')
82
83
84 security.declareProtected('Change Device', 'manage_addItemsToZenMenu')
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')
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')
124
125
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