在Plone插件中包含Python脚本
我有一个通过Zope创建的Plone插件,这个插件里面包含了Javascript和页面模板文件。有些Javascript函数需要通过AJAX调用Python脚本,我该如何在我的插件中包含这些Python脚本,而不需要通过ZMI呢?
我有一个“browser”文件夹,里面有一个“configure.zcml”文件,用来注册资源目录和我的模板文件。我想,注册Python文件应该和这个过程差不多,或者和注册Javascript文件的方式类似,但可能不是这样?
1 个回答
7
你需要把你的Python代码注册为内容对象的视图:
<browser:page
for="**INTERFACE**"
name="**name**"
class="**class**"
attribute="**method**"
permission="zope2.View"
/>
这里的INTERFACE是你想要查看的对象的接口,name是视图的名称(比如说,http://path-to-object/@@name
),class是你定义脚本的Python类,而attribute是这个类的一个可选方法(默认是__call__)。严格来说,我认为class可以是任何可调用的东西,不一定非得是类的方法。
这是我用来处理kss动作的脚本(其实和自己写AJAX脚本差不多)——你的类可能需要继承自BrowserView(PloneKSSView是专门为KSS视图定制的一个版本):
<browser:page
for="Products.VirtualDataCentre.interfaces.IDDCode"
name="getTableColumns"
class="Products.VirtualDataCentre.browser.DDActions.DDActions"
attribute="getTableColumns"
permission="zope2.View"
/>
其中IDDCode是我需要视图的内容类型,而DDActions.py里包含:
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]))