开发者

Which event should I use just before a page is shown on browser on Plone to trigger a subscriber?

I want to create a subscriber that gets triggered when the user tries to access the resource (which is a custom content-type). So, the object is not being added, modified, nothing, is just being traversed. Something like a Zope View Event.

So, basically, suppose a custom content type has a custom workflow (two states: private and viewed). The initial state is private. This content type is only going to be created programatically, using _createObjectByType by anonymous users. Suppose an object called myobjet was added, programatically, to the root folder of my Plone site.

What I want is: when the user access

http://localhost:8080/Plone/myobject

...it automatically changes the state of the workflow of this object to viewed. The url http://localhost:8080/Plone/myobject is going to be a custom view, not the default base_edit.

Which event should I use? I tried IEndRequestEvent and IBeforeTraverseEvent from this list and none of them work: the handler is not being called for my custom object interface.

I've tried other events with my custom object interface (like IObjectEditedEvent), and, for this event, my handler is开发者_运维问答 called when I edit an object that implements the interface. But using IEndRequestEvent and IBeforeTraverseEvent doesn't call the handler.

IEndRequestEvent and IBeforeTraverseEvent only work when I set the subscriber to all interfaces:

  <subscriber
     for="*
          zope.app.publication.interfaces.IBeforeTraverseEvent"
     handler=".subscriber.myhandler"
     />

And when I make myhandler print the object and the event in this situation, it shows:

<PloneSite at Plone>
<zope.app.publication.interfaces.BeforeTraverseEvent object at 0xd52618c>

If the solution is to write an event myself, is there an easy tutorial for this?


You might want to have a look at http://pypi.python.org/pypi/plone.validatehook.

Make sure you bind the event to the right interface. If you bind it to "Interface" (as described on the plone.validatehook pypi page) the event will get called for every single request. In order to restrict the event to contentish objects you can do the following:

from Products.CMFCore.interfaces import IContentish

@adapter(IContentish, IPostValidationEvent)
def RedirectMember(object, event):
    ...

(Edit: I removed my first answer because it didn't work)


Not sure what this subscriber is supposed to do, but if the object is not being modified, added or whatsoever than I must suspect it will just be viewed...so why not just use the __call__ method of the items view (or the __update__ method if you are using five.grok/dexterity)?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜