本文介绍了无法在导入的cherrpy应用程序子类(站点树)中调用装饰器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将cherrypy用作网络服务器,并且我想在返回页面之前检查用户的登录状态。这适用于主Application类中的方法(在 site.py 中),但是当我在更深一层的类中的方法上调用相同的修饰函数时,会出现错误网页树(在单独的文件中)。

I am using cherrypy as a web server, and I want to check a user's logged-in status before returning the page. This works on methods in the main Application class (in site.py) but gives an error when I call the same decorated function on method in a class that is one layer deeper in the webpage tree (in a separate file).

validate_user()是用作装饰器的函数。它将用户作为 cherrypy.Tool 传递到该页面或将其发送到401受限页面,例如:

validate_user() is the function used as a decorator. It either passes a user to the page or sends them to a 401 restricted page, as a cherrypy.Tool, like this:

from user import validate_user
cherrypy.tools.validate_user = cherrypy.Tool('before_handler', validate_user)

我通过分配实例将站点的不同部分附加到主 site.py 文件的Application类中。子类相应地作为变量:

I attach different sections of the site to the main site.py file's Application class by assigning instances of the sub-classes as variables accordingly:

from user import UserAuthentication

class Root:
    user = UserAuthentication() # maps user/login, user/register, user/logout, etc
    admin = Admin()
    api = Api()

    @cherrypy.expose
    @cherrypy.tools.validate_user()
    def how_to(self, **kw):
        from other_stuff import how_to_page
        return how_to_page(kw) 

但是,当我尝试使用 validate_user()在广告内分钟或Api或分析部分。这些在单独的文件中。

This, however, does not work when I try to use the validate_user() inside the Admin or Api or Analysis sections. These are in separate files.

import cherrypy

class Analyze:
    @cherrypy.expose
    @cherrypy.tools.validate_user() #### THIS LINE GIVES ERROR ####
    def explore(self, *args, **kw): # @addkw(fetch=['uid'])
        import explore
        kw['uid'] = cherrypy.session.get('uid',-1)
        return explore.explorer(args, kw)

错误是cherrypy.tools没有validate_user函数或方法。但是我在site.py中分配的其他内容确实出现在此处的cherrypy中。为什么不能在属于我的整体站点地图的单独文件中使用此工具的原因是什么?

The error is that cherrypy.tools doesn't have a validate_user function or method. But other things I assign in site.py do appear in cherrypy here. What's the reason why I can't use this tool in a separate file that is part of my overall site map?

如果与此相关,则validate_user()函数简单地查看cherrypy.request.cookie,找到 session_token值,并将其与我们的数据库进行比较,如果ID匹配则将其传递给我们。

If this is relevant, the validate_user() function simply looks at the cherrypy.request.cookie, finds the 'session_token' value, and compares it to our database and passes it along if the ID matches.

对不起,我不知道不知道Analyze()和Api()以及User()页面是子类,嵌套类,扩展方法还是什么。所以我不能给它一个精确的标题。我需要以某种方式将父类传递给他们吗?

Sorry I don't know if the Analyze() and Api() and User() pages are subclasses, or nested classes, or extended methods, or what. So I can't give this a precise title. Do I need to pass in the parent class to them somehow?

推荐答案

这里的问题是Python处理除函数/之外的所有内容。导入过程中的方法主体。因此,在 site.py 中,当您导入用户(或从用户import< anything> ; ),这会导致所有 user 模块在 Python解释器定义之前被处理。 validate_user 工具(包括装饰器),该工具正在尝试通过值(而不是引用)访问该工具。

The issue here is that Python processes everything except the function/method bodies during import. So in site.py, when you import user (or from user import <anything>), that causes all of the user module to be processed before the Python interpreter has gotten to the definition of the validate_user tool, including the decorator, which is attempting to access that tool by value (rather than by a reference).

CherryPy还有另一种用config装饰功能的机制,该机制将启用那些处理程序上的工具。代替使用 @ cherrypy.tools.validate_user ,使用:

CherryPy has another mechanism for decorating functions with config that will enable tools on those handlers. Instead of @cherrypy.tools.validate_user, use:

@cherrypy.config(**{"tools.validate_user.on": True})

此装饰器起作用是因为无需从 cherrypy.tools 访问 validate_user 来在处理程序上自行安装,而是配置CherryPy来安装

This decorator works because instead of needing to access validate_user from cherrypy.tools to install itself on the handler, it instead configures CherryPy to install that tool on the handler later, when the handler is invoked.

如果该类上的所有方法都需要该工具,则可以在类本身上使用该配置装饰器。

If that tool is needed for all methods on that class, you can use that config decorator on the class itself.

您也可以在服务器配置中为给定端点启用该工具,如另一个问题所述。

You could alternatively, enable that tool for given endpoints in the server config, as mentioned in the other question.

这篇关于无法在导入的cherrpy应用程序子类(站点树)中调用装饰器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-13 13:16