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

Source Code for Module Products.ZenWidgets.Portlet

  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 logging 
 12  log = logging.getLogger('zen.Portlet') 
 13   
 14  from string import Template 
 15   
 16  from Products.ZenModel.ZenossSecurity import ZEN_COMMON 
 17  from os.path import basename, exists 
 18  from Products.ZenRelations.RelSchema import ToManyCont, ToOne 
 19  from Products.ZenModel.ZenModelRM import ZenModelRM 
 20  from Globals import InitializeClass 
 21  from Products.ZenUtils.Utils import zenPath 
 22   
23 -def manage_addPortlet(self, context, REQUEST=None):
24 """ 25 Add a portlet. 26 """ 27 pass
28
29 -class Portlet(ZenModelRM):
30 """ 31 A wrapper for a portlet javascript source file that can include metadata 32 such as a name, a title, a description and permissions. 33 34 Portlets should not be instantiated directly. They should only be created 35 by a PortletManager object. 36 """ 37 source = '' 38 height = 200 39 40 portal_type = meta_type = 'Portlet' 41 42 _relations = ( 43 ("portletManager", ToOne( 44 ToManyCont, "Products.ZenWidgets.PortletManager", "portlets")), 45 ) 46 47 _properties = ( 48 {'id':'title','type':'string','mode':'w'}, 49 {'id':'description', 'type':'string', 'mode':'w'}, 50 {'id':'permission', 'type':'string', 'mode':'w'}, 51 {'id':'sourcepath', 'type':'string', 'mode':'w'}, 52 {'id':'preview', 'type':'string', 'mode':'w'}, 53 {'id':'height', 'type':'int', 'mode':'w'}, 54 ) 55 56
57 - def __init__(self, sourcepath, id='', title='', description='', 58 preview='', height=200, permission=ZEN_COMMON):
59 if not id: id = basename(sourcepath).split('.')[0] 60 self.id = id 61 ZenModelRM.__init__(self, id) 62 self.title = title 63 self.description = description 64 self.permission = permission 65 self.sourcepath = sourcepath 66 self.preview = preview 67 self.height = height 68 self._read_source()
69
70 - def _getSourcePath(self):
71 return zenPath(self.sourcepath)
72
73 - def check(self):
74 return exists(self._getSourcePath())
75
76 - def _read_source(self):
77 try: 78 path = self.sourcepath if exists(self.sourcepath) else self._getSourcePath() 79 f = file(path) 80 except IOError as ex: 81 log.error("Unable to load portlet from '%s': %s", path, ex) 82 return 83 else: 84 tvars = {'portletId': self.id, 85 'portletTitle': self.title, 86 'portletHeight': self.height} 87 self.source = Template(f.read()).safe_substitute(tvars) 88 f.close()
89
90 - def getPrimaryPath(self,fromNode=None):
91 """ 92 Override the default, which doesn't account for things on zport 93 """ 94 return ('', 'zport') + super(Portlet, self).getPrimaryPath(fromNode)
95
96 - def get_source(self, debug_mode=False):
97 if debug_mode: self._read_source() 98 src = [] 99 src.append(self.source) 100 src.append("YAHOO.zenoss.portlet.register_portlet('%s', '%s');" % ( 101 self.id, self.title)) 102 return '\n'.join(src)
103 104 InitializeClass(Portlet) 105