简体   繁体   English

Tornado - Python 全局变量

[英]Tornado - Python global variable

I'm trying to use Tornado with SqlAlchemy, I need to pass the current user from RequestHandler (tornado) to models (SqlAlchemy) in the insert or update action.我正在尝试将 Tornado 与 SqlAlchemy 一起使用,我需要在插入或更新操作中将当前用户从 RequestHandler(龙卷风)传递给模型(SqlAlchemy)。 But I don't want to pass the value directly to the model, example:但我不想将值直接传递给模型,例如:

#### RequestHandler POST method...
user = Session.query(User).get(1)
user.name = "bla, bla, bla..."
user.updated_by = self.current_user # don't use...
session.commit()

I'm using a global variable, in a __ init__.py file, and set the current user value in the RequestHandler and after, get the value, in before update event with SqlAlchemy.我在 __init__.py 文件中使用全局变量,并在 RequestHandler 中设置当前用户值,然后在使用 SqlAlchemy 的更新事件之前获取值。

The idea is to know what user is the creator and updater.这个想法是要知道什么用户是创建者和更新者。 Why I don't want pass the current user directly to model like the before example ?, because this will be a tool for other developers, and I'm trying to make comfortable for them, also, they can forget about it and it is important.为什么我不想像之前的示例那样直接将当前用户传递给模型?,因为这将是其他开发人员的工具,我正在努力让他们感到舒适,而且,他们可以忘记它,它是重要的。

Is this a good idea, or maybe is there other better way ?这是一个好主意,还是有其他更好的方法?

Your solution will have issues if you're handling more than one request at a time.如果您一次处理多个请求,您的解决方案就会出现问题。 Tornado is an async web framework so another request might overwrite your global var and set the user to someone else. Tornado 是一个异步 Web 框架,因此另一个请求可能会覆盖您的全局变量并将用户设置为其他人。 It's good practice to store request depending data on self , tornado will make sure that data is altered by other simultaneous requests.将请求存储在self上的数据是一个很好的做法,tornado 将确保数据被其他同时请求更改。

A solution that might work for you is to add your tool in the basic handler or create a decorator.一个可能对您有用的解决方案是将您的工具添加到基本处理程序中或创建一个装饰器。 It's tricky to sugest more details, please include more info in your question if you would like to get better alternatives.建议更多细节很棘手,如果您想获得更好的选择,请在您的问题中包含更多信息。

The current user is available in every handler (and template).当前用户在每个处理程序(和模板)中都可用。 How you determine, authenticate and set the current user is up to you.如何确定、验证和设置当前用户取决于您。

Basically just subclass tornado.web.RequestHandler and override the get_current_user method in your new/own BaseHandler.基本上只是子类tornado.web.RequestHandler并覆盖新/自己的 BaseHandler 中的get_current_user方法。

Here the quote from the tornado docs:这是来自龙卷风文档的引述:

tornado User authentication龙卷风用户认证

User authentication The currently authenticated user is available in every request handler as self.current_user, and in every template as current_user.用户身份验证 当前经过身份验证的用户在每个请求处理程序中作为 self.current_user 可用,在每个模板中作为 current_user 可用。 By default, current_user is None.默认情况下,current_user 为无。

To implement user authentication in your application, you need to override the get_current_user() method in your request handlers to determine the current user based on, eg, the value of a cookie.要在您的应用程序中实现用户身份验证,您需要覆盖请求处理程序中的 get_current_user() 方法,以根据例如 cookie 的值来确定当前用户。 Here is an example that lets users log into the application simply by specifying a nickname, which is then saved in a cookie.这是一个示例,用户只需指定昵称即可登录应用程序,然后将昵称保存在 cookie 中。

You can see a fully working example in the official tornado blog demo您可以在官方 tornado 博客演示中看到一个完整的示例

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

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