我有一个烧瓶应用程序,它被列为几个MethodView
。
在我的一个MethodViews上,我想在每个请求之前打开一个数据库连接,并在请求之后关闭它,与this非常相似。
我知道如何使用全局@app.before_request
和@app.teardown_request
函数,但这些函数将针对每个请求运行。我想要只为特定MethodView中的路由运行的有限版本。
答案 0 :(得分:2)
如果要创建MethodView
子类,最简单的方法是简单地添加一个在任何适当方法调用时调用的函数:
class AutoConnectingView(MethodView):
def setup(self):
# Connect to the database here and store the connection on self.
self.db = connect_to_db()
def teardown(self):
self.db.close()
def dispatch_request(self, *args, **kwargs):
self.setup()
response = super(AutoConnectingView, self).dispatch_request(*args, **kwargs)
self.teardown()
return response
class ARoomWithAView(AutoConnectingView):
def get(self):
rooms = self.db.execute("SELECT * FROM RoomsWithViews")
return render_template("rooms.html", rooms=rooms)
答案 1 :(得分:2)
您可以使用装饰器,将某些视图标记为依赖于数据库。
NoSQLStore = {}
class NoSQL(object):
""" fake """
def query(self, key):
return '%s\n' % NoSQLStore.get(key, 'no such key')
def put(self, key, value):
NoSQLStore[key] = value
def with_database(fn):
""" Decorator for functions, that need database access.
NoSQL object will be stored under ``g.db``.
"""
def connect_and_close(*args, **kwargs):
g.db = NoSQL()
__result = fn(*args, **kwargs)
# a real database should be somehow ``closed()`` here
return __result
return connect_and_close
class StoreAPI(MethodView):
@with_database
def get(self):
return g.db.query(request.args.get('key'))
@with_database
def post(self):
key, value = str(random.randint(0, 1000)), str(random.randint(0, 1000))
g.db.put(key, value)
return 'set %s => %s\n' % (key, value)
可在此处找到可运行的独立示例:https://gist.github.com/4424587