- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
这是我想组合的两个装饰器,因为它们非常相似,不同之处在于如何处理未经过身份验证的用户。我更希望有一个可以用参数调用的装饰器。
# Authentication decorator for routes
# Will redirect to the login page if not authenticated
def requireAuthentication(fn):
def decorator(**kwargs):
# Is user logged on?
if "user" in request.session:
return fn(**kwargs)
# No, redirect to login page
else:
redirect('/login?url={0}{1}'.format(request.path, ("?" + request.query_string if request.query_string else '')))
return decorator
# Authentication decorator for routes
# Will return an error message (in JSON) if not authenticated
def requireAuthenticationJSON(fn):
def decorator(**kwargs):
# Is user logged on?
if "user" in request.session:
return fn(**kwargs)
# No, return error
else:
return {
"exception": "NotAuthorized",
"error" : "You are not authorized, please log on"
}
return decorator
目前我正在将这些装饰器用于特定路线,例如
@get('/day/')
@helpers.requireAuthentication
def day():
...
@get('/night/')
@helpers.requireAuthenticationJSON
def night():
...
我更喜欢这个:
@get('/day/')
@helpers.requireAuthentication()
def day():
...
@get('/night/')
@helpers.requireAuthentication(json = True)
def night():
...
我在 python 3.3 上使用 Bottle 框架。有可能做我想做的事吗?怎么办?
最佳答案
只需添加另一个包装器来捕获 json
参数:
def requireAuthentication(json=False):
def decorator(fn):
def wrapper(**kwargs):
# Is user logged on?
if "user" in request.session:
return fn(**kwargs)
# No, return error
if json:
return {
"exception": "NotAuthorized",
"error" : "You are not authorized, please log on"
}
redirect('/login?url={0}{1}'.format(request.path, ("?" + request.query_string if request.query_string else '')))
return wrapper
return decorator
我已将您原来的 requireAuthentication
函数重命名为 decorator
(因为那是该函数所做的,它装饰了 fn
)并重命名了旧的 decorator
到 wrapper
,通常的约定。
无论你在 @
之后放置什么,它都是一个表达式,首先求值以找到实际的装饰器函数。 @helpers.requireAuthentication()
表示您要调用 requireAuthentication
并且它的返回值 然后用作函数的实际装饰器 @
行适用于。
关于python - 将两个 python 装饰器合二为一,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17088072/
我有这个, public enum Condition : uint // bitwise { None = 0, NewLine = 1, Space = 2 } Rule.
两条防线,一个函数: 试问你如何能保证客户端和服务器端具有相同的功能?表单域的验证闪现在我们眼前。别人把你的html复制到另外一个脚本,然后改变客户端的表单域验证--这并不是一件难事。摆
当我们打开此工作簿时,此代码将查看一个范围并找到包含文本“RCA Pending”的单元格,并将弹出一个 MsgBox,让用户知道该列中的哪一行包含特定文本。问题是,如果有多行包含此文本,也会有多个
我有一个 pandapower 网络,在一些公交车上,我将电池和光伏发电作为 FMU。现在我有一个主算法,可以在不同 FMU 的输入和输出之间创建连接并运行模拟。 我的问题是是否有可能将所有内容组合到
2 个不同的 jscript 合而为一: 我有 2 个 html 文件,其中有 2 个不同的 jscript。我想用 2 个 html 页面制作一个页面。我想在 1.html 按钮中添加 2.html
我的问题很简单,就是想把两张表拼成一张,不PK第一张表完全不同,它们完全不同 table1. table2. |в|q| |@|John | |ы|a|
我想弄清楚是否有办法将我的两个类合二为一。我的程序只是一个循环链表,我必须使用尽可能少的类。我还需要一些帮助来设置一种方法,让我用另一个节点替换一个节点。这是我的两个类(class)有什么想法吗? 第
我有 NSArray 和 NSArray。我想把所有的 NSArray 合二为一。 我认为有一个简单的函数可以使用,不是吗? 这是我收到的: ( ( n0eGi1KJW
向所有 CSS 专家致敬:D 我遇到了这种情况(在移动设备上): one two three four 我想,当通过@media处于横向模式时,将其更改为这样(不显示:无等): o
我想做的是将两个 or 查询与一个 and 查询结合起来 WHERE (id contains[cd] %@ OR name contains[cd] %@) AND manu = %@" 到目前为止
我正在尝试为游戏创建背景并有两个背景图像。我可以使用两个不同的 SKSpriteNode()、SKTexture、SKAction.sequence..etc 和一些数学将它们拼接在一起,但结果有点不
我有一个 HICON,我想将其用作另一个 HICON 的叠加层,以创建结果 HICON。结果 HICON 然后将用于“所有者绘制”控件(注意:它不使用图像列表)。覆盖图标具有透明颜色 RGB(0, 2
我正在修复我们在我们的一个网站上使用的模板,该模板具有以下代码 此代码段有效。 GS_googleAddAdSenseService("ca-pub-123"); GS_googleEna
我有以下 gulp 任务: gulp.task('app-scripts', function() { return getEnvScriptsStream() .pipe(
Perl 模板工具包 - 如何加入/连接多个变量(合为一个)例如,在 html 选择字段/组合中选择所选元素时需要它。 我在这里找到问题 https://www.perlmonks.org/?disp
我有一个 super 简单的网站,我想在本地托管。 我希望能够从网站触发windows程序并将结果返回给网站。 How can I accomplish this? Is this even poss
我遇到了这个问题,我在 中有一个 Logo 和一些导航链接。当我使用 CSS text-align:center; Logo 位于中心的一行,导航链接位于其正下方的另一行。 理想情况下,我希望它们并
我需要一个同时是 HashMap 和 ArrayList 的类。 为什么需要HashMap?根据键快速访问对象。 为什么我需要 ArrayList?随机选择一个元素。 你知道如何解决这个问题吗?有现成
我有一个模型类如下: public class CCP implements Serializable{ private static final long serialVersionUID
我正在使用 ReactiveCocoa 并且我有几个 SignalProducers let center = NSNotificationCenter.defaultCenter() let sig
我是一名优秀的程序员,十分优秀!