如何为python-eve应用程序动态更新mongo\u前缀

2024-10-03 15:35:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用不同的mongo\u db\u前缀定义了两个mongo数据库,如:

USER1_MONGO_DBNAME = 'user1db'
USER2_MONGO_DBNAME = 'user2db'

对于这两个用户,剩余的mongodb设置是相同的。你知道吗

如果我想动态更改用于eve应用程序的mongodb名称,如何更改?你知道吗


Tags: 用户数据库应用程序db定义mongomongodb动态
1条回答
网友
1楼 · 发布于 2024-10-03 15:35:34

请看documentation中的这个片段:

from eve.auth import BasicAuth

class MyBasicAuth(BasicAuth):
    def check_auth(self, username, password, allowed_roles, resource, method):
        if username == 'user1':
            self.set_mongo_prefix('MONGO1')
        elif username == 'user2':
            self.set_mongo_prefix('MONGO2')
        else:
            # serve all other users from the default db.
            self.set_mongo_prefix(None)
        return username is not None and password == 'secret'

app = Eve(auth=MyBasicAuth)
app.run()

这个例子是根据经过身份验证的用户切换DB。您也可以在回调函数中利用set_mongo_prefix方法,请参见Event Hooks。你知道吗

相关问题 更多 >