Web2py - 使用 MongoDB 进行身份验证

Web2py - Auth with MongoDB

美好的一天,

我正在尝试将 MongoDB 与 web2py 一起使用,为此我从身份验证开始,但这出现了一些我不明白的错误。

在关系数据库中,web2py 创建身份验证 tables,集合中的 MongoDB 不会自动创建。

以下是尝试登录我时的代码和错误:

db.py

db = DAL("mongodb://localhost/primer", check_reserved=["mongodb_nonreserved",],  adapter_args={"safe":False})
from gluon.tools import Auth, Service, PluginManager

auth = Auth(db)
service = Service()
plugins = PluginManager()

auth.settings.remember_me_form = False
auth.settings.actions_disabled=['register','change_password','request_reset_password','retrieve_username','profile']
auth.define_tables(username=True)

from gluon.contrib.login_methods.ldap_auth import ldap_auth
auth.settings.login_methods = [ldap_auth(server='localhost', port='10389', base_dn='ou=people,o=empresa,dc=com,dc=br')]

身份验证由 LDAP 进行,并且在具有 AUTH_USER table.

的关系数据库中完美运行

但是使用MongoDB登录,出现如下错误:

    Traceback (most recent call last):
  File "C:\Users\Rafa\Desktop\web2py-10-06-2015p4\applications\contrato\controllers/appadmin.py", line 249, in select
nrows = db(query, ignore_common_filters=True).count()
  File "C:\Users\Rafa\Desktop\web2py-10-06-2015p4\gluon\packages\dal\pydal\objects.py", line 2016, in count
return db._adapter.count(self.query,distinct)
  File "C:\Users\Rafa\Desktop\web2py-10-06-2015p4\gluon\packages\dal\pydal\adapters\mongo.py", line 200, in count
count=True,snapshot=snapshot)['count'])
  File "C:\Users\Rafa\Desktop\web2py-10-06-2015p4\gluon\packages\dal\pydal\adapters\mongo.py", line 319, in select
sort=mongosort_list, snapshot=snapshot).count()}
  File "C:\Python27\lib\site-packages\pymongo\collection.py", line 929, in find
    return Cursor(self, *args, **kwargs)
TypeError: __init__() got an unexpected keyword argument 'snapshot'

数据库"primer"已创建,只有两个集合"posts"和"system.indexes"

有人可以帮我解决这个错误,以便能够将 MongoDB 与 web2py 一起使用吗?

谢谢!

找到了。

来自 pymongo's changelog 与 2.8 相比,pymongo 3.0 有很多突破性的变化

The following find/find_one options have been removed:

  • snapshot (use the new modifiers option instead)

所以卸载pymongo,试试最新的3.0之前:

pip install pymongo==2.8.1

这是我的尝试:

>>> from pydal import *
No handlers could be found for logger "web2py"
>>> db = DAL('mongodb://localhost/connect_test')
>>> db.define_table('some',Field('key'),Field('value'))
<Table some (id,key,value)>
>>> db.define_table('some2',Field('ref','reference some'),Field('value'))
<Table some2 (id,ref,value)>
>>> db(db.some).select()
<Rows (1)>
>>> db(db.some).select().first()
<Row {'value': 'pir', 'key': 'bla', 'id': 26563964102769618087622556519L}>
>>>

[编辑] 还有更多。这至少适用于 pydal 15.03。谷歌搜索一些代码,我在 mongo.py adapter 中发现了以下内容:

        from pymongo import version
        if 'fake_version' in driver_args:
            version = driver_args['fake_version']
        if int(version.split('.')[0]) < 3:
            raise Exception(
                "pydal requires pymongo version >= 3.0, found '%s'"
                % version)

这就像一个大皱眉的好土壤......

将 pydal 更新到 15.07 后,它确实出现了刹车:

RuntimeError: Failure to connect, tried 5 times:
Traceback (most recent call last):
  File "C:\Python27\lib\site-packages\pydal\base.py", line 437, in __init__
    self._adapter = ADAPTERS[self._dbname](**kwargs)
  File "C:\Python27\lib\site-packages\pydal\adapters\base.py", line 57, in __call__
    obj = super(AdapterMeta, cls).__call__(*args, **kwargs)
  File "C:\Python27\lib\site-packages\pydal\adapters\mongo.py", line 82, in __init__
    % version)
Exception: pydal requires pymongo version >= 3.0, found '2.8.1'

所以又回到升级pymongo :) 使用 3.0.3 的 pymongo 和 15.07 的 pydal,它再次发挥了魅力。