Quantcast

IObjectModifiedEvent, object modified or added?

classic Classic list List threaded Threaded
5 messages Options
Imran Azad Imran Azad
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

IObjectModifiedEvent, object modified or added?

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:

@grok.subscribe(IPerson, IObjectModifiedEvent)

def rename(person, event):
    id = person.getId()
    parent = person.aq_parent
    parent.manage_renameObject(id, baseNormalize(person.title))

Thanks
Gilles Lenfant-2 Gilles Lenfant-2
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: IObjectModifiedEvent, object modified or added?

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
>
> --
> View this message in context: http://plone.293351.n2.nabble.com/IObjectModifiedEvent-object-modified-or-added-tp6630019p6630019.html
> Sent from the Product Developers mailing list archive at Nabble.com.
> _______________________________________________
> Product-Developers mailing list
> [hidden email]
> https://lists.plone.org/mailman/listinfo/plone-product-developers

_______________________________________________
Product-Developers mailing list
[hidden email]
https://lists.plone.org/mailman/listinfo/plone-product-developers
Imran Azad Imran Azad
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: IObjectModifiedEvent, object modified or added?

Hi Gilles,

Thanks for that, that's quite an interesting implementation, in the end I decided to do it like so:
@grok.subscribe(IPerson, IObjectModifiedEvent)
def rename(person, event):
    id = person.getId()
    parent = person.aq_parent
    if not hasattr(person, 'newName'):  
       parent.manage_renameObject(id, id + "-old")

What do you think? I kind feel like it's a hack, makes me feel very uncomfortable.
David Glick (GW) David Glick (GW)
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: IObjectModifiedEvent, object modified or added?

In reply to this post by Gilles Lenfant-2
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
 [hidden email]
 206.286.1235x32

Online tools and strategies for the environmental movement.

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


_______________________________________________
Product-Developers mailing list
[hidden email]
https://lists.plone.org/mailman/listinfo/plone-product-developers
Imran Azad Imran Azad
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: IObjectModifiedEvent, object modified or added?

Thanks David, much appreciated.
-----Original Message-----
From: "David Glick-3 [via Plone]" <[hidden email]>
To: Imran Azad <[hidden email]>

Sent: 28/07/2011 21:23:20
Subject: Re: IObjectModifiedEvent, object modified or added?



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
 [hidden email]
 206.286.1235x32

Online tools and strategies for the environmental movement.

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


_______________________________________________
Product-Developers mailing list
[hidden email]
https://lists.plone.org/mailman/listinfo/plone-product-developers


_______________________________________________
If you reply to this email, your message will be added to the discussion below:
http://plone.293351.n2.nabble.com/IObjectModifiedEvent-object-modified-or-added-tp6630019p6631229.html

To unsubscribe from IObjectModifiedEvent, object modified or added?, visit Hospital needs YOU! We need you to register as a Public, Patient or Staff member and help shape the future of your healthcare. Call 0800 280 2581 to register by phone or you can email  [hidden email]  and ask to be registered as a new member.  For more information about our hospitals and Foundation Trust Membership please visit www.bradfordhospitals.nhs.uk
This message is confidential. It may also contain privileged information. The contents of this e-mail and any attachments are intended for the named addressee only. Unless you are the named addressee or authorised to receive the e-mail of the named addressee you may not disclose, use or copy the contents of the e-mail. If you are not the person for whom the message was intended, please notify the sender immediately at Bradford Teaching Hospitals NHS Foundation Trust and delete the material from your computer. You must not use the message for any other purpose, nor disclose its contents to any person other than the intended recipient. Bradford Teaching Hospitals NHS Foundation Trust does not accept responsibility for this message and any views or opinions contained in this e-mail are solely those of the author unless expressly stated otherwise.

Loading...