如何在创建内容类型后执行操作?

Dan*_*dez 6 plone dexterity

我正在尝试在创建Dexterity内容类型后执行一些任意代码.例如,内容类型可以代表马.

import logging
logger = logging.getLogger("Plone")

class IHorse(form.Schema):

    def __init__(self, context):
        logger.info('Creating horse')
        super(self).init(self, context)
Run Code Online (Sandbox Code Playgroud)

我想在前台运行应用程序时在控制台中打印记录器消息"Creating horse".但马是创造的,我没有得到它的消息.我猜内容类型对象是由它创建的__init__,但也许我错了.

Mar*_*ers 7

您已连接到内容类型__init__架构.模式用作填充内容的字段的基础,但它不是内容类型类本身.

如果要挂钩内容类型创建,请注册事件订阅者:

from zope.app.container.interfaces import IObjectAddedEvent

@grok.subscribe(IHorse, IObjectAddedEvent)
def logHorseCreated(horse, event):
    logger.info('Created a horse')
Run Code Online (Sandbox Code Playgroud)

如果您真的必须__init__方法中自定义内容项初始化,则必须创建自己的自定义内容类.

from plone.dexterity.content import Item

class Horse(Item):
    def __init__(self, id=None):
        super(Horse, self).__init__(id)
Run Code Online (Sandbox Code Playgroud)