Google App Engine任务队列和用户服务 [英] Google app engine task queue and users service

查看:85
本文介绍了Google App Engine任务队列和用户服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用python和Google应用程序引擎.我想使用任务队列.作为任务队列处理程序的一部分,我检查当前用户是否是管理员(使用Users服务).此测试始终失败.有没有办法使该测试通过?

I am using python and Google app engine. I would like to use the task queue. As part of the task queue handler I check if the current user is an admin (using the Users service). This test always fails. Is there a way to cause this test to pass?

更新:为避免进一步的混乱,我试图查找触发任务的用户是否是管理员(这只是一个简单的示例).我了解该任务是从服务器运行的,所有用户的Cookie早已消失.所以我希望得到的答案是一种将会话转移到任务的方法

update: To avoid further confusion, I am trying to find if the user that triggered the task was an admin or not (this is just a simple example). I understand that the task is being run from the server and all the users cookies are long gone. So the answer I was hopping for was a way to transfer the session to the task

import logging
from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.api import taskqueue

class MyRequesHandler(webapp.RequestHandler):
    def get(self):
        taskqueue.add(url="/task/")

class MyTaskHandler(webapp.RequestHandler):
    def post(self):
        if users.is_current_user_admin():
            logging.debug("admin")
        else:
            logging.debug("not admin")


def main():
    logging.getLogger().setLevel(logging.DEBUG)
    application = webapp.WSGIApplication([
            ('/', MyRequesHandler),
            ('/task/', MyTaskHandler)
        ],
        debug=True)
    run_wsgi_app(application)

推荐答案

Users API反映了当前请求的登录用户的详细信息,显然,在任务队列任务的情况下,没有用户,因为它是由任务队列系统启动的.在入队任务之前,您需要执行此检查,然后将结果作为标志传递给任务.

The Users API reflects the details of the logged in user for the current request, and obviously in the case of a task queue task, there is no user, since it's initiated by the task queue system. You'll need to perform this check before you enqueue the task, and pass the result as a flag to the task, instead.

这篇关于Google App Engine任务队列和用户服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆