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 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
     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_extjsPortlet(self, id, title, height=200, permission=ZEN_COMMON):
     53          """
     54          Registers an ExtJS portlet
     55          """
     56          ppath = os.path.join('Products','ZenWidgets','ZenossPortlets','ExtPortlet.js')
     57          self.register_portlet(ppath, id=id, title=title, height=height,
     58                                permission=permission)
     59
     60 -    def register_portlet(self, sourcepath, id='', title='', description='',
     61                           preview='', height=200, permission=ZEN_COMMON):
     62          """
     63          Registers a new source file and creates an associated Portlet to store
     64          the metadata and provide access methods.
     65          """
     66          p = self.find(id, sourcepath)
     67          if p: self.unregister_portlet(p.id)
     68          p = Portlet(sourcepath, id, title, description, preview, height, permission)
     69          self.portlets._setObject(id, p)
     70
     71 -    def unregister_portlet(self, id):
     72          try:
     73              self.portlets._delObject(id)
     74          except: pass
     75
     76 -    def get_portlets(self):
     77          """
     78          Looks up in the registry and returns all portlet objects to which the
     79          current user has access.
     80          """
     81          user = getSecurityManager().getUser()
     82          dmd = self.dmd.primaryAq()
     83          return filter(
     84              lambda x:user.has_permission(x.permission, dmd) and x.check(),
     85              self.portlets())
     86
     87 -    def find(self, id='', sourcepath=''):
     88          """
     89          Look for a registered portlet with an id or source path.
     90          """
     91          for portlet in self.portlets():
     92              if portlet.id==id or portlet.sourcepath==sourcepath: return portlet
     93          return None
     94
     95 -    def update_source(self, REQUEST=None):
     96          """
     97          Reread the source files for all portlets.
     98          """
     99          for portlet in self.portlets():
    100              portlet._read_source()
    101
    102 -    def get_source(self, REQUEST=None):
    103          """
    104          Return the source of the portlets permitted to this user as a
    105          javascript file.
    106          """
    107          srcs = [x.get_source(DevelopmentMode) for x in self.get_portlets()]
    108          srcs.append('YAHOO.register("portletsource", YAHOO.zenoss.portlet, {})')
    109          if REQUEST:
    110              REQUEST.response.headers['Content-Type'] = 'text/javascript'
    111          return '\n'.join(srcs)
    112
    113 -    def edit_portlet_perms(self, REQUEST=None):
    114          """
    115          blargh
    116          """
    117          for portlet in REQUEST.form:
    118              if not portlet.endswith('_permission'): continue
    119              portname = portlet.split('_')[0]
    120              p = self.find(id=portname)
    121              p.permission = REQUEST.form[portlet]
    122          if REQUEST:
    123              from Products.ZenUtils.Time import SaveMessage
    124              messaging.IMessageSender(self).sendToBrowser(
    125                  'Permissions Saved',
    126                  SaveMessage()
    127              )
    128              REQUEST['RESPONSE'].redirect('/zport/dmd/editPortletPerms')
    129
    130
    131  InitializeClass(PortletManager)
    132