Esempio n. 1
0
    def setUp(self):
        """Login as manager."""
        super(TestPortlet, self).setUp()
        z2.login(self.app['acl_users'], 'admin')
        self.addPythonScript('first', u'First', 'return []')
        self.addPythonScript('second', u'Second', 'return []')
        self.addPythonScript(
            'third', u'Third', u"""
from Products.CMFCore.utils import getToolByName
portal_catalog = getToolByName(context, 'portal_catalog')
return portal_catalog()""")
        self.addPythonScript(
            'fourth', u'Fourth', u"""
from Products.CMFCore.utils import getToolByName
portal_catalog = getToolByName(context, 'portal_catalog')
return {'results':portal_catalog(), 'text':'Additional text', 'icon_url': '/link/to/icon.png'}
""")
        manager = IPythonScriptManager(self.portal)
        manager.rescanScripts()
        manager.enableScript('/plone/second')
        manager.enableScript('/plone/third')
        manager.enableScript('/plone/fourth')

        self.portal.invokeFactory("Folder", "folder")
        folder = self.portal.folder
        folder.setTitle(u'Folder')
        folder.invokeFactory("Folder", "subfolder")
        subfolder = folder.subfolder
        subfolder.setTitle(u'Subfolder')
        subfolder.invokeFactory("Document", "doc")
        doc = subfolder.doc
        doc.setTitle(u'Document')
    def __call__(self):
        """Rescan and redirect."""
        # Check against CSRF.
        CheckAuthenticator(self.request)
        PostOnly(self.request)

        manager = IPythonScriptManager(self.context)
        manager.rescanScripts()
        IStatusMessage(self.request).addStatusMessage(_(u'Rescanned'))
        self.request.response.redirect(self.context.absolute_url() + '/@@manage_pythonscript')
def PythonScriptsVocabFactory(context):
    """Produces Python Scripts vocabulary for given context."""
    portal_url = getToolByName(context, 'portal_url')
    portal = portal_url.getPortalObject()
    manager = IPythonScriptManager(portal)
    terms = [
        SimpleVocabulary.createTerm(name, name, script.title)
        for (name, script) in manager.getEnabledScripts()
    ]
    return SimpleVocabulary(terms)
    def setUp(self):
        """Login as manager."""
        super(TestPortlet, self).setUp()
        z2.login(self.app['acl_users'], 'admin')
        self.addPythonScript('first', u'First', 'return []')
        self.addPythonScript('second', u'Second', 'return []')
        self.addPythonScript('third', u'Third', u"""
from Products.CMFCore.utils import getToolByName
portal_catalog = getToolByName(context, 'portal_catalog')
return portal_catalog()""")
        self.addPythonScript('fourth', u'Fourth', u"""
from Products.CMFCore.utils import getToolByName
portal_catalog = getToolByName(context, 'portal_catalog')
return {'results':portal_catalog(), 'text':'Additional text', 'icon_url': '/link/to/icon.png'}
""")
        manager = IPythonScriptManager(self.portal)
        manager.rescanScripts()
        manager.enableScript('/plone/second')
        manager.enableScript('/plone/third')
        manager.enableScript('/plone/fourth')

        self.portal.invokeFactory("Folder", "folder")
        folder = self.portal.folder
        folder.setTitle(u'Folder')
        folder.invokeFactory("Folder", "subfolder")
        subfolder = folder.subfolder
        subfolder.setTitle(u'Subfolder')
        subfolder.invokeFactory("Document", "doc")
        doc = subfolder.doc
        doc.setTitle(u'Document')
 def getScripts(self):
     """Return list of available scripts descriptions."""
     manager = IPythonScriptManager(self.context)
     for path, info in manager.getScripts():
         data = {
             'path': path,
             'title': info.title,
             'enabled': info.enabled,
             'timing': info.timing
         }
         if info.timing:
             data.update(info.getTiming())
         yield data
    def getItemsAndProps(self):
        """Depending on what is returned by the python script,
        returns a list of results to be rendered inside portlet or
        a dict that contains the iterable value under the 'results' key
        and other properties to be rendered in the portlet.
        """
        portal_url = getToolByName(self.context, 'portal_url')
        portal = portal_url.getPortalObject()
        manager = IPythonScriptManager(portal)
        script_name = self.data.script_name
        try:
            info = manager.getInfo(script_name)
        except KeyError:
            logger.exception(u'Could not find script %r' % script_name)
            return [], {}
        if not info.enabled:
            logger.warning(u'Script %r is not enabled' % script_name)
            return [], {}
        script = manager.getScript(script_name)
        before = time()
        try:
            results = self.run_script(script)
        except Exception:
            logger.exception(u'Error while running script %r' % script_name)
            return [], {}
        else:
            timing = time() - before
            logger.info(u'Script %r executed successfully in %.3f sec' % (script_name, timing))
            if info.timing:
                info.addTiming(timing)

            if isinstance(results, dict):
                if not results.has_key('results'):
                    raise ValueError
                props = results
                results = props.pop('results')
            else:
                props = {}

            limit = self.data.limit_results
            if limit:
                results = results[:limit]
            results = self.wrap_results(results)
            return results, props
 def getScriptManager(self):
     """Return script manager for Plone site."""
     return IPythonScriptManager(self.ploneSite)
 def getScripts(self):
     """Return list of available scripts descriptions."""
     manager = IPythonScriptManager(self.context)
     for path, info in manager.getScripts():
         script = manager.getScript(path)
         yield {"path": path, "title": info.title, "enabled": info.enabled, "errors": self.checkSyntax(script)}
def onPythonScriptAdded(script):
    """Handle adding Python script item - register it with script manager."""
    portal_url = getToolByName(script, 'portal_url')
    plone_site = portal_url.getPortalObject()
    manager = IPythonScriptManager(plone_site)
    manager.addScript(script)