[Solved] Adding Event Listner for a behavior

After hours and hours…

To me, this is quite confusing

What happens is that if something is added to a folderish content type, the modified event fires.

So, for example: in my 'Movie' content type I planned to make a 'gallery' with images added to the folderish content type. The movie content type is based on wildcard.media, and every time I add an image, it will fetch the preview thumbnail from youtube.

After discovering this, I made an ugly 'check'. This seems to work (but there is still an error about 'Attempt to unindex nonexistent object'

My code now is this:

def make_dates(object, event):
    """Make date content items"""

    if str(event).startswith("<zope.lifecycleevent.ObjectModifiedEvent"):
        #check how long the event lasts
        event_time = object.end - object.start

        old_dates = object.listFolderContents(contentFilter={"portal_type" : "dato"})

        if len(old_dates) > 0:
            api.content.delete(objects=old_dates)

        if object.dates:
            _create_dates(object, object.dates, object.title, event_time)


def _create_dates(object, dates, title, event_time):
    for date in dates:
        #all events are calculated as same lenght
        my_date = api.content.create(
            type='dato',
            container=object,
            title=date.isoformat(),
            start=date,
            end= date+event_time,
            )
        #api.content.transition(obj=my_date, transition='publish')

UPDATE: the dates need to be 'localized' to show up in the calendar, maybe something like:

pytz.timezone(default_timezone()).localize(date)