Web2py - Auth with MongoDB

933 Views Asked by At

Good day,

I'm trying to use MongoDB with web2py, and for that I started with authentication, but this appeared some errors that I do not understand.

In a relational database, the web2py creates the authentication tables, MongoDB in the collections are not created automatically.

Below is the code and the error when trying to log me:

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')]

The authentication is by LDAP, and works perfectly in a relational database, which has the AUTH_USER table.

However, the loging using MongoDB, this appearing the following error:

    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'

The database "primer" is created and only has two collections "posts" and "system.indexes"

Could someone help me with this error to be able to use MongoDB with the web2py?

Thank You!

1

There are 1 best solutions below

0
On BEST ANSWER

Found it.

From pymongo's changelog There are a lot of breaking changes in pymongo 3.0 compared to 2.8

The following find/find_one options have been removed:

  • snapshot (use the new modifiers option instead)

So uninstall pymongo and try the latest before 3.0:

pip install pymongo==2.8.1

Here's my attempt:

>>> 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}>
>>>

[edit] There's more to it. This worked at least with pydal 15.03. Googling some code i found the following in the 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)

Which was like good soil for a big frown...

After updating pydal to 15.07 it apears to brake indeed:

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'

So it's back to upgrading pymongo :) With pymongo at 3.0.3 and pydal at 15.07 it works like a charm again.