本文介绍了在before_request信号触发之前,烧瓶碰撞装饰器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Flask并使用before_request修饰器将有关
请求的信息发送给分析系统。我现在试图创建一个装饰,
可以防止在几条路线上发送这些事件。

我遇到的问题是让我的装饰器在before_request
信号被触发前被调用。

$ $ $ $ $ $ $ def $ exclude $ * args,** kwargs):
print装饰函数之前
返回func(* args,exclude_from_analytics = True,** kwargs)

返回包装

#------------------------

@exclude_from_analytics
@ app.route('/ ')
def index():
return make_response('..')

#------------------ ------

@ app.before_request
def analytics_view(* args,** kwargs):
如果在kwargs和kwargs中有'exclude_from_analytics'['exclude_from_analytics']是真的:
return


解决方案

装饰器简单地把一个属性放在函数上(在我的例子中,我使用 _exclude_from_analytics 作为属性)。我发现使用和。



如果找不到属性在端点上,您可以忽略分析。

 从瓶子导入瓶子,请求

app =瓶子(__name__)

def exclude_from_analytics(func):
func._exclude_from_analytics = True
return func

@ app.route('/ a')
@exclude_from_analytics
def a():
返回'a'

@ app.route('/ b')
def b()
返回'b'

@ app.before_request
def analytics_view(* args,** kwargs):
#默认为任何你想要的。
run_analytics = True

#如果你愿意,你可以在这里处理404s。
如果request.ndpoint在app.view_functions中:
view_func = app.view_functions [request.endpoint]
run_analytics =不是hasattr(view_func,'_exclude_from_analytics')

格式(request.path,run_analytics)

app.run(debug = True)
应该运行{0}:{1}

输出(忽略静态文件...)

 应该在/ a上运行分析:False 
127.0.0.1 - - [24 / Oct / 2013 15:55:15]GET / a HTTP / 1.1200 -
运行分析/ b:True
127.0.0.1 - [24 / Oct / 2013 15:55:18]GET / b HTTP / 1.1200 -

我还没有测试过,看看是否适用蓝图。此外,包装并返回NEW函数的装饰器可能会导致此操作无法工作,因为该属性可能被隐藏。


I'm using Flask and using the before_request decorator to send information aboutrequests to an analytics system. I'm now trying to create a decorator that wouldprevent sending these events on a few routes.

The problem I'm running into is getting my decorator to get called before the before_requestsignal gets fired.

def exclude_from_analytics(func):

    @wraps(func)
    def wrapped(*args, **kwargs):
        print "Before decorated function"
        return func(*args, exclude_from_analytics=True, **kwargs)

    return wrapped

# ------------------------

@exclude_from_analytics
@app.route('/')
def index():
    return make_response('..')

# ------------------------

@app.before_request
def analytics_view(*args, **kwargs):
    if 'exclude_from_analytics' in kwargs and kwargs['exclude_from_analytics'] is True:
       return
解决方案

You can use the decorator to simply put an attribute on the function (in my example below, I'm using _exclude_from_analytics as the attribute). I find the view function using a combination of request.endpoint and app.view_functions.

If the attribute is not found on the endpoint, you can ignore analytics.

from flask import Flask, request

app = Flask(__name__)

def exclude_from_analytics(func):
    func._exclude_from_analytics = True
    return func

@app.route('/a')
@exclude_from_analytics
def a():
    return 'a'

@app.route('/b')
def b():
    return 'b'

@app.before_request
def analytics_view(*args, **kwargs):
    # Default this to whatever you'd like.
    run_analytics = True

    # You can handle 404s differently here if you'd like.
    if request.endpoint in app.view_functions:
        view_func = app.view_functions[request.endpoint]
        run_analytics = not hasattr(view_func, '_exclude_from_analytics')

    print 'Should run analytics on {0}: {1}'.format(request.path, run_analytics)

app.run(debug=True)

The output (ignoring static files...)

Should run analytics on /a: False
127.0.0.1 - - [24/Oct/2013 15:55:15] "GET /a HTTP/1.1" 200 -
Should run analytics on /b: True
127.0.0.1 - - [24/Oct/2013 15:55:18] "GET /b HTTP/1.1" 200 -

I have not tested to see if this works with blueprints. Additionally, a decorator that wraps and returns a NEW function could cause this to not work since the attribute might be hidden.

这篇关于在before_request信号触发之前,烧瓶碰撞装饰器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 18:53