[Product-Developers] IObjectModifiedEvent, object modified or added?

David Glick davidglick at groundwire.org
Thu Jul 28 20:21:36 UTC 2011

On 7/28/11 12:54 PM, Gilles Lenfant wrote:
> Le 28 juil. 2011 à 16:15, imran.azad a écrit :
>> Hi,
>> How can I workout if an object has been 'modified' or whether it's a new
>> object within the IObjectModifiedEvent event? The issue is that
>> IObjectModifiedEvent is trigged even if an object is added. I need the code
>> below to execute only when an object is 'modified:
> You could mark the creation in the request. I know this seems somehow overkill but, assuming the IObjectCreatedEvent comes first...
> @grok.subscribe(IPerson, IObjectCreatedEvent)
> def markCreationOnly(person, event):
>      # Other stuffs ?
>      request = person.REQUEST
>      request.set('__created_person__', person.getPhysicalPath())
>> @grok.subscribe(IPerson, IObjectModifiedEvent)
>> def rename(person, event):
>      request = person.REQUEST
>     created = request.get('__created_person__', False)
>     if created and person.getPhysicalPath() == created:
>         # Object created and modified
>         return
>>     id = person.getId()
>>     parent = person.aq_parent
>>     parent.manage_renameObject(id, baseNormalize(person.title))
>> Thanks
Another possible approach would be to check if the item has a non-None 
_p_oid attribute. That should only be set if the object has been 
committed to the ZODB. (But test and make sure it works for your 
case...there may be other scenarios like if savepoints are used.)

David Glick
 Web Developer
 davidglick at groundwire.org

Online tools and strategies for the environmental movement. 

Sign up for our newsletter: http://www.groundwire.org/email-capture

More information about the Product-Developers mailing list