我有一个烧瓶应用程序,布局为几个MethodView
s。
在我的一个 MethodViews 上,我想在每个请求之前打开一个数据库连接,并在请求之后关闭它,与this非常相似。
我知道如何使用全局@app.before_request
和@app.teardown_request
函数,但它们会针对每个请求运行。我想要仅针对特定 MethodView 中的路由运行的有限版本。
如果您正在创建一个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)
您可以使用装饰器,它将某些视图标记为依赖于数据库。
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