Search code examples
pythonsessionpyramidwsgi

Add function in pyramid that executes after view


How do I add code to my pyramid app that is executed after the code in the view?

I need to do something to my beaker session before and after the view code. Before is no problem, I use a @subscriber(NewRequest). All the ways I tried so far seem to happen too late (values I write to the session do not seem to be saved, although code is executed, as I can see in the log).

I tried putting it in a @subscriber(BeforeRender), a @subscriber(NewResponse), and in a finished callback I add in the NewRequest: event.request.add_finished_callback(finished_callback) – None of the values I write to the session stick. Only the one I added as last line in the view handler does (but I will not write that line in all of my views).

The pyramid docs on NewResponse state:

Postprocessing a response is usually better handled in a WSGI middleware component than in subscriber code that is called by a pyramid.interfaces.INewResponse event. [...]

But I'm lost on that, since I don't know wsgi that well and trying to find a spot to enter via google did not point my anywhere.


Solution

  • Tweens (be-tween) allow you to execute code before and after each request.