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

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