Skip to content

Commit 2c44a61

Browse files
committed
Merge pull request #185 from r4fek/feature/issue170
Add support for Replica Sets using MongoClient and MongoReplicaSetClient. Closes #170
2 parents cb2ff80 + 9640212 commit 2c44a61

File tree

2 files changed

+36
-5
lines changed

2 files changed

+36
-5
lines changed

django_mongodb_engine/base.py

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,17 @@
22
import datetime
33
import decimal
44
import sys
5+
import warnings
56

67
from django.conf import settings
78
from django.core.exceptions import ImproperlyConfigured
89
from django.db.backends.signals import connection_created
910
from django.db.utils import DatabaseError
11+
from pymongo import ReadPreference
1012

1113
from pymongo.collection import Collection
12-
from pymongo.connection import Connection
14+
from pymongo.mongo_client import MongoClient
15+
from pymongo.mongo_replica_set_client import MongoReplicaSetClient
1316

1417
# handle pymongo backward compatibility
1518
try:
@@ -207,9 +210,10 @@ def _connect(self):
207210

208211
def pop(name, default=None):
209212
return settings.pop(name) or default
213+
210214
db_name = pop('NAME')
211215
host = pop('HOST')
212-
port = pop('PORT')
216+
port = pop('PORT', 27017)
213217
user = pop('USER')
214218
password = pop('PASSWORD')
215219
options = pop('OPTIONS', {})
@@ -226,8 +230,35 @@ def pop(name, default=None):
226230
for key in options.iterkeys():
227231
options[key.lower()] = options.pop(key)
228232

233+
read_preference = options.get('read_preference')
234+
replicaset = options.get('replicaset')
235+
236+
if not read_preference:
237+
read_preference = options.get('slave_okay', options.get('slaveok'))
238+
if read_preference:
239+
options['read_preference'] = ReadPreference.SECONDARY
240+
warnings.warn("slave_okay has been deprecated. "
241+
"Please use read_preference instead.")
242+
243+
if replicaset:
244+
connection_class = MongoReplicaSetClient
245+
else:
246+
connection_class = MongoClient
247+
248+
conn_options = dict(
249+
host=host,
250+
port=int(port),
251+
max_pool_size=None,
252+
document_class=dict,
253+
tz_aware=False,
254+
_connect=True,
255+
auto_start_request=True,
256+
safe=False
257+
)
258+
conn_options.update(options)
259+
229260
try:
230-
self.connection = Connection(host=host, port=port, **options)
261+
self.connection = connection_class(**conn_options)
231262
self.database = self.connection[db_name]
232263
except TypeError:
233264
exc_info = sys.exc_info()

tests/mongodb/tests.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def test_generic_field(self):
5555
self.assertEqual(RawModel.objects.get(id=id).raw, obj)
5656

5757
def test_databasewrapper_api(self):
58-
from pymongo.connection import Connection
58+
from pymongo.mongo_client import MongoClient
5959
from pymongo.database import Database
6060
from pymongo.collection import Collection
6161
from random import shuffle
@@ -70,7 +70,7 @@ def test_databasewrapper_api(self):
7070
lambda: self.assertIsInstance(wrapper.get_collection('foo'),
7171
Collection),
7272
lambda: self.assertIsInstance(wrapper.database, Database),
73-
lambda: self.assertIsInstance(wrapper.connection, Connection),
73+
lambda: self.assertIsInstance(wrapper.connection, MongoClient),
7474
]
7575
shuffle(calls)
7676
for call in calls:

0 commit comments

Comments
 (0)