Trigger Python Code when saving a Dexterity Object

I am not sure if I overlooked something in the docus, but I wonder if it is possible to trigger the execution of some Python code, when a DX Type Object ist created and saved.

We have made on own DX Type, hat is derivated form the Event Type with additional Fields, so that users should be able enter their Event themselves. Two new fields are e.g. the City and the Type of Event.
We want to list the events based on those Informations (All events of Type A, all events in City ABC ...) Untill now we have created standard Event Type Objects by a python Script that gets input from an ICS File and place City + Type-of-event into the subject field of the object (categories) so that it was indexed and found thru a catalog search. Now we want, that a python code that is triggered thru the save button copies those fields into the subject field.
I understand that we could use collective.dexteritytextindexer to index the new fields, but to reuse parts of our python scipt would be easier.

For all DX Types you can register event subscribers, more in the docs:

<!-- configure.zcml-->
<subscriber
  zcml:condition="installed zope.lifecycleevent"
  for="plone.dexterity.interfaces.IDexterityItem
     zope.lifecycleevent.ObjectAddedEvent"
  handler=".subscribers.item_added" />

<subscriber
  zcml:condition="installed zope.lifecycleevent"
   for="plone.dexterity.interfaces.IDexterityContainer
     zope.lifecycleevent.ObjectAddedEvent"
   handler=".subscribers.container_added" />

<subscriber
  zcml:condition="installed zope.lifecycleevent"
  for="plone.dexterity.interfaces.IDexterityItem
    zope.lifecycleevent.IObjectModifiedEvent"
  handler=".subscribers.item_modified" />

<subscriber
  zcml:condition="installed zope.lifecycleevent"
  for="plone.dexterity.interfaces.IDexterityContainer
     zope.lifecycleevent.IObjectModifiedEvent"
  handler=".subscribers.container_modified" />

<subscriber
  zcml:condition="installed zope.lifecycleevent"
  for="plone.dexterity.interfaces.IDexterityContainer
     zope.lifecycleevent.IObjectModifiedEvent"
  handler=".subscribers.container_modified" />
# subscribers.py
def item_modified(obj, event):
    do_what_you_want(obj=obj)

def container_modified(obj, event):
    do_what_you_want(obj=obj)

def item_added(obj, event):
    do_what_you_want(obj=obj)

def container_added(obj, event):
    do_what_you_want(obj=obj)
2 Likes

Plone Foundation Code of Conduct