Plone - 覆盖Zope Schema字段

时间:2013-01-30 10:18:49

标签: python schema plone zope

在使用Plone时,我已将DocumentViewer product集成到我的Plone应用程序中,以帮助查看PDF文件。文档查看器附加产品定义了一组模式/字段,可以在控制面板设置中查看,即站点设置 - > 文档查看器设置

您可以查看字段/架构的定义方式here

现在我想通过在example.product中覆盖它来向IGlobalDocumentViewerSettings接口添加另一个字段。

我认为我不能使用SchemaExtender,因为它们不是Archetypes。我也尝试按照by this link提供的说明进行操作,但无济于事。我可以重新安装我的产品,但我添加的字段未显示。

以下是我的代码示例:

from collective.documentviewer.interfaces import IGlobalDocumentViewerSettings
from collective.documentviewer.interfaces import IDocumentViewerSettings
from zope import schema
from zope.interface import implements

class DocViewerSchemaProvider(object):
    implements(IGlobalDocumentViewerSettings)

    def getSchema(self):
        """
        """
        return IEnhancedDocumentViewerSchema

class IEnhancedDocumentViewerSchema(IDocumentViewerSettings):
    """ 
    Use all the fields from the default schema, and add various extra fields.
    """

    folder_location = schema.TextLine(
        title=u"Default folder location",
        description=u'This folder will be created in the Plone root folder. '
                    u'Plone client must have write access to directory.',
        default=u"files_folder")

任何人都可以帮助我如何覆盖这个特定的界面吗?

1 个答案:

答案 0 :(得分:3)

由于作者(我)没有简单地覆盖它,所以进行覆盖会有点复杂。您需要执行以下步骤。警告,它是所有伪代码,所以你需要调整,以使它适合你。

首先,通过扩展您想要自定义的界面来提供您的自定义界面:

class IEnhancedDocumentViewerSchema(IGlobalDocumentViewerSettings):
    """ 
    Use all the fields from the default schema, and add various extra fields.
    """

    folder_location = schema.TextLine(
        title=u"Default folder location",
        description=u'This folder will be created in the Plone root folder. '
                    u'Plone client must have write access to directory.',
        default=u"files_folder")

然后,创建用于存储和检索schema ::

设置的设置适配器
from collective.documentviewer.settings import Base
class CustomSettings(Base):
    implements(IEnhancedDocumentViewerSchema)
    use_interface = IEnhancedDocumentViewerSchema

然后,注册您的适配器::

<adapter 
    for="Products.CMFPlone.interfaces.IPloneSiteRoot"
    provides="my.product.interfaces.IEnhancedDocumentViewerSchema"
    factory=".somewhere.CustomSettings" />

然后,使用自定义架构创建表单::

from z3c.form import field
from plone.app.z3cform.layout import wrap_form
from collective.documentviewer.views import GlobalSettingsForm
class CustomGlobalSettingsForm(GlobalSettingsForm):
    fields = field.Fields(IEnhancedDocumentViewerSchema)
CustomGlobalSettingsFormView = wrap_form(CustomGlobalSettingsForm)

然后,为您的产品创建自定义图层,从而扩展文档浏览器图层。这将需要两个步骤。首先,添加图层接口::

from collective.documentviewer.interfaces import ILayer as IDocumentViewerLayer
class ICustomLayer(IDocumentViewerLayer):
    """
    custom layer class
    """

使用通用设置注册您的图层。使用以下内容将xml文件browserlayer.xml添加到您的配置文件中(确保重新安装产品,以便层注册)::

<?xml version="1.0"?>
<layers name="" meta_type="ComponentRegistry">
    <layer name="my.product" 
           interface="my.product.interfaces.ICustomLayer" />
</layers>

最后,使用您的自定义表单覆盖全局设置视图,仅适用于您为产品注册的图层::

<browser:page
    name="global-documentviewer-settings"
    for="Products.CMFPlone.interfaces.IPloneSiteRoot"
    class=".somewhere.CustomGlobalSettingsFormView"
    layer=".interfaces.ICustomLayer"
    permission="cmf.ManagePortal" />
哇,这太难了。