diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 2fde7c87..0eabcd2f 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -6,6 +6,7 @@ Changelog Version 0.5.2 (TBD) ----------------- * Add support for Replica Sets (Thanks @r4fek) +* PyMongo 3 support Version 0.5.1 (Nov 2013) diff --git a/django_mongodb_engine/base.py b/django_mongodb_engine/base.py index 033008e4..4169deca 100644 --- a/django_mongodb_engine/base.py +++ b/django_mongodb_engine/base.py @@ -234,7 +234,9 @@ def pop(name, default=None): replicaset = options.get('replicaset') if not read_preference: - read_preference = options.get('slave_okay', options.get('slaveok')) + read_preference = options.pop('slave_okay', + options.pop('slaveok', None)) + if read_preference: options['read_preference'] = ReadPreference.SECONDARY warnings.warn("slave_okay has been deprecated. " @@ -248,12 +250,8 @@ def pop(name, default=None): conn_options = dict( host=host, port=int(port), - max_pool_size=None, document_class=dict, tz_aware=False, - _connect=True, - auto_start_request=True, - safe=False ) conn_options.update(options) diff --git a/django_mongodb_engine/compiler.py b/django_mongodb_engine/compiler.py index a26c0af1..aac1c596 100644 --- a/django_mongodb_engine/compiler.py +++ b/django_mongodb_engine/compiler.py @@ -136,7 +136,7 @@ def get_cursor(self): return [] fields = get_selected_fields(self.query) - cursor = self.collection.find(self.mongo_query, fields=fields) + cursor = self.collection.find(self.mongo_query, fields) if self.ordering: cursor.sort(self.ordering) if self.query.low_mark > 0: diff --git a/django_mongodb_engine/utils.py b/django_mongodb_engine/utils.py index 6974936b..77aa120e 100644 --- a/django_mongodb_engine/utils.py +++ b/django_mongodb_engine/utils.py @@ -70,8 +70,6 @@ def log(self, op, duration, args, kwargs=None): logger.debug(msg, extra={'duration': duration}) def find(self, *args, **kwargs): - if not 'slave_okay' in kwargs and self.collection.slave_okay: - kwargs['slave_okay'] = True return DebugCursor(self, self.collection, *args, **kwargs) def logging_wrapper(method): diff --git a/docs/source/reference/settings.rst b/docs/source/reference/settings.rst index e23ca372..97556f2e 100644 --- a/docs/source/reference/settings.rst +++ b/docs/source/reference/settings.rst @@ -3,9 +3,9 @@ Settings .. TODO fix highlighting -Connection Settings -------------------- -Additional flags may be passed to :class:`pymongo.Connection` using the +Client Settings +--------------- +Additional flags may be passed to :class:`pymongo.MongoClient` using the ``OPTIONS`` dictionary:: DATABASES = { @@ -14,9 +14,7 @@ Additional flags may be passed to :class:`pymongo.Connection` using the 'NAME' : 'my_database', ... 'OPTIONS' : { - 'slave_okay' : True, - 'tz_aware' : True, - 'network_timeout' : 42, + 'socketTimeoutMS' : 500, ... } } @@ -24,17 +22,18 @@ Additional flags may be passed to :class:`pymongo.Connection` using the All of these settings directly mirror PyMongo settings. In fact, all Django MongoDB Engine does is lower-casing the names before passing the flags to -:class:`~pymongo.Connection`. For a list of possible options head over to the -`PyMongo documentation on connection options`_. +:class:`~pymongo.MongoClient`. For a list of possible options head over to the +`PyMongo documentation on client options`_. .. _operations-setting: -Safe Operations (``getLastError``) ----------------------------------- +Acknowledged Operations +----------------------- Use the ``OPERATIONS`` dict to specify extra flags passed to :meth:`Collection.save `, :meth:`~pymongo.collection.Collection.update` or -:meth:`~pymongo.collection.Collection.remove` (and thus to ``getLastError``): +:meth:`~pymongo.collection.Collection.remove` (and thus included in the +write concern): .. code-block:: python @@ -43,21 +42,15 @@ Use the ``OPERATIONS`` dict to specify extra flags passed to ... } -Since any options to ``getLastError`` imply ``safe=True``, -this configuration passes ``safe=True, w=3`` as keyword arguments to each of -:meth:`~pymongo.collection.Collection.save`, -:meth:`~pymongo.collection.Collection.update` and -:meth:`~pymongo.collection.Collection.remove`. - Get a more fine-grained setup by introducing another layer to this dict: .. code-block:: python 'OPTIONS' : { 'OPERATIONS' : { - 'save' : {'safe' : True}, + 'save' : {'w': 3}, 'update' : {}, - 'delete' : {'fsync' : True} + 'delete' : {'j' : True} }, ... } @@ -69,10 +62,10 @@ Get a more fine-grained setup by introducing another layer to this dict: "`insert vs. update`" into `save`. -A full list of ``getLastError`` flags may be found in the -`MongoDB documentation `_. +A full list of write concern flags may be found in the +`MongoDB documentation `_. .. _Similar to Django's built-in backends: http://docs.djangoproject.com/en/dev/ref/settings/#std:setting-OPTIONS -.. _PyMongo documentation on connection options: - http://api.mongodb.org/python/current/api/pymongo/connection.html +.. _PyMongo documentation on client options: + http://api.mongodb.org/python/current/api/pymongo/mongo_client.html diff --git a/tests/mongodb/models.py b/tests/mongodb/models.py index b5bd39c3..f3af8bd0 100644 --- a/tests/mongodb/models.py +++ b/tests/mongodb/models.py @@ -92,7 +92,7 @@ class MongoMeta: {'fields': [('custom_column', -1), 'f3']}, [('geo', '2d')], {'fields': [('geo_custom_column', '2d'), 'f2'], - 'min': 42, 'max': 21}, + 'min': 21, 'max': 42}, {'fields': [('dict1.foo', 1)]}, {'fields': [('dict_custom_column.foo', 1)]}, {'fields': [('embedded.a', 1)]}, diff --git a/tests/mongodb/tests.py b/tests/mongodb/tests.py index 715ec5bc..a8e4b19f 100644 --- a/tests/mongodb/tests.py +++ b/tests/mongodb/tests.py @@ -8,7 +8,10 @@ from django.db.models import Q from gridfs import GridOut -from pymongo import ASCENDING, DESCENDING +from pymongo import (ASCENDING, + DESCENDING, + ReadPreference, + version_tuple as pymongo_version) from django_mongodb_engine.base import DatabaseWrapper @@ -193,7 +196,7 @@ def __enter__(self): return self.new_wrapper def __exit__(self, *exc_info): - self.new_wrapper.connection.disconnect() + self.new_wrapper.connection.close() connections._connections.default = self._old_connection def test_pymongo_connection_args(self): @@ -203,20 +206,23 @@ class foodict(dict): with self.custom_database_wrapper({ 'OPTIONS': { - 'SLAVE_OKAY': True, + 'READ_PREFERENCE': ReadPreference.SECONDARY, 'TZ_AWARE': True, 'DOCUMENT_CLASS': foodict, - }}) as connection: - for name, value in connection.settings_dict[ - 'OPTIONS'].iteritems(): - name = '_Connection__%s' % name.lower() - if name not in connection.connection.__dict__: - # slave_okay was moved into BaseObject in PyMongo 2.0. - name = name.replace('Connection', 'BaseObject') - if name not in connection.connection.__dict__: - # document_class was moved into MongoClient in PyMongo 2.4. - name = name.replace('BaseObject', 'MongoClient') - self.assertEqual(connection.connection.__dict__[name], value) + }}) as db: + connection = db.connection + if pymongo_version[0] >= 3: + tz_aware = connection.codec_options.tz_aware + document_class = connection.codec_options.document_class + else: + tz_aware = connection.tz_aware + document_class = connection.document_class + + self.assertEqual(ReadPreference.SECONDARY, + connection.read_preference) + + self.assertEqual(True, tz_aware) + self.assertEqual(foodict, document_class) def test_operation_flags(self): def test_setup(flags, **method_kwargs): @@ -248,20 +254,18 @@ def test_setup(flags, **method_kwargs): self.assertEqual(method_kwargs[name], Collection._method_kwargs[name]) - if Collection._method_kwargs['update'].get('safe'): - self.assertEqual(*update_count) + self.assertEqual(*update_count) test_setup({}, save={}, update={'multi': True}, remove={}) + test_setup({}, + save={}, + update={'multi': True}, + remove={}) test_setup({ - 'safe': True}, - save={'safe': True}, - update={'safe': True, 'multi': True}, - remove={'safe': True}) - test_setup({ - 'delete': {'safe': True}, 'update': {}}, + 'delete': {}, 'update': {}}, save={}, update={'multi': True}, - remove={'safe': True}) + remove={}) test_setup({ 'insert': {'fsync': True}, 'delete': {'fsync': True}}, save={}, diff --git a/tests/settings/settings_base.py b/tests/settings/settings_base.py index f6c1b4ef..3482d484 100644 --- a/tests/settings/settings_base.py +++ b/tests/settings/settings_base.py @@ -2,7 +2,7 @@ 'default': { 'ENGINE': 'django_mongodb_engine', 'NAME': 'test', - 'OPTIONS': {'OPERATIONS': {'safe': True}}, + 'OPTIONS': {'OPERATIONS': {}}, }, 'other': { 'ENGINE': 'django_mongodb_engine',