2
2
import datetime
3
3
import decimal
4
4
import sys
5
+ import warnings
5
6
6
7
from django .conf import settings
7
8
from django .core .exceptions import ImproperlyConfigured
8
9
from django .db .backends .signals import connection_created
9
10
from django .db .utils import DatabaseError
11
+ from pymongo import ReadPreference
10
12
11
13
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
13
16
14
17
# handle pymongo backward compatibility
15
18
try :
@@ -207,9 +210,10 @@ def _connect(self):
207
210
208
211
def pop (name , default = None ):
209
212
return settings .pop (name ) or default
213
+
210
214
db_name = pop ('NAME' )
211
215
host = pop ('HOST' )
212
- port = pop ('PORT' )
216
+ port = pop ('PORT' , 27017 )
213
217
user = pop ('USER' )
214
218
password = pop ('PASSWORD' )
215
219
options = pop ('OPTIONS' , {})
@@ -226,8 +230,35 @@ def pop(name, default=None):
226
230
for key in options .iterkeys ():
227
231
options [key .lower ()] = options .pop (key )
228
232
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
+
229
260
try :
230
- self .connection = Connection ( host = host , port = port , ** options )
261
+ self .connection = connection_class ( ** conn_options )
231
262
self .database = self .connection [db_name ]
232
263
except TypeError :
233
264
exc_info = sys .exc_info ()
0 commit comments