【问题标题】:Plone - Override Zope Schema fieldsPlone - 覆盖 Zope Schema 字段
【发布时间】:2013-01-31 05:22:04
【问题描述】:

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

您可以查看字段/模式是如何定义的here

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

我认为我不能使用SchemaExtender,因为它们不是原型。我也尝试按照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")

谁能帮助我如何覆盖这个特定的界面?

【问题讨论】:

    标签: python schema plone zope


    【解决方案1】:

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

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

    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")
    

    然后,创建用于存储和检索架构设置的设置适配器::

    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)
    

    然后,为您的产品创建一个自定义层,扩展文档查看器层。这将需要 2 个步骤。首先,添加图层接口::

    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" />
    

    哇,太难了。

    【讨论】:

    • 那么毕竟,你为什么还需要这个字段呢?您打算如何与文档查看器集成?您是否还要覆盖转换过程?
    • 我有一个包含内容类型的自定义产品。内容类型允许 PDF 附件。所以我需要一种在查看器中查看 PDF 附件的方法。我已经完成了这项工作,并且已经覆盖了转换过程。由于您的转换仅适用于 Plone File 对象,因此我必须将附件转换为 Plone File 内容类型才能进行转换。但是转换后的文件必须存储在某个地方,即在 Plone 文件夹中。所以我要添加一个配置设置来指定文件将存储在哪个文件夹中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多