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

# Source Code for Module Products.ZenWidgets.PortletManager

      1  ##############################################################################
      2  #
      3  # Copyright (C) Zenoss, Inc. 2007, all rights reserved.
      4  #
      5  # This content is made available according to terms specified in
      6  # License.zenoss under the directory where your Zenoss product is installed.
      7  #
      8  ##############################################################################
      9
     10
     11  import os, md5
     12  from Globals import InitializeClass, DevelopmentMode
     13  from AccessControl import getSecurityManager
     14  from Products.ZenRelations.RelSchema import *
     15  from Products.ZenModel.ZenModelRM import ZenModelRM
     16  from Products.ZenMessaging.audit import audit
     17  from Products.ZenModel.ZenossSecurity import *
     18  from Products.ZenWidgets import messaging
     19
     20  from Portlet import Portlet
     21
     22  getuid = lambda:md5.md5(os.urandom(10)).hexdigest()[:8]
     23
     24 -class DuplicatePortletRegistration(Exception): pass
     25
     26 -def manage_addPortletManager(context, id="", REQUEST=None):
     27      """
     28      Create a portlet manager under context.
     29      """
     30      if not id: id = "ZenPortletManager"
     31      zpm = PortletManager(id)
     32      context._setObject(id, zpm)
     33      zpm = context._getOb(id)
     34      zpm.buildRelations()
     35
     36 -class PortletManager(ZenModelRM):
     37      """
     38      A registry for portlet source and metadata. Provides access functions and
     39      handles portlet permissions.
     40      """
     41
     42      portal_type = meta_type = 'PortletManager'
     43
     44      _relations = (
     45          ("portlets", ToManyCont(ToOne, "Products.ZenWidgets.Portlet",
     46              "portletManager")),
     47      )
     48
     49 -    def register_extjsPortlet(self, id, title, height=200, permission=ZEN_COMMON):
     50          """
     51          Registers an ExtJS portlet
     52          """
     53          ppath = os.path.join('Products','ZenWidgets','ZenossPortlets','ExtPortlet.js')
     54          self.register_portlet(ppath, id=id, title=title, height=height,
     55                                permission=permission)
     56
     57 -    def register_portlet(self, sourcepath, id='', title='', description='',
     58                           preview='', height=200, permission=ZEN_COMMON):
     59          """
     60          Registers a new source file and creates an associated Portlet to store
     61          the metadata and provide access methods.
     62          """
     63          p = self.find(id, sourcepath)
     64          if p:
     65              old_values = (p.sourcepath, p.id, p.title, p.description, p.preview, p.height, p.permission)
     66              new_values = (sourcepath, id, title, description, preview, height, permission)
     67              if old_values == new_values:
     68                  # Portlet unchanged - don't re-register
     69                  return
     70              self.unregister_portlet(p.id)
     71          p = Portlet(sourcepath, id, title, description, preview, height, permission)
     72          self.portlets._setObject(id, p)
     73
     74 -    def unregister_portlet(self, id):
     75          self.portlets._delObject(id)
     76
     77 -    def get_portlets(self):
     78          """
     79          Looks up in the registry and returns all portlet objects to which the
     80          current user has access.
     81          """
     82          user = getSecurityManager().getUser()
     83          dmd = self.dmd.primaryAq()
     84          return filter(
     85              lambda x:user.has_permission(x.permission, dmd) and x.check(),
     86              self.portlets())
     87
     88 -    def find(self, id='', sourcepath=''):
     89          """
     90          Look for a registered portlet with an id or source path.
     91          """
     92          for portlet in self.portlets():
     93              if portlet.id==id or portlet.sourcepath==sourcepath: return portlet
     94          return None
     95
     96 -    def update_source(self, REQUEST=None):
     97          """
     98          Reread the source files for all portlets.
     99          """
    100          for portlet in self.portlets():
    101              portlet._read_source()
    102
    103 -    def get_source(self, REQUEST=None):
    104          """
    105          Return the source of the portlets permitted to this user as a
    106          javascript file.
    107          """
    108          srcs = [x.get_source(DevelopmentMode) for x in self.get_portlets()]
    109          srcs.append('YAHOO.register("portletsource", YAHOO.zenoss.portlet, {})')
    110          if REQUEST:
    111              REQUEST.response.headers['Content-Type'] = 'text/javascript'
    112          return '\n'.join(srcs)
    113
    114 -    def edit_portlet_perms(self, REQUEST=None):
    115          """
    116          blargh
    117          """
    118          for portlet in REQUEST.form:
    119              if not portlet.endswith('_permission'): continue
    120              portname = portlet.split('_')[0]
    121              p = self.find(id=portname)
    122              p.permission = REQUEST.form[portlet]
    123          if REQUEST:
    124              from Products.ZenUtils.Time import SaveMessage
    125              messaging.IMessageSender(self).sendToBrowser(
    126                  'Permissions Saved',
    127                  SaveMessage()
    128              )
    129              REQUEST['RESPONSE'].redirect('/zport/dmd/editPortletPerms')
    130          audit('UI.Portlet.Edit', data_=REQUEST.form)
    131
    132
    133  InitializeClass(PortletManager)
    134