Package ZenWidgets :: Module PortletManager
[hide private]
[frames] | no frames]

Source Code for Module ZenWidgets.PortletManager

  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   
 14  from sets import Set 
 15  import os, md5 
 16  from Globals import InitializeClass, DevelopmentMode 
 17  from AccessControl import getSecurityManager 
 18  from OFS.SimpleItem import SimpleItem 
 19  from OFS.PropertyManager import PropertyManager 
 20  from Acquisition import aq_base 
 21  from Products.ZenRelations.RelSchema import * 
 22  from Products.ZenModel.ZenModelRM import ZenModelRM 
 23   
 24  from Products.ZenModel.ZenossSecurity import * 
 25   
 26  from Portlet import Portlet 
 27   
 28  getuid = lambda:md5.md5(os.urandom(10)).hexdigest()[:8] 
 29   
30 -class DuplicatePortletRegistration(Exception): pass
31
32 -def manage_addPortletManager(context, id="", REQUEST=None):
33 """ 34 Create a portlet manager under context. 35 """ 36 if not id: id = "ZenPortletManager" 37 zpm = PortletManager(id) 38 context._setObject(id, zpm) 39 zpm = context._getOb(id) 40 zpm.buildRelations()
41
42 -class PortletManager(ZenModelRM):
43 """ 44 A registry for portlet source and metadata. Provides access functions and 45 handles portlet permissions. 46 """ 47 48 portal_type = meta_type = 'PortletManager' 49 50 _relations = ( 51 ("portlets", ToManyCont(ToOne, "Products.ZenWidgets.Portlet", 52 "portletManager")), 53 ) 54
55 - def register_portlet(self, sourcepath, id='', title='', description='', 56 preview='', permission=ZEN_COMMON):
57 """ 58 Registers a new source file and creates an associated Portlet to store 59 the metadata and provide access methods. 60 """ 61 p = self.find(id, sourcepath) 62 if p: self.unregister_portlet(p.id) 63 p = Portlet(sourcepath, id, title, description, preview, permission) 64 self.portlets._setObject(id, p)
65
66 - def unregister_portlet(self, id):
67 try: 68 self.portlets._delObject(id) 69 except: pass
70
71 - def get_portlets(self):
72 """ 73 Looks up in the registry and returns all portlet objects to which the 74 current user has access. 75 """ 76 user = getSecurityManager().getUser() 77 dmd = self.dmd.primaryAq() 78 return filter( 79 lambda x:user.has_permission(x.permission, dmd) and x.check(), 80 self.portlets())
81
82 - def find(self, id='', sourcepath=''):
83 """ 84 Look for a registered portlet with an id or source path. 85 """ 86 for portlet in self.portlets(): 87 if portlet.id==id or portlet.sourcepath==sourcepath: return portlet 88 return None
89
90 - def get_source(self, REQUEST=None):
91 """ 92 Return the source of the portlets permitted to this user as a 93 javascript file. 94 """ 95 srcs = [x.get_source(DevelopmentMode) for x in self.get_portlets()] 96 srcs.append('YAHOO.register("portletsource", YAHOO.zenoss.portlet, {})') 97 if REQUEST: 98 REQUEST.response.headers['Content-Type'] = 'text/javascript' 99 return '\n'.join(srcs)
100
101 - def edit_portlet_perms(self, REQUEST=None):
102 """ 103 blargh 104 """ 105 for portlet in REQUEST.form: 106 if not portlet.endswith('_permission'): continue 107 portname = portlet.split('_')[0] 108 p = self.find(id=portname) 109 p.permission = REQUEST.form[portlet] 110 if REQUEST: 111 from Products.ZenUtils.Time import SaveMessage 112 REQUEST['message'] = SaveMessage() 113 REQUEST['RESPONSE'].redirect('/zport/dmd/editPortletPerms')
114 115 116 InitializeClass(PortletManager) 117