在 Google 应用引擎上的模板中访问用户的 OpenID 信息 (tipfy)
我正在使用 Google 应用引擎中内置的 OpenID 身份验证,并尝试在模板中自动提供当前登录用户的信息。
手动执行可能类似于:
from google.appengine.api import users
from tipfy import RequestHandler
from tipfy.ext.jinja2 import render_response
def WhateverHandler(RequestHandler):
def get(self):
user = users.get_current_user()
return render_response('template_name.html', user=user)
# great, now I can use {{ user.nickname }} or whatever in the template
我不想在每个请求处理程序中编写此代码,因此希望能够添加某种中间件来处理它(我主要是在这里猜测):
from google.appengine.api import users
class GoogleUsersMiddleware(object)
def post_dispatch(self, handler, response):
user = users.get_current_user()
# now somehow add user to the response?
return response
有什么想法吗?我尝试了解tipfy 的SessionMiddleware 是如何工作的,但不太明白。
干杯。
I'm using the OpenID authentication that's built into Google app engine and trying to make the currently signed in user's information automatically available in templates.
Doing it manually might be something like:
from google.appengine.api import users
from tipfy import RequestHandler
from tipfy.ext.jinja2 import render_response
def WhateverHandler(RequestHandler):
def get(self):
user = users.get_current_user()
return render_response('template_name.html', user=user)
# great, now I can use {{ user.nickname }} or whatever in the template
I don't want to write this code in every request handler so expecting to be able to add some kind of middleware to take care of it (and I'm mostly guessing here):
from google.appengine.api import users
class GoogleUsersMiddleware(object)
def post_dispatch(self, handler, response):
user = users.get_current_user()
# now somehow add user to the response?
return response
Any ideas? I've tried looking at how tipfy's SessionMiddleware works but don't quite get it.
Cheers.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以创建 RequestHandler 的子类,在 __init__ 上自动检索当前用户。您还可以向该子类添加方法来自定义模板呈现。我认为中间件不是解决这个问题的正确方法。
You can create a subclass of RequestHandler that retrieves the current user automatically on
__init__
. Also you can add methods to that subclass to customize template rendering. I don't think Middleware is the right solution for this problem.