0
我使用Flask-limiter擴展來限制特定路由的頁面請求數(除非他們以管理員身份登錄)。我正在使用Flask-Stormpath擴展來管理我的用戶身份驗證。在Flask Limiter函數中訪問用戶對象返回上下文錯誤
出於某種原因,我現有的函數返回此錯誤:
Traceback (most recent call last):
File "index.py", line 772, in <module>
@limiter.limit("20/hour", exempt_when=admin_conditions())
File "index.py", line 114, in admin_conditions
if current_app.user.is_authenticated():
File "c:\Python27\lib\site-packages\werkzeug\local.py", line 338, in __getattr
__
return getattr(self._get_current_object(), name)
File "c:\Python27\lib\site-packages\werkzeug\local.py", line 297, in _get_curr
ent_object
return self.__local()
File "c:\Python27\lib\site-packages\flask\globals.py", line 34, in _find_app
raise RuntimeError('working outside of application context')
RuntimeError: working outside of application context
這裏是我的代碼:
# Rate Limiting function
def admin_conditions():
if user.is_authenticated():
group_list = []
for group in user.groups:
group_list.append(group.name)
if not group_list:
return(False)
else:
if group_list[0] == "admins":
return(True)
else:
return(False)
else:
return(False)
# route
@limiter.limit("20/hour", exempt_when=admin_conditions())
我讀了admin_conditions函數被調用的request context通過燒瓶限制器,但我不明白如何從那裏訪問用戶對象。任何想法發生了什麼?