我想为这个“文章”端点使用来自Flask-Security-Too的令牌授权。但是,装饰器函数@auth_token_required("token")
需要应用程序的上下文。该应用程序在不同的文件中初始化。
我已经添加了app.app_context()
,但我不知道如何告诉装饰器使用它的上下文。
注意:我正在使用Flask-RestX来提供 SwaggerUI(和 OpenAPI 规范)。此外,我使用 Blueprints/Namespaces 来模块化 API - 这使得它有点复杂。
from flask import Blueprint
from flask_restx import Api, Resource, fields, Namespace
from flask_security import auth_token_required
from .db import db_session
from .models import Article
from flask import current_app as app
article_blueprint = Blueprint('api', __name__, url_prefix='/api/article')
flask_api = Api(article_blueprint, version='0.1', title='Article Manager',
description='Article management',)
article_manager = Namespace('article', description='Article Management Endpoint')
parser = article_manager.parser()
parser.add_argument('title', type=str, required=False,
help='Title of Article', location='json')
parser.add_argument('text', type=str, required=False,
help='Text of Article', location='json')
parser.add_argument('id', type=int, required=False,
help='ID of Article', location='json')
@article_manager.route('/<string:title>','/<int:id>')
class GetArticle(Resource):
# FIXME app.app_context() ?!
@auth_token_required("token")
def get(self, title=None, id=None):
# removed error handling for simplicity
if title is not None:
data = Article.query.filter(Article.title.ilike('%'+title+'%')).first()
if id is not None:
data = Article.query.filter_by(id=id).first()
db_session.commit()
return {'id': data.id, 'title': data.title, 'text': data.text}, 200
flask_api.add_namespace(article_manager)
烧瓶告诉我:
Exception has occurred: RuntimeError
Working outside of application context.