How to execute actions after creating content types?
Asked Answered
I

1

6

I'm trying to execute some arbitrary code after a Dexterity content type is created. For example the content type could represent a horse.

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

class IHorse(form.Schema):

    def __init__(self, context):
        logger.info('Creating horse')
        super(self).init(self, context)

I want to get the logger message "Creating horse" printed in the console when running the app in foreground. But the horse is created and I don't get messages for it. I guess that content type objects are created by the __init__, but maybe I'm in a mistake.

Innsbruck answered 26/2, 2013 at 20:50 Comment(0)
M
7

You hooked into the __init__ for the schema of your content type. The schema is used as the basis for the fields populating your content, but it's not the content type class itself.

If you want to hook into content type creation, you register event subscribers instead:

from zope.app.container.interfaces import IObjectAddedEvent

@grok.subscribe(IHorse, IObjectAddedEvent)
def logHorseCreated(horse, event):
    logger.info('Created a horse')

If you really have to customize the content item initialization in an __init__ method, you'd have to create your own custom content class instead.

from plone.dexterity.content import Item

class Horse(Item):
    def __init__(self, id=None):
        super(Horse, self).__init__(id)
Metathesize answered 26/2, 2013 at 20:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.