Skip to content

Commit 7a3adcf

Browse files
author
Mark Unsworth
committed
Merge pull request #1 from django-nonrel/master
Merge from upstream
2 parents cb2ff80 + 6781ea3 commit 7a3adcf

File tree

6 files changed

+46
-11
lines changed

6 files changed

+46
-11
lines changed

CHANGELOG.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ Changelog
33

44
.. currentmodule:: djangotoolbox.fields
55

6+
Version 0.5.2 (TBD)
7+
-----------------
8+
* Add support for Replica Sets (Thanks @r4fek)
9+
10+
611
Version 0.5.1 (Nov 2013)
712
-----------------
813
* Fixed packaging issues

django_mongodb_engine/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/python
22
# -*- coding: utf-8 -*-
33

4-
__version__ = (0, 5, 1)
4+
__version__ = (0, 5, 2)
55
__author__ = "Flavio Percoco Premoli, Alberto Paro, " + \
66
"Jonas Haag and contributors"
77
__contact__ = "[email protected]"

django_mongodb_engine/base.py

Lines changed: 35 additions & 4 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:
@@ -188,7 +191,7 @@ def __init__(self, *args, **kwargs):
188191

189192
def get_collection(self, name, **kwargs):
190193
if (kwargs.pop('existing', False) and
191-
name not in self.connection.database.collection_names()):
194+
name not in self.database.collection_names()):
192195
return None
193196
collection = self.collection_class(self.database, name, **kwargs)
194197
if settings.DEBUG:
@@ -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()

django_mongodb_engine/query.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
from warnings import warn
2-
warn("A() queries are deprecated as of 0.5 and will be removed in 0.6.",
3-
DeprecationWarning)
4-
52

63
from djangotoolbox.fields import RawField, AbstractIterableField, \
74
EmbeddedModelField
@@ -16,6 +13,8 @@
1613
class A(object):
1714

1815
def __init__(self, op, value):
16+
warn("A() queries are deprecated as of 0.5 and will be removed in 0.6.", DeprecationWarning)
17+
1918
self.op = op
2019
self.val = value
2120

docs/source/topics/lists-and-dicts.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ Stores Python lists (or any other iterable), represented in BSON as arrays. ::
2626
::
2727

2828
>>> Post(tags=['django', 'mongodb'], ...).save()
29-
>>> Post.objecs.get(...).tags
29+
>>> Post.objects.get(...).tags
3030
['django', 'mongodb']
3131

3232
The typed variant automatically does type conversions according to the given type::

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)