2

I'm having a problem with a doctest because I'm trying to rename the IDs of a content type object in an IObjectAddedEvent handler. My requirement is to have IDs that are sequential and context specific eg CAM-001, CAM-002, BLK-001, BLK-002, etc

When I add a object manually in the browser the event handler renames the id correctly but when I try create it in a doctest it fails soon after it is added to it's container. plone.dexterity addContentToContainer calls _setObject with the original id, then the event handler kicks in and renames the id, and then when _getObject uses original id it obviously can't find the object so it bomb with an attribute error.

I created a product to illustrate this here https://github.com/mikejmets/wt.testrig.

I also tried using plone.api in the doctest but that also fails.

All ideas welcome.

4

1 回答 1

0

由于您使用的是灵巧,因此最好的解决方案是编写自己的NameGenerator行为。

我猜您的 DX 内容已激活以下行为: <element value="plone.app.content.interfaces.INameFromTitle" />

此行为负责在创建后重命名项目。您应该删除它并添加您自己的。

例子:

使用 zcml 注册行为。

<plone:behavior
    title="Special name(id) generator"
    description=""
    provides="dotted.name.to.your.INameGenerator"
    factory="dotted.name.to.your.name_generator.NameGenerator"
    for="dotted.name.to.content.interface"
    />

对应的python代码。

from plone.app.content.interfaces import INameFromTitle
from zope.component import getUtility
from zope.interface import implements


class INameGenerator(INameFromTitle):
    """Behavior interface.
    """


class NameGenerator(object):
    """Customized name from title behavior."
    """

    implements(INameGenerator)


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

    @property
    def title(self):

        # YOUR IMPLEMENTATION
        title = ...

        return title

重要提示:继承你的接口INameFromTitle

现在添加<element value="dotted.name.to.your.INameGenerator" />到您的内容行为。

可能INameFromTitle从您的内容类型中删除行为就足够了,但明确地实现您自己的行为会更好。

于 2014-11-28T12:31:13.110 回答