Remove more code related to supporting pymongo2
This commit is contained in:
		| @@ -2,8 +2,6 @@ from pymongo import MongoClient, ReadPreference, uri_parser | ||||
| from pymongo.database import _check_name | ||||
| import six | ||||
|  | ||||
| from mongoengine.pymongo_support import IS_PYMONGO_3 | ||||
|  | ||||
| __all__ = ['MongoEngineConnectionError', 'connect', 'disconnect', 'disconnect_all', | ||||
|            'register_connection', 'DEFAULT_CONNECTION_NAME', 'DEFAULT_DATABASE_NAME', | ||||
|            'get_db', 'get_connection'] | ||||
| @@ -14,11 +12,11 @@ DEFAULT_DATABASE_NAME = 'test' | ||||
| DEFAULT_HOST = 'localhost' | ||||
| DEFAULT_PORT = 27017 | ||||
|  | ||||
| if IS_PYMONGO_3: | ||||
|     READ_PREFERENCE = ReadPreference.PRIMARY | ||||
| else: | ||||
|     from pymongo import MongoReplicaSetClient | ||||
|     READ_PREFERENCE = False | ||||
| _connection_settings = {} | ||||
| _connections = {} | ||||
| _dbs = {} | ||||
|  | ||||
| READ_PREFERENCE = ReadPreference.PRIMARY | ||||
|  | ||||
|  | ||||
| class MongoEngineConnectionError(Exception): | ||||
| @@ -28,12 +26,7 @@ class MongoEngineConnectionError(Exception): | ||||
|     pass | ||||
|  | ||||
|  | ||||
| _connection_settings = {} | ||||
| _connections = {} | ||||
| _dbs = {} | ||||
|  | ||||
|  | ||||
| def check_db_name(name): | ||||
| def _check_db_name(name): | ||||
|     """Check if a database name is valid. | ||||
|     This functionality is copied from pymongo Database class constructor. | ||||
|     """ | ||||
| @@ -57,7 +50,6 @@ def _get_connection_settings( | ||||
|     : param host: the host name of the: program: `mongod` instance to connect to | ||||
|     : param port: the port that the: program: `mongod` instance is running on | ||||
|     : param read_preference: The read preference for the collection | ||||
|        ** Added pymongo 2.1 | ||||
|     : param username: username to authenticate with | ||||
|     : param password: password to authenticate with | ||||
|     : param authentication_source: database to authenticate against | ||||
| @@ -83,7 +75,7 @@ def _get_connection_settings( | ||||
|         'authentication_mechanism': authentication_mechanism | ||||
|     } | ||||
|  | ||||
|     check_db_name(conn_settings['name']) | ||||
|     _check_db_name(conn_settings['name']) | ||||
|     conn_host = conn_settings['host'] | ||||
|  | ||||
|     # Host can be a list or a string, so if string, force to a list. | ||||
| @@ -119,7 +111,7 @@ def _get_connection_settings( | ||||
|                 conn_settings['authentication_source'] = uri_options['authsource'] | ||||
|             if 'authmechanism' in uri_options: | ||||
|                 conn_settings['authentication_mechanism'] = uri_options['authmechanism'] | ||||
|             if IS_PYMONGO_3 and 'readpreference' in uri_options: | ||||
|             if 'readpreference' in uri_options: | ||||
|                 read_preferences = ( | ||||
|                     ReadPreference.NEAREST, | ||||
|                     ReadPreference.PRIMARY, | ||||
| @@ -158,7 +150,6 @@ def register_connection(alias, db=None, name=None, host=None, port=None, | ||||
|     : param host: the host name of the: program: `mongod` instance to connect to | ||||
|     : param port: the port that the: program: `mongod` instance is running on | ||||
|     : param read_preference: The read preference for the collection | ||||
|        ** Added pymongo 2.1 | ||||
|     : param username: username to authenticate with | ||||
|     : param password: password to authenticate with | ||||
|     : param authentication_source: database to authenticate against | ||||
| @@ -259,22 +250,6 @@ def get_connection(alias=DEFAULT_CONNECTION_NAME, reconnect=False): | ||||
|     else: | ||||
|         connection_class = MongoClient | ||||
|  | ||||
|         # For replica set connections with PyMongo 2.x, use | ||||
|         # MongoReplicaSetClient. | ||||
|         # TODO remove this once we stop supporting PyMongo 2.x. | ||||
|         if 'replicaSet' in conn_settings and not IS_PYMONGO_3: | ||||
|             connection_class = MongoReplicaSetClient | ||||
|             conn_settings['hosts_or_uri'] = conn_settings.pop('host', None) | ||||
|  | ||||
|             # hosts_or_uri has to be a string, so if 'host' was provided | ||||
|             # as a list, join its parts and separate them by ',' | ||||
|             if isinstance(conn_settings['hosts_or_uri'], list): | ||||
|                 conn_settings['hosts_or_uri'] = ','.join( | ||||
|                     conn_settings['hosts_or_uri']) | ||||
|  | ||||
|             # Discard port since it can't be used on MongoReplicaSetClient | ||||
|             conn_settings.pop('port', None) | ||||
|  | ||||
|     # Iterate over all of the connection settings and if a connection with | ||||
|     # the same parameters is already established, use it instead of creating | ||||
|     # a new one. | ||||
|   | ||||
| @@ -18,7 +18,7 @@ from mongoengine.context_managers import (set_write_concern, | ||||
|                                           switch_db) | ||||
| from mongoengine.errors import (InvalidDocumentError, InvalidQueryError, | ||||
|                                 SaveConditionError) | ||||
| from mongoengine.pymongo_support import IS_PYMONGO_3, list_collection_names | ||||
| from mongoengine.pymongo_support import list_collection_names | ||||
| from mongoengine.queryset import (NotUniqueError, OperationError, | ||||
|                                   QuerySet, transform) | ||||
|  | ||||
| @@ -822,10 +822,7 @@ class Document(six.with_metaclass(TopLevelDocumentMetaclass, BaseDocument)): | ||||
|         index_spec['background'] = background | ||||
|         index_spec.update(kwargs) | ||||
|  | ||||
|         if IS_PYMONGO_3: | ||||
|             return cls._get_collection().create_index(fields, **index_spec) | ||||
|         else: | ||||
|             return cls._get_collection().ensure_index(fields, **index_spec) | ||||
|         return cls._get_collection().create_index(fields, **index_spec) | ||||
|  | ||||
|     @classmethod | ||||
|     def ensure_index(cls, key_or_list, drop_dups=False, background=False, | ||||
| @@ -858,7 +855,7 @@ class Document(six.with_metaclass(TopLevelDocumentMetaclass, BaseDocument)): | ||||
|         drop_dups = cls._meta.get('index_drop_dups', False) | ||||
|         index_opts = cls._meta.get('index_opts') or {} | ||||
|         index_cls = cls._meta.get('index_cls', True) | ||||
|         if IS_PYMONGO_3 and drop_dups: | ||||
|         if drop_dups: | ||||
|             msg = 'drop_dups is deprecated and is removed when using PyMongo 3+.' | ||||
|             warnings.warn(msg, DeprecationWarning) | ||||
|  | ||||
| @@ -889,11 +886,7 @@ class Document(six.with_metaclass(TopLevelDocumentMetaclass, BaseDocument)): | ||||
|                 if 'cls' in opts: | ||||
|                     del opts['cls'] | ||||
|  | ||||
|                 if IS_PYMONGO_3: | ||||
|                     collection.create_index(fields, background=background, **opts) | ||||
|                 else: | ||||
|                     collection.ensure_index(fields, background=background, | ||||
|                                             drop_dups=drop_dups, **opts) | ||||
|                 collection.create_index(fields, background=background, **opts) | ||||
|  | ||||
|         # If _cls is being used (for polymorphism), it needs an index, | ||||
|         # only if another index doesn't begin with _cls | ||||
| @@ -904,12 +897,8 @@ class Document(six.with_metaclass(TopLevelDocumentMetaclass, BaseDocument)): | ||||
|             if 'cls' in index_opts: | ||||
|                 del index_opts['cls'] | ||||
|  | ||||
|             if IS_PYMONGO_3: | ||||
|                 collection.create_index('_cls', background=background, | ||||
|                                         **index_opts) | ||||
|             else: | ||||
|                 collection.ensure_index('_cls', background=background, | ||||
|                                         **index_opts) | ||||
|             collection.create_index('_cls', background=background, | ||||
|                                     **index_opts) | ||||
|  | ||||
|     @classmethod | ||||
|     def list_indexes(cls): | ||||
|   | ||||
| @@ -7,7 +7,6 @@ _PYMONGO_37 = (3, 7) | ||||
|  | ||||
| PYMONGO_VERSION = tuple(pymongo.version_tuple[:2]) | ||||
|  | ||||
| IS_PYMONGO_3 = PYMONGO_VERSION[0] >= 3 | ||||
| IS_PYMONGO_GTE_37 = PYMONGO_VERSION >= _PYMONGO_37 | ||||
|  | ||||
|  | ||||
|   | ||||
| @@ -10,6 +10,7 @@ from bson import SON, json_util | ||||
| from bson.code import Code | ||||
| import pymongo | ||||
| import pymongo.errors | ||||
| from pymongo.collection import ReturnDocument | ||||
| from pymongo.common import validate_read_preference | ||||
| import six | ||||
| from six import iteritems | ||||
| @@ -21,14 +22,10 @@ from mongoengine.connection import get_db | ||||
| from mongoengine.context_managers import set_write_concern, switch_db | ||||
| from mongoengine.errors import (InvalidQueryError, LookUpError, | ||||
|                                 NotUniqueError, OperationError) | ||||
| from mongoengine.pymongo_support import IS_PYMONGO_3 | ||||
| from mongoengine.queryset import transform | ||||
| from mongoengine.queryset.field_list import QueryFieldList | ||||
| from mongoengine.queryset.visitor import Q, QNode | ||||
|  | ||||
| if IS_PYMONGO_3: | ||||
|     from pymongo.collection import ReturnDocument | ||||
|  | ||||
|  | ||||
| __all__ = ('BaseQuerySet', 'DO_NOTHING', 'NULLIFY', 'CASCADE', 'DENY', 'PULL') | ||||
|  | ||||
| @@ -631,26 +628,20 @@ class BaseQuerySet(object): | ||||
|         sort = queryset._ordering | ||||
|  | ||||
|         try: | ||||
|             if IS_PYMONGO_3: | ||||
|                 if full_response: | ||||
|                     msg = 'With PyMongo 3+, it is not possible anymore to get the full response.' | ||||
|                     warnings.warn(msg, DeprecationWarning) | ||||
|                 if remove: | ||||
|                     result = queryset._collection.find_one_and_delete( | ||||
|                         query, sort=sort, **self._cursor_args) | ||||
|                 else: | ||||
|                     if new: | ||||
|                         return_doc = ReturnDocument.AFTER | ||||
|                     else: | ||||
|                         return_doc = ReturnDocument.BEFORE | ||||
|                     result = queryset._collection.find_one_and_update( | ||||
|                         query, update, upsert=upsert, sort=sort, return_document=return_doc, | ||||
|                         **self._cursor_args) | ||||
|  | ||||
|             if full_response: | ||||
|                 msg = 'With PyMongo 3+, it is not possible anymore to get the full response.' | ||||
|                 warnings.warn(msg, DeprecationWarning) | ||||
|             if remove: | ||||
|                 result = queryset._collection.find_one_and_delete( | ||||
|                     query, sort=sort, **self._cursor_args) | ||||
|             else: | ||||
|                 result = queryset._collection.find_and_modify( | ||||
|                     query, update, upsert=upsert, sort=sort, remove=remove, new=new, | ||||
|                     full_response=full_response, **self._cursor_args) | ||||
|                 if new: | ||||
|                     return_doc = ReturnDocument.AFTER | ||||
|                 else: | ||||
|                     return_doc = ReturnDocument.BEFORE | ||||
|                 result = queryset._collection.find_one_and_update( | ||||
|                     query, update, upsert=upsert, sort=sort, return_document=return_doc, | ||||
|                     **self._cursor_args) | ||||
|         except pymongo.errors.DuplicateKeyError as err: | ||||
|             raise NotUniqueError(u'Update failed (%s)' % err) | ||||
|         except pymongo.errors.OperationFailure as err: | ||||
| @@ -1082,9 +1073,8 @@ class BaseQuerySet(object): | ||||
|         ..versionchanged:: 0.5 - made chainable | ||||
|         .. deprecated:: Ignored with PyMongo 3+ | ||||
|         """ | ||||
|         if IS_PYMONGO_3: | ||||
|             msg = 'snapshot is deprecated as it has no impact when using PyMongo 3+.' | ||||
|             warnings.warn(msg, DeprecationWarning) | ||||
|         msg = 'snapshot is deprecated as it has no impact when using PyMongo 3+.' | ||||
|         warnings.warn(msg, DeprecationWarning) | ||||
|         queryset = self.clone() | ||||
|         queryset._snapshot = enabled | ||||
|         return queryset | ||||
| @@ -1108,9 +1098,8 @@ class BaseQuerySet(object): | ||||
|  | ||||
|         .. deprecated:: Ignored with PyMongo 3+ | ||||
|         """ | ||||
|         if IS_PYMONGO_3: | ||||
|             msg = 'slave_okay is deprecated as it has no impact when using PyMongo 3+.' | ||||
|             warnings.warn(msg, DeprecationWarning) | ||||
|         msg = 'slave_okay is deprecated as it has no impact when using PyMongo 3+.' | ||||
|         warnings.warn(msg, DeprecationWarning) | ||||
|         queryset = self.clone() | ||||
|         queryset._slave_okay = enabled | ||||
|         return queryset | ||||
| @@ -1211,7 +1200,7 @@ class BaseQuerySet(object): | ||||
|  | ||||
|         pipeline = initial_pipeline + list(pipeline) | ||||
|  | ||||
|         if IS_PYMONGO_3 and self._read_preference is not None: | ||||
|         if self._read_preference is not None: | ||||
|             return self._collection.with_options(read_preference=self._read_preference) \ | ||||
|                        .aggregate(pipeline, cursor={}, **kwargs) | ||||
|  | ||||
| @@ -1421,11 +1410,7 @@ class BaseQuerySet(object): | ||||
|         if isinstance(field_instances[-1], ListField): | ||||
|             pipeline.insert(1, {'$unwind': '$' + field}) | ||||
|  | ||||
|         result = self._document._get_collection().aggregate(pipeline) | ||||
|         if IS_PYMONGO_3: | ||||
|             result = tuple(result) | ||||
|         else: | ||||
|             result = result.get('result') | ||||
|         result = tuple(self._document._get_collection().aggregate(pipeline)) | ||||
|  | ||||
|         if result: | ||||
|             return result[0]['total'] | ||||
| @@ -1452,11 +1437,7 @@ class BaseQuerySet(object): | ||||
|         if isinstance(field_instances[-1], ListField): | ||||
|             pipeline.insert(1, {'$unwind': '$' + field}) | ||||
|  | ||||
|         result = self._document._get_collection().aggregate(pipeline) | ||||
|         if IS_PYMONGO_3: | ||||
|             result = tuple(result) | ||||
|         else: | ||||
|             result = result.get('result') | ||||
|         result = tuple(self._document._get_collection().aggregate(pipeline)) | ||||
|         if result: | ||||
|             return result[0]['total'] | ||||
|         return 0 | ||||
| @@ -1564,7 +1545,7 @@ class BaseQuerySet(object): | ||||
|         # XXX In PyMongo 3+, we define the read preference on a collection | ||||
|         # level, not a cursor level. Thus, we need to get a cloned collection | ||||
|         # object using `with_options` first. | ||||
|         if IS_PYMONGO_3 and self._read_preference is not None: | ||||
|         if self._read_preference is not None: | ||||
|             self._cursor_obj = self._collection\ | ||||
|                 .with_options(read_preference=self._read_preference)\ | ||||
|                 .find(self._query, **self._cursor_args) | ||||
|   | ||||
| @@ -8,9 +8,7 @@ from six import iteritems | ||||
|  | ||||
| from mongoengine.base import UPDATE_OPERATORS | ||||
| from mongoengine.common import _import_class | ||||
| from mongoengine.connection import get_connection | ||||
| from mongoengine.errors import InvalidQueryError | ||||
| from mongoengine.pymongo_support import IS_PYMONGO_3 | ||||
|  | ||||
| __all__ = ('query', 'update') | ||||
|  | ||||
| @@ -163,16 +161,14 @@ def query(_doc_cls=None, **kwargs): | ||||
|                     # PyMongo 3+ and MongoDB < 2.6 | ||||
|                     near_embedded = False | ||||
|                     for near_op in ('$near', '$nearSphere'): | ||||
|                         if isinstance(value_dict.get(near_op), dict) and ( | ||||
|                                 IS_PYMONGO_3 or get_connection().max_wire_version > 1): | ||||
|                         if isinstance(value_dict.get(near_op), dict): | ||||
|                             value_son[near_op] = SON(value_son[near_op]) | ||||
|                             if '$maxDistance' in value_dict: | ||||
|                                 value_son[near_op][ | ||||
|                                     '$maxDistance'] = value_dict['$maxDistance'] | ||||
|                                 value_son[near_op]['$maxDistance'] = value_dict['$maxDistance'] | ||||
|                             if '$minDistance' in value_dict: | ||||
|                                 value_son[near_op][ | ||||
|                                     '$minDistance'] = value_dict['$minDistance'] | ||||
|                                 value_son[near_op]['$minDistance'] = value_dict['$minDistance'] | ||||
|                             near_embedded = True | ||||
|  | ||||
|                     if not near_embedded: | ||||
|                         if '$maxDistance' in value_dict: | ||||
|                             value_son['$maxDistance'] = value_dict['$maxDistance'] | ||||
|   | ||||
		Reference in New Issue
	
	Block a user