Web2py - 使用MongoDB进行身份验证

时间:2015-06-19 13:04:28

标签: mongodb web2py

美好的一天,

我试图将MongoDB与web2py一起使用,为此我开始使用身份验证,但这出现了一些我不理解的错误。

在关系数据库中,web2py会创建身份验证表,集合中的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表的关系数据库中完美运行。

但是,使用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'

数据库"引物"已创建,只有两个集合"帖子"和" system.indexes"

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

谢谢!

1 个答案:

答案 0 :(得分:1)

找到它。

来自pymongo's changelog pymongo 3.0与2.8相比有很多重大变化。

  

已删除以下find / find_one选项:

     
      
  • snapshot(改为使用new modifiers选项)
  •   

所以卸载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 :) 随着pymongo在3.0.3和pydal在15.07,它再次像魅力。