3

在 Plone 4.3.3 下,我尝试在我的一个产品中自定义原型内容类型的类方法。

我有bsw.produit_1一个内容类型MyContent定义如下的产品:

class MyContent(base.ATCTContent):

    implements(IMyContent)

    meta_type = "MyContent"
    schema = MyContent`

    def ma_fonction(self):

        ......
        return res

我想ma_fonction在另一个产品中修改我的功能代码。我尝试使用适配器并遵循 plone 文档,但没有成功。

我希望自定义功能的类:

class CustomClass(object):
    """  """

    implements(IMyContent)
    adapts(IMyContent)

    def at_post_payment_script(self, obj_transaction):
        """ """

            ......
            # My new code
            return res

configure.zcml我声明我的适配器的地方:

  <adapter for="bsw.produit_1.content.mycontent.MyContent"
           provides="bsw.produit_1.interfaces.IMyContent"
           factory=".customclass.CustomClass" />

在我的 zcml 声明中,我还尝试archetypes.schemaextender.interfaces.ISchemaExtender使用 asprovides或 put 接口IMyContentfor不是类。

这些都不起作用,每次都不会执行自定义代码。有人对此有解决方案吗?

4

2 回答 2

5

您需要的解决方案取决于您想要实现的目标。

但是 archetypes.schemaextender 是错误的解决方案。schemaextender 可以修改架构,包括:

  • 字段顺序
  • 字段/小部件属性
  • 图式
  • 字段的设置器/获取器
  • 新领域
  • 覆盖字段

实现自己的适配器绝对是正确的方法。

首先,您需要为默认行为实现一个适配器。其次,您需要调整上下文和请求。other该请求很重要,因为如果您的产品已安装,这是一种定义更具体的适配器的方法。

默认实现的 Python 代码(adapter.py):

from zope.component import adapts
from zope.interface import Interface
from zope.interface import implements


class IBehavior(Interface):
    def __init__(context, request)
        """Adapts context and request"""

    # ... more ...


class DefaultBehavior(object):
    implements(IBehavior)
    adapts(IMyContent, Interface)  # IMPORTAN two discriminators 

    def __init__(self, context, request):
        self.context = context
        self.request = request

    def __call__(self):

        # your default implementation goes here.

使用 zcml 注册适配器:

<adapter factory=".adapter.DefaultBehavior" />

您现在可以调用默认适配器ma_fonction

from zope.component import getMultiAdapter


class MyContent(base.ATCTContent)

    def ma_fonction(self):
        adapter = getMultiAdapter((self, self.REQUEST), IDefaultBehavior)
        return adapter()

other现在您可以使用浏览器层在您的产品中实现更具体的适配器。检查文档, 如何注册浏览器层

在你的other包中,你现在可以注册一个实现相同IBehavior接口的适配器,但也适应你的浏览器层。

from other.package.interfaces import IOtherPackageLayer
from zope.component import adapts
from zope.interface import implements


class DifferenBehavior(object):
    implements(IBehavior)
    adapts(IMyContent, IOtherPackageLayer)  # IMPORTAN adapt the browserlayer not Interface 

    def __init__(self, context, request):
        self.context = context
        self.request = request

    def __call__(self):

        # your different implementation goes here.

也注册 zcml:

<adapter factory=".adapter.DifferenBehavior" />

如果未安装软件包,您ma_fonction现在调用默认适配器。other如果other安装了软件包,则使用不同的适配器。

于 2015-03-12T19:46:15.810 回答
1

您可以使用的最简单的方法(尽管在政治上不正确!)是猴子补丁

看一下collective.monkeypatcher,您只需要这样的配置(在您的第3 方产品中):

<monkey:patch
    description=""
    class="your.package.MyContent"
    original="ma_fonction"
    replacement=".monkeys.new_ma_fonction"
    />

然后在你的包中创建一个monkeys.py包含新方法的模块:

def new_ma_fonction(self):
    # do stuff
    return res
于 2015-03-13T15:56:29.250 回答