Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to change the order of subscribers? #257

Open
wesleybl opened this issue Mar 28, 2022 · 8 comments
Open

How to change the order of subscribers? #257

wesleybl opened this issue Mar 28, 2022 · 8 comments
Labels

Comments

@wesleybl
Copy link
Member

BUG/PROBLEM REPORT (OR OTHER COMMON ISSUE)

What I did:

In a Plone addon, I registered a subscriber:

  <subscriber for="my.products.contents.mycontent.IMyContent
                              OFS.interfaces.IObjectWillBeRemovedEvent"
                      handler=".handler.myhandler" />

What I expect to happen:

I would like this event of mine to be executed before the subscribers registered by Zope. When I delete an object of mine, the notification occurs:

    notify(ObjectWillBeRemovedEvent(ob, self, id))

In that notification, the event:

  <subscriber
      for=".interfaces.IContentish
           zope.interface.interfaces.IObjectEvent"
      handler=".CMFCatalogAware.handleContentishEvent"
      />

runs before mine. How do I get mine to run sooner?

What actually happened:

Event registered by Zope runs before mine.

What version of Python and Zope/Addons I am using:

Python: 3.8.12
Zope 4.6.3
zope.interface 5.4.0

@wesleybl wesleybl changed the title How to change the order of subscribers How to change the order of subscribers? Mar 28, 2022
@d-maurer
Copy link
Contributor

d-maurer commented Mar 28, 2022 via email

@wesleybl
Copy link
Member Author

Neither approach is easy:
zope.event/zope.interface simply lacks support to easily
control the order in which event handlers are executed.

I just couldn't in any way put my subscriber ahead of Zope's. This would be a great feature for Zope.

What I'm trying to do is prevent the deletion of content, under certain conditions. So I thought I'd make a subscriber that checks this and raise an exception in case the object can't be deleted. It's just that there are subscribers that run before mine, that do things to the object, like uncatalog from catalog.

@d-maurer
Copy link
Contributor

d-maurer commented Mar 29, 2022 via email

@wesleybl
Copy link
Member Author

You can use a specific exception and register a corresponding
error view: to inform the user about the prevented deletion.

If I catch the exception to display a friendly message, there is no rollback of things done by subscribers, like uncatalog.

@d-maurer
Copy link
Contributor

d-maurer commented Mar 30, 2022 via email

@wesleybl
Copy link
Member Author

@d-maurer remembering that I'm in the Plone context. In Plone, I can delete multiple objects at the same time. So it is necessary to catch the error, because some objects can be deleted and others not, in the same request. See:

https://github.com/plone/plone.app.content/blob/a4a7b277bd9b50b9755b23ea72d973a657e9b3f2/plone/app/content/browser/contents/__init__.py#L88-L104

self.action(obj) it could be a delete:

https://github.com/plone/plone.app.content/blob/a4a7b277bd9b50b9755b23ea72d973a657e9b3f2/plone/app/content/browser/contents/delete.py#L66

@d-maurer
Copy link
Contributor

d-maurer commented Mar 30, 2022 via email

@wesleybl
Copy link
Member Author

but other parts could do whatever they want (including
deletion of objects not prevented by your special logic).

Yes, that's why it would be good for validation to be on the subscriber. But for this to work, my subscriber would have to be run before Zope's subscribers. That's why it would be interesting to be able to choose the order of execution of the subscribers. I think I'll open an issue on zope.configuration requesting this feature

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants