From c502c757cd8293e776403966d7078ac63bae3638 Mon Sep 17 00:00:00 2001 From: Yoav Shapira Date: Tue, 8 Jan 2013 11:04:47 -0500 Subject: [PATCH 01/12] Update setup.py versions, update to use MongoReplicaSetClient. --- minimongo/model.py | 33 +++++++++++++++------------------ setup.py | 4 ++-- 2 files changed, 17 insertions(+), 20 deletions(-) diff --git a/minimongo/model.py b/minimongo/model.py index 523e0cf..eaf7947 100644 --- a/minimongo/model.py +++ b/minimongo/model.py @@ -5,8 +5,8 @@ from bson import DBRef, ObjectId from minimongo.collection import DummyCollection from minimongo.options import _Options -from pymongo import Connection - +from pymongo import MongoReplicaSetClient +from pymongo.read_preference import ReadPreference class ModelBase(type): """Metaclass for all models. @@ -15,7 +15,7 @@ class ModelBase(type): populated from the parrent's Meta if any. """ - # A very rudimentary connection pool. + # A very rudimentary connection pool, keyed by replicaSet name. _connections = {} def __new__(mcs, name, bases, attrs): @@ -44,26 +44,23 @@ def __new__(mcs, name, bases, attrs): new_class.collection = DummyCollection return new_class - if not (options.host and options.port and options.database): + if not (options.host and options.port and options.database and options.replicaSetName and options.replicaSetUri): raise Exception( - 'Model %r improperly configured: %s %s %s' % ( - name, options.host, options.port, options.database)) + 'Model %r improperly configured: %s %s %s %s %s' % ( + name, options.host, options.port, options.database, options.replicaSetName, options.replicaSetUri)) - # Checking connection pool for an existing connection. - hostport = options.host, options.port - if hostport in mcs._connections: - connection = mcs._connections[hostport] + # Checking connection / client pool for an existing connection / client. + replicaSetName = options.replicaSetName + if replicaSetName in mcs._connections: + client = mcs._connections[replicaSetName] else: - # _connect=False option - # creates :class:`pymongo.connection.Connection` object without - # establishing connection. It's required if there is no running - # mongodb at this time but we want to create :class:`Model`. - connection = Connection(*hostport, _connect=False) - mcs._connections[hostport] = connection + client = MongoReplicaSetClient(options.replicaSetUri, replicaSet=replicaSetName).test + client.read_preference = ReadPreference.SECONDARY_PREFERRED + mcs._connections[replicaSetName] = client new_class._meta = options - new_class.connection = connection - new_class.database = connection[options.database] + new_class.connection = client + new_class.database = client[options.database] if options.username and options.password: new_class.database.authenticate(options.username, options.password) new_class.collection = options.collection_class( diff --git a/setup.py b/setup.py index 4ff8438..68c67a1 100644 --- a/setup.py +++ b/setup.py @@ -43,12 +43,12 @@ def run(self): requires = ["pymongo"] setup(name="minimongo", - version="0.2.6", + version="0.3.0", packages=find_packages(), cmdclass={"test": PyTest}, platforms=["any"], - install_requires = ["pymongo>=1.9"], + install_requires = ["pymongo>=2.4"], zip_safe=False, include_package_data=True, From 9a30396fbd7095f536f25922edc8646ee1539eb9 Mon Sep 17 00:00:00 2001 From: Yoav Shapira Date: Tue, 8 Jan 2013 11:11:06 -0500 Subject: [PATCH 02/12] Ignore venv. --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index a2b31ea..4b93e5a 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ dist/ minimongo.egg-info/ minimongo/app_config.py pymongo-*/ +venv From 8c1a7ed33e3b877c06428abe30d5cf91a6f072a3 Mon Sep 17 00:00:00 2001 From: Yoav Shapira Date: Wed, 9 Jan 2013 12:04:04 -0500 Subject: [PATCH 03/12] Working on using replica sets. --- minimongo/model.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/minimongo/model.py b/minimongo/model.py index eaf7947..c3a3008 100644 --- a/minimongo/model.py +++ b/minimongo/model.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- import copy - +import logging import re from bson import DBRef, ObjectId from minimongo.collection import DummyCollection @@ -53,8 +53,10 @@ def __new__(mcs, name, bases, attrs): replicaSetName = options.replicaSetName if replicaSetName in mcs._connections: client = mcs._connections[replicaSetName] + logging.debug("Got database client from pool for replicaSetName=%s" % replicaSetName) else: - client = MongoReplicaSetClient(options.replicaSetUri, replicaSet=replicaSetName).test + logging.debug("Creating new database client for replicaSetName=%s" % replicaSetName) + client = MongoReplicaSetClient(options.replicaSetUri, replicaSet=replicaSetName)[options.database] client.read_preference = ReadPreference.SECONDARY_PREFERRED mcs._connections[replicaSetName] = client From 08789f50badd8bd0c4ddd49f90c9865105e492b8 Mon Sep 17 00:00:00 2001 From: Yoav Shapira Date: Wed, 9 Jan 2013 12:16:13 -0500 Subject: [PATCH 04/12] Fixed typo in import. --- minimongo/model.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/minimongo/model.py b/minimongo/model.py index c3a3008..f8f85a3 100644 --- a/minimongo/model.py +++ b/minimongo/model.py @@ -6,7 +6,7 @@ from minimongo.collection import DummyCollection from minimongo.options import _Options from pymongo import MongoReplicaSetClient -from pymongo.read_preference import ReadPreference +from pymongo.read_preferences import ReadPreference class ModelBase(type): """Metaclass for all models. From f080287595b22a5900fc1af271ae0a5b565736f7 Mon Sep 17 00:00:00 2001 From: Yoav Shapira Date: Wed, 9 Jan 2013 12:28:16 -0500 Subject: [PATCH 05/12] Add replica set options to options.py. Pythonic spelling for them. Work without them too. --- minimongo/model.py | 34 ++++++++++++++++++++++------------ minimongo/options.py | 4 ++++ 2 files changed, 26 insertions(+), 12 deletions(-) diff --git a/minimongo/model.py b/minimongo/model.py index f8f85a3..051ed1d 100644 --- a/minimongo/model.py +++ b/minimongo/model.py @@ -44,22 +44,32 @@ def __new__(mcs, name, bases, attrs): new_class.collection = DummyCollection return new_class - if not (options.host and options.port and options.database and options.replicaSetName and options.replicaSetUri): + if not (options.host and options.port and options.database): raise Exception( - 'Model %r improperly configured: %s %s %s %s %s' % ( - name, options.host, options.port, options.database, options.replicaSetName, options.replicaSetUri)) + 'Model %r improperly configured: %s %s %s' % ( + name, options.host, options.port, options.database)) # Checking connection / client pool for an existing connection / client. - replicaSetName = options.replicaSetName - if replicaSetName in mcs._connections: - client = mcs._connections[replicaSetName] - logging.debug("Got database client from pool for replicaSetName=%s" % replicaSetName) + pool_key = options.host,options.port + if options.replica_set_name: + logging.debug("Using replica_set_name=%s as database pool key." % options.replica_set_name) + pool_key = options.replica_set_name + + if pool_key in mcs._connections: + client = mcs._connections[pool_key] + logging.debug("Got database client from pool for pool_key=%s" % pool_key) else: - logging.debug("Creating new database client for replicaSetName=%s" % replicaSetName) - client = MongoReplicaSetClient(options.replicaSetUri, replicaSet=replicaSetName)[options.database] - client.read_preference = ReadPreference.SECONDARY_PREFERRED - mcs._connections[replicaSetName] = client - + logging.debug("Creating new database client for pool_key=%s" % pool_key) + if options.replica_set_name: + logging.debug("Setting up a replica set client...") + client = MongoReplicaSetClient(options.replica_set_uri, replicaSet=options.replica_set_name)[options.database] + client.read_preference = ReadPreference.SECONDARY_PREFERRED + else: + logging.debug("Setting up a normal client...") + client = MongoClient(options.host, options.port)[options.database] + + mcs._connections[pool_key] = client + new_class._meta = options new_class.connection = client new_class.database = client[options.database] diff --git a/minimongo/options.py b/minimongo/options.py index 96b497c..1bd6e37 100644 --- a/minimongo/options.py +++ b/minimongo/options.py @@ -51,6 +51,10 @@ class _Options(object): username = None password = None + # Replica set details. + replica_set_name = None + replica_set_uri = None + # Should indices be created at startup? auto_index = True From 0c278664b8dce7fc85df67f323da56bbd40a5b9c Mon Sep 17 00:00:00 2001 From: Yoav Shapira Date: Wed, 9 Jan 2013 12:28:26 -0500 Subject: [PATCH 06/12] Add replica set options to options.py. Pythonic spelling for them. Work without them too. --- minimongo/model.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/minimongo/model.py b/minimongo/model.py index 051ed1d..f0bb654 100644 --- a/minimongo/model.py +++ b/minimongo/model.py @@ -5,7 +5,7 @@ from bson import DBRef, ObjectId from minimongo.collection import DummyCollection from minimongo.options import _Options -from pymongo import MongoReplicaSetClient +from pymongo import MongoClient, MongoReplicaSetClient from pymongo.read_preferences import ReadPreference class ModelBase(type): From f1012c428160c2af40db2fa94afe6f3abb2014bd Mon Sep 17 00:00:00 2001 From: Yoav Shapira Date: Wed, 9 Jan 2013 12:32:40 -0500 Subject: [PATCH 07/12] Don't select a databae yet. --- minimongo/model.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/minimongo/model.py b/minimongo/model.py index f0bb654..3b5ad83 100644 --- a/minimongo/model.py +++ b/minimongo/model.py @@ -62,11 +62,11 @@ def __new__(mcs, name, bases, attrs): logging.debug("Creating new database client for pool_key=%s" % pool_key) if options.replica_set_name: logging.debug("Setting up a replica set client...") - client = MongoReplicaSetClient(options.replica_set_uri, replicaSet=options.replica_set_name)[options.database] + client = MongoReplicaSetClient(options.replica_set_uri, replicaSet=options.replica_set_name) client.read_preference = ReadPreference.SECONDARY_PREFERRED else: logging.debug("Setting up a normal client...") - client = MongoClient(options.host, options.port)[options.database] + client = MongoClient(options.host, options.port) mcs._connections[pool_key] = client From e17765ef064cb58c69437f3d6aa27029b2e17002 Mon Sep 17 00:00:00 2001 From: Yoav Shapira Date: Mon, 14 Jan 2013 10:15:20 -0500 Subject: [PATCH 08/12] Fix bug, applying patch from Parker. --- minimongo/model.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/minimongo/model.py b/minimongo/model.py index 3b5ad83..b59d600 100644 --- a/minimongo/model.py +++ b/minimongo/model.py @@ -57,9 +57,9 @@ def __new__(mcs, name, bases, attrs): if pool_key in mcs._connections: client = mcs._connections[pool_key] - logging.debug("Got database client from pool for pool_key=%s" % pool_key) + logging.debug("Got database client from pool for pool_key=%s" % (pool_key,)) else: - logging.debug("Creating new database client for pool_key=%s" % pool_key) + logging.debug("Creating new database client for pool_key=%s" % (pool_key,)) if options.replica_set_name: logging.debug("Setting up a replica set client...") client = MongoReplicaSetClient(options.replica_set_uri, replicaSet=options.replica_set_name) From 31c86737760b0b11d731d1c3b0b886f4dd8c148e Mon Sep 17 00:00:00 2001 From: Yoav Shapira Date: Mon, 14 Jan 2013 10:15:54 -0500 Subject: [PATCH 09/12] Bump version in setup.py. --- setup.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/setup.py b/setup.py index 68c67a1..e524ffd 100644 --- a/setup.py +++ b/setup.py @@ -43,7 +43,7 @@ def run(self): requires = ["pymongo"] setup(name="minimongo", - version="0.3.0", + version="0.3.1", packages=find_packages(), cmdclass={"test": PyTest}, platforms=["any"], @@ -58,5 +58,5 @@ def run(self): long_description=LONG_DESCRIPTION, classifiers=CLASSIFIERS, keywords=["mongo", "mongodb", "pymongo", "orm"], - url="http://github.com/slacy/minimongo", + url="http://github.com/HappierApp/minimongo", ) From be8c593cfefbf13c213b790c1b2d5b9e65f6f8b6 Mon Sep 17 00:00:00 2001 From: J Rob Gant Date: Sat, 20 May 2017 16:24:41 -0400 Subject: [PATCH 10/12] Fix tests, update documentation --- README.rst | 4 +++- minimongo/tests/test_model.py | 4 +--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/README.rst b/README.rst index 75eed53..2c75658 100644 --- a/README.rst +++ b/README.rst @@ -28,10 +28,12 @@ you can use ``easy_install -U minimongo``. Otherwise, you can download the source from `GitHub `_ and run ``python setup.py install``. +Instead of installing as above, you should use `pip install -e .`. + Dependencies ============ -- pymongo_ 1.9+ +- pymongo_ 2.8+ - `sphinx `_ (optional -- for documentation generation) diff --git a/minimongo/tests/test_model.py b/minimongo/tests/test_model.py index 2bda954..d5feee9 100644 --- a/minimongo/tests/test_model.py +++ b/minimongo/tests/test_model.py @@ -480,9 +480,7 @@ def test_collection_class(): def test_str(): assert str(TestModel()) == 'TestModel({})' - assert str(TestModel({'foo': 'bar'})) == 'TestModel({\'foo\': \'bar\'})' - - assert str(TestModel({'foo': 'bar'})) == 'TestModel({\'foo\': \'bar\'})' + assert str(TestModel({'foo': 'bar'})) == 'TestModel({u\'foo\': u\'bar\'})' def test_auto_collection_name(): From 194bb846a333e8d217d81032a41e593ae542a94b Mon Sep 17 00:00:00 2001 From: J Rob Gant Date: Sat, 20 May 2017 21:34:10 -0400 Subject: [PATCH 11/12] Update for pymongo 2.9+. Remove safe arg from save. Rename fields to projection. --- minimongo/collection.py | 1 + minimongo/model.py | 1 + setup.py | 4 ++-- 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/minimongo/collection.py b/minimongo/collection.py index 1ebcb07..1544469 100644 --- a/minimongo/collection.py +++ b/minimongo/collection.py @@ -8,6 +8,7 @@ class Cursor(PyMongoCursor): def __init__(self, *args, **kwargs): self._wrapper_class = kwargs.pop('wrap') + kwargs['projection'] = kwargs.pop('fields', None) super(Cursor, self).__init__(*args, **kwargs) def next(self): diff --git a/minimongo/model.py b/minimongo/model.py index fb13b27..79e8ec5 100644 --- a/minimongo/model.py +++ b/minimongo/model.py @@ -228,6 +228,7 @@ def mongo_update(self, values=None, **kwargs): def save(self, *args, **kwargs): """Save this object to it's mongo collection.""" + kwargs.pop('safe', None) self.collection.save(self, *args, **kwargs) return self diff --git a/setup.py b/setup.py index 8e47fd0..e45b14a 100644 --- a/setup.py +++ b/setup.py @@ -46,12 +46,12 @@ def run(self): requires = ["pymongo"] setup(name="minimongo", - version="0.3.2", + version="0.3.3", packages=find_packages(), cmdclass={"test": PyTest}, platforms=["any"], - install_requires = ["pymongo<=2.8", "six"], + install_requires = ["pymongo>=2.9", "six"], zip_safe=False, include_package_data=True, From 4a92f15c4a15f874f08f91832972fd9810b43728 Mon Sep 17 00:00:00 2001 From: J Rob Gant Date: Sun, 21 May 2017 10:24:43 -0400 Subject: [PATCH 12/12] Remove six install_requires trying to resolve heroku deploy. --- setup.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/setup.py b/setup.py index e45b14a..d9d1d6a 100644 --- a/setup.py +++ b/setup.py @@ -46,12 +46,12 @@ def run(self): requires = ["pymongo"] setup(name="minimongo", - version="0.3.3", + version="0.3.4", packages=find_packages(), cmdclass={"test": PyTest}, platforms=["any"], - install_requires = ["pymongo>=2.9", "six"], + install_requires = ["pymongo>=2.9"], zip_safe=False, include_package_data=True,