将函数传递到类中并将其用作类方法的装饰器 [英] passing function into a class and using it as a decorator of class methods
问题描述
首先,我创建了一些我想在任何地方使用的用户管理功能,并将它们绑定到cherrypy,以为我可以将cherrypy导入其他地方并且它们在那里。
first I created some user management functions I want to use everywhere, and bound them to cherrypy, thinking I could import cherrypy elsewhere and they would be there. Other functions seem to import fine this way, when not used as decorators.
from user import validuser
cherrypy.validuser = validuser
del validuser
那没有用,所以接下来我尝试传递该函数从顶层页面进入我的cherrypy网站的一部分( / analyze
)的类:
that didn't work, so next I tried passing the function into the class that is a section of my cherrypy site (/analyze
) from the top level class of pages:
class Root:
analyze = Analyze(cherrypy.validuser) #maps to /analyze
在Analyze类中,我提到了它们。这适用于正常功能,但不适用于装饰器。为什么不?
And in the Analyze class, I referred to them. This works for normal functions but not for decorators. why not?
class Analyze:
def __init__(self, validuser):
self.validuser = validuser
@cherrypy.expose
@self.validuser(['uid'])
def index(self, **kw):
return analysis_panel.pick_data_sets(user_id=kw['uid'])
我被卡住了。如何传递函数并将其用作装饰器。我不想像这样包装我的函数:
I'm stuck. How can I pass functions in and use them as decorators. I'd rather not wrap my functions like this:
return self.validuser(analysis_panel.pick_data_sets(user_id=kw['uid']),['uid'])
谢谢。
添加/编辑:这是装饰器正在做的事情,因为作为一个单独的问题,我认为它没有将user_id正确地添加到kwarg中
ADDED/EDITED: here's what the decorator is doing, because as a separate issue, I don't think it is properly adding user_id into the kwargs
def validuser(old_function, fetch=['uid']):
def new_function(*args, **kw):
"... do stuff. decide is USER is logged in. return USER id or -1 ..."
if USER != -1 and 'uid' in fetch:
kw['uid'] = user_data['fc_uid']
return old_function(*args, **kw)
return new_function
只有传入的kwarg出现在new_function的kwarg中。我没有尝试添加的任何内容。 (我正在做的事情似乎在这里起作用如何在装饰器中将变量传递给装饰函数中的函数参数?)
only the kwargs that were passed in appear in the kwargs for the new_function. Anything I try to add isn't there. (what I'm doing appears to work here How can I pass a variable in a decorator to function's argument in a decorated function?)
推荐答案
CherryPy处理此类情况的正确方法是使用工具,并在需要身份验证的网站部分上启用该工具。考虑首先创建此用户身份验证工具:
The proper way in CherryPy to handle a situation like this is to have a tool and to enable that tool on the parts of your site that require authentication. Consider first creating this user-auth tool:
@cherrypy.tools.register('before_handler')
def validate_user():
if USER == -1:
return
cherrypy.request.uid = user_data['fc_uid']
Note that the 'register' decorator was added in CherryPy 5.5.0.
然后,无论您想验证用户的哪个位置,都可以用工具:
Then, wherever you wish to validate the user, either decorate the handler with the tool:
class Analyze:
@cherrypy.expose
@cherrypy.tools.validate_user()
def index(self):
return analysis_panel.pick_data_sets(user_id=cherrypy.request.uid)
或在您的cherrypy配置中启用该工具:
Or in your cherrypy config, enable that tool:
config = {
'/analyze': {
'tools.validate_user.on': True,
},
}
这篇关于将函数传递到类中并将其用作类方法的装饰器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!