lightningmanic
lightningmanic

Reputation: 2165

Include Python scripts with Plone Add-on

I have a Plone Add-on (created through Zope) that includes Javascript and page template files. Some of the Javascript functions need to call Python scripts (through AJAX calls) - how to I include these Python scripts in my add-on without going through the ZMI?

I have a "browser" folder which contains a "configure.zcml" file - registering the resource directories and my template files. I would assume registering python files would be similar to this, or similar to the way Javascript files get registered, but perhaps not?

Upvotes: 3

Views: 588

Answers (1)

Auspex
Auspex

Reputation: 2254

You register your python as Views on the content object:

<browser:page
 for="**INTERFACE**"
 name="**name**"
 class="**class**"
 attribute="**method**"
 permission="zope2.View"
 />

Where INTERFACE is an interface of the object you want to have the view of, name is the view name (ie, http://path-to-object/@@name), class is the Python class where your script is defined, and attribute is an optional method of the class (it defaults to __call__). Strictly, I think class is any callable, not necessarily a method of a class.

This is a script I use for a kss action (pretty much the same thing as writing your own AJAX scripts) - your class may need to inherit from BrowserView (PloneKSSView is a specialization of that for KSS views):

<browser:page
 for="Products.VirtualDataCentre.interfaces.IDDCode"
 name="getTableColumns"
 class="Products.VirtualDataCentre.browser.DDActions.DDActions"
 attribute="getTableColumns"
 permission="zope2.View"
 />

where IDDCode is the content type on which I need the view, and DDActions.py has:

from Products.Five import BrowserView
from plone.app.kss.plonekssview import PloneKSSView
class DDActions(PloneKSSView):
    def getTableColumns(self, table, currValue, currLabel):
        columns = self.context.getColumnNames(table)
        for (field, curr) in [('valueColumn', currValue), ('labelColumn',currLabel)]:
            self.replaceSelect(field, columns, (curr or self.context[field]))

Upvotes: 7

Related Questions