我正在構建groupon-y服務來學習應用引擎。我想跟蹤登錄該網站的帳戶列表(使用Google身份驗證)。什麼是最好的方法來做到這一點?由於我使用谷歌身份驗證,因此沒有單一時間點他們'註冊';他們可以在任何網址登錄。在appengine中登錄track
這裏是我試過:
- 在那做了get_or_insert
- 我想我可以繼承@login_required每一個請求處理程序的開始force_login功能,但我仍然必須記住裝飾我所有的請求處理程序
是不是有辦法在登錄後立即掛鉤並註冊一些邏輯?
我正在構建groupon-y服務來學習應用引擎。我想跟蹤登錄該網站的帳戶列表(使用Google身份驗證)。什麼是最好的方法來做到這一點?由於我使用谷歌身份驗證,因此沒有單一時間點他們'註冊';他們可以在任何網址登錄。在appengine中登錄track
這裏是我試過:
是不是有辦法在登錄後立即掛鉤並註冊一些邏輯?
沒有登錄,但如你所說後立即執行代碼,記得要做到在每一個處理程序的開始,或者在每個處理程序中一個裝飾的方式。裝飾者聽起來像一個更好的選擇。
你可以使用裝飾器 - 你必須記得把它放在任何需要登錄的處理器的前面 - 或者你可以在你的所有處理器擴展的基本RequestHandler中完成(我的個人偏好),或者你可以編寫一些WSGI中間件。隨你便。
你會如何寫一個裝飾?
有得 [RequiresAuthentication]
在.NET MVC相似?
class Importer(webapp.RequestHandler):
@RequiresAuthentication
def get(self):
pass
def RequiresAuthentication(fn):
def authorize(self):
if not users.get_current_user():
self.redirect(users.create_login_url(self.request.uri))
else:
return fn(self)
return authorize
我剛纔在測試應用程序中測試過,它似乎工作。
看着@login_required之後,它不適合崗位工作:
def login_required(handler_method):
"""A decorator to require that a user be logged in to access a handler.
To use it, decorate your get() method like this:
@login_required
def get(self):
user = users.get_current_user(self)
self.response.out.write('Hello, ' + user.nickname())
We will redirect to a login page if the user is not logged in. We always
redirect to the request URI, and Google Accounts only redirects back as a GET
request, so this should not be used for POSTs.
"""
def check_login(self, *args):
if self.request.method != 'GET':
raise webapp.Error('The check_login decorator can only be used for GET '
'requests')
user = users.get_current_user()
if not user:
self.redirect(users.create_login_url(self.request.uri))
return
else:
handler_method(self, *args)
return check_login
我沒有讀您的文章。我剛剛實施了@login_required – DerpDerp 2011-02-12 05:37:46