在 Google 应用引擎上的模板中访问用户的 OpenID 信息 (tipfy)

发布于 2024-10-28 08:25:20 字数 873 浏览 4 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

早茶月光 2024-11-04 08:25:20

您可以创建 RequestHandler 的子类,在 __init__ 上自动检索当前用户。您还可以向该子类添加方法来自定义模板呈现。我认为中间件不是解决这个问题的正确方法。

from google.appengine.api import users
from tipfy import RequestHandler
from tipfy.ext.jinja2 import render_response

class UserPageHandler(RequestHandler):
    def __init__(self, app, request):
        super(UserPageHandler, self).__init__(app, request)

        self.user = users.get_current_user()

    def user_response(self, template_name):
        return render_response(template_name, user=self.user)

def WhateverHandler(UserPageHandler):
    def get(self):
        return self.user_response('template_name.html')

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.

from google.appengine.api import users
from tipfy import RequestHandler
from tipfy.ext.jinja2 import render_response

class UserPageHandler(RequestHandler):
    def __init__(self, app, request):
        super(UserPageHandler, self).__init__(app, request)

        self.user = users.get_current_user()

    def user_response(self, template_name):
        return render_response(template_name, user=self.user)

def WhateverHandler(UserPageHandler):
    def get(self):
        return self.user_response('template_name.html')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文