How to perform actions after creating content types?

I am trying to execute arbitrary code after creating a Dexterity content type. For example, the type of content may 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 “Horse Creation” magazine message printed on the console when the application runs in the foreground. But the horse is created, and I do not receive messages from her. I assume that content type objects are created using __init__ , but maybe I'm wrong.

+6
source share
1 answer

You are connected to __init__ for a schema of your content type. The schema is used as the basis for the fields that populate your content, but this is not the content type type itself.

If you want to connect to the creation of a content type, instead register event subscribers :

 from zope.app.container.interfaces import IObjectAddedEvent @grok.subscribe(IHorse, IObjectAddedEvent) def logHorseCreated(horse, event): logger.info('Created a horse') 

If you really need to configure the initialization of the content item in the __init__ method, you need 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) 
+7
source

All Articles