简体   繁体   English

在视图后执行的金字塔中添加功能

[英]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) . 之前没问题,我使用@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. 我尝试将它放在一个@subscriber(BeforeRender) ,一个@subscriber(NewResponse) ,并在一个完成的回调中我在NewRequest添加: event.request.add_finished_callback(finished_callback) - 没有我写入会话棒的值。 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: NewResponse状态下的金字塔文档

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. 后处理响应通常在WSGI中间件组件中比在pyramid.interfaces.INewResponse事件调用的订户代码中更好地处理。 [...] [...]

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. 但我迷失了,因为我不知道wsgi那么好并试图找到一个通过谷歌进入的地方没有指向我的任何地方。

Got my solution from the answer by @MikkoOhtamaa, but I wanted the code to be on this page, so here is what I did with some explanation: 从@MikkoOhtamaa的答案得到我的解决方案,但我希望代码在这个页面上,所以这里是我做了一些解释:

This can be achieved with a tween. 这可以通过补间来实现。 That is a function (or other callable), that is called instead of the view and gets the job of calling the view, so you can do stuff just before and after the call. 这是一个函数(或其他可调用的),它被调用而不是视图,并获得调用视图的工作,因此您可以在调用之前和之后执行操作。 Using this I got rid of the @subscriber(NewRequest) as well and put it all in one place. 使用这个我摆脱了@subscriber(NewRequest)并把它放在一个地方。 Imagine this in your projects main init .py, where you create the wsgi-app. 想象一下,在您的项目main init .py中,您可以在其中创建wsgi-app。 The project's name would be myapp . 该项目的名称将是myapp

def values_tween_factory(handler, registry):
    """
    Factory for creating the tween that wraps around the view.
    """
    def values_tween(request):
        """
        This is called in stead of the view with the view as param.
        """
        # do stuff before view code with request and session
        request.some_values = request.session.get('stored_values', [])
        # execute the view, creates the response
        response = handler(request)
        # do stuff after the view code with request and session
        request.session['stored_values'] = request.some_values
        # return the response returned by the view
        return response

    # return the new tween
    return state_tween

# [... other module level stuff ...]


def main(global_config, **settings):
    """
    The main function creating the wsgi-app.
    """
    config = Configurator(settings=settings)

    # [...] other stuff, like DB

    # register the tween - must be done by dotted name
    config.add_tween('myapp.values_tween_factory')

    # ... do more other stuff

    application = config.make_wsgi_app()
    # done - return created application object:
    return application

Tweens (be-tween)允许您在每个请求之前和之后执行代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM