Compare commits
7 Commits
v0.10.7
...
no-conflic
Author | SHA1 | Date | |
---|---|---|---|
|
df12211c25 | ||
|
088c5f49d9 | ||
|
d8d98b6143 | ||
|
02fb3b9315 | ||
|
4f87db784e | ||
|
7e6287b925 | ||
|
999cdfd997 |
@@ -52,10 +52,14 @@ Some simple examples of what MongoEngine code looks like:
|
|||||||
|
|
||||||
.. code :: python
|
.. code :: python
|
||||||
|
|
||||||
|
from mongoengine import *
|
||||||
|
connect('mydb')
|
||||||
|
|
||||||
class BlogPost(Document):
|
class BlogPost(Document):
|
||||||
title = StringField(required=True, max_length=200)
|
title = StringField(required=True, max_length=200)
|
||||||
posted = DateTimeField(default=datetime.datetime.now)
|
posted = DateTimeField(default=datetime.datetime.now)
|
||||||
tags = ListField(StringField(max_length=50))
|
tags = ListField(StringField(max_length=50))
|
||||||
|
meta = {'allow_inheritance': True}
|
||||||
|
|
||||||
class TextPost(BlogPost):
|
class TextPost(BlogPost):
|
||||||
content = StringField(required=True)
|
content = StringField(required=True)
|
||||||
|
@@ -4,7 +4,9 @@ Changelog
|
|||||||
|
|
||||||
Changes in 0.10.8
|
Changes in 0.10.8
|
||||||
=================
|
=================
|
||||||
- Fill this in as PRs for v0.10.8 are merged
|
- Added ability to specify an authentication mechanism (e.g. X.509) #1333
|
||||||
|
- Added support for falsey primary keys (e.g. doc.pk = 0) #1354
|
||||||
|
- Fixed BaseQuerySet#sum/average for fields w/ explicit db_field #1417
|
||||||
|
|
||||||
Changes in 0.10.7
|
Changes in 0.10.7
|
||||||
=================
|
=================
|
||||||
|
@@ -6,6 +6,7 @@ __all__ = ['ConnectionError', 'connect', 'register_connection',
|
|||||||
|
|
||||||
|
|
||||||
DEFAULT_CONNECTION_NAME = 'default'
|
DEFAULT_CONNECTION_NAME = 'default'
|
||||||
|
|
||||||
if IS_PYMONGO_3:
|
if IS_PYMONGO_3:
|
||||||
READ_PREFERENCE = ReadPreference.PRIMARY
|
READ_PREFERENCE = ReadPreference.PRIMARY
|
||||||
else:
|
else:
|
||||||
@@ -24,7 +25,9 @@ _dbs = {}
|
|||||||
|
|
||||||
def register_connection(alias, name=None, host=None, port=None,
|
def register_connection(alias, name=None, host=None, port=None,
|
||||||
read_preference=READ_PREFERENCE,
|
read_preference=READ_PREFERENCE,
|
||||||
username=None, password=None, authentication_source=None,
|
username=None, password=None,
|
||||||
|
authentication_source=None,
|
||||||
|
authentication_mechanism=None,
|
||||||
**kwargs):
|
**kwargs):
|
||||||
"""Add a connection.
|
"""Add a connection.
|
||||||
|
|
||||||
@@ -38,6 +41,9 @@ def register_connection(alias, name=None, host=None, port=None,
|
|||||||
:param username: username to authenticate with
|
:param username: username to authenticate with
|
||||||
:param password: password to authenticate with
|
:param password: password to authenticate with
|
||||||
:param authentication_source: database to authenticate against
|
:param authentication_source: database to authenticate against
|
||||||
|
:param authentication_mechanism: database authentication mechanisms.
|
||||||
|
By default, use SCRAM-SHA-1 with MongoDB 3.0 and later,
|
||||||
|
MONGODB-CR (MongoDB Challenge Response protocol) for older servers.
|
||||||
:param is_mock: explicitly use mongomock for this connection
|
:param is_mock: explicitly use mongomock for this connection
|
||||||
(can also be done by using `mongomock://` as db host prefix)
|
(can also be done by using `mongomock://` as db host prefix)
|
||||||
:param kwargs: allow ad-hoc parameters to be passed into the pymongo driver
|
:param kwargs: allow ad-hoc parameters to be passed into the pymongo driver
|
||||||
@@ -53,9 +59,11 @@ def register_connection(alias, name=None, host=None, port=None,
|
|||||||
'read_preference': read_preference,
|
'read_preference': read_preference,
|
||||||
'username': username,
|
'username': username,
|
||||||
'password': password,
|
'password': password,
|
||||||
'authentication_source': authentication_source
|
'authentication_source': authentication_source,
|
||||||
|
'authentication_mechanism': authentication_mechanism
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Handle uri style connections
|
||||||
conn_host = conn_settings['host']
|
conn_host = conn_settings['host']
|
||||||
# host can be a list or a string, so if string, force to a list
|
# host can be a list or a string, so if string, force to a list
|
||||||
if isinstance(conn_host, str_types):
|
if isinstance(conn_host, str_types):
|
||||||
@@ -63,25 +71,33 @@ def register_connection(alias, name=None, host=None, port=None,
|
|||||||
|
|
||||||
resolved_hosts = []
|
resolved_hosts = []
|
||||||
for entity in conn_host:
|
for entity in conn_host:
|
||||||
# Handle uri style connections
|
|
||||||
|
# Handle Mongomock
|
||||||
if entity.startswith('mongomock://'):
|
if entity.startswith('mongomock://'):
|
||||||
conn_settings['is_mock'] = True
|
conn_settings['is_mock'] = True
|
||||||
# `mongomock://` is not a valid url prefix and must be replaced by `mongodb://`
|
# `mongomock://` is not a valid url prefix and must be replaced by `mongodb://`
|
||||||
resolved_hosts.append(entity.replace('mongomock://', 'mongodb://', 1))
|
resolved_hosts.append(entity.replace('mongomock://', 'mongodb://', 1))
|
||||||
|
|
||||||
|
# Handle URI style connections, only updating connection params which
|
||||||
|
# were explicitly specified in the URI.
|
||||||
elif '://' in entity:
|
elif '://' in entity:
|
||||||
uri_dict = uri_parser.parse_uri(entity)
|
uri_dict = uri_parser.parse_uri(entity)
|
||||||
resolved_hosts.append(entity)
|
resolved_hosts.append(entity)
|
||||||
conn_settings.update({
|
|
||||||
'name': uri_dict.get('database') or name,
|
if uri_dict.get('database'):
|
||||||
'username': uri_dict.get('username'),
|
conn_settings['name'] = uri_dict.get('database')
|
||||||
'password': uri_dict.get('password'),
|
|
||||||
'read_preference': read_preference,
|
for param in ('read_preference', 'username', 'password'):
|
||||||
})
|
if uri_dict.get(param):
|
||||||
|
conn_settings[param] = uri_dict[param]
|
||||||
|
|
||||||
uri_options = uri_dict['options']
|
uri_options = uri_dict['options']
|
||||||
if 'replicaset' in uri_options:
|
if 'replicaset' in uri_options:
|
||||||
conn_settings['replicaSet'] = True
|
conn_settings['replicaSet'] = True
|
||||||
if 'authsource' in uri_options:
|
if 'authsource' in uri_options:
|
||||||
conn_settings['authentication_source'] = uri_options['authsource']
|
conn_settings['authentication_source'] = uri_options['authsource']
|
||||||
|
if 'authmechanism' in uri_options:
|
||||||
|
conn_settings['authentication_mechanism'] = uri_options['authmechanism']
|
||||||
else:
|
else:
|
||||||
resolved_hosts.append(entity)
|
resolved_hosts.append(entity)
|
||||||
conn_settings['host'] = resolved_hosts
|
conn_settings['host'] = resolved_hosts
|
||||||
@@ -123,6 +139,7 @@ def get_connection(alias=DEFAULT_CONNECTION_NAME, reconnect=False):
|
|||||||
conn_settings.pop('username', None)
|
conn_settings.pop('username', None)
|
||||||
conn_settings.pop('password', None)
|
conn_settings.pop('password', None)
|
||||||
conn_settings.pop('authentication_source', None)
|
conn_settings.pop('authentication_source', None)
|
||||||
|
conn_settings.pop('authentication_mechanism', None)
|
||||||
|
|
||||||
is_mock = conn_settings.pop('is_mock', None)
|
is_mock = conn_settings.pop('is_mock', None)
|
||||||
if is_mock:
|
if is_mock:
|
||||||
@@ -157,6 +174,7 @@ def get_connection(alias=DEFAULT_CONNECTION_NAME, reconnect=False):
|
|||||||
connection_settings.pop('username', None)
|
connection_settings.pop('username', None)
|
||||||
connection_settings.pop('password', None)
|
connection_settings.pop('password', None)
|
||||||
connection_settings.pop('authentication_source', None)
|
connection_settings.pop('authentication_source', None)
|
||||||
|
connection_settings.pop('authentication_mechanism', None)
|
||||||
if conn_settings == connection_settings and _connections.get(db_alias, None):
|
if conn_settings == connection_settings and _connections.get(db_alias, None):
|
||||||
connection = _connections[db_alias]
|
connection = _connections[db_alias]
|
||||||
break
|
break
|
||||||
@@ -176,11 +194,13 @@ def get_db(alias=DEFAULT_CONNECTION_NAME, reconnect=False):
|
|||||||
conn = get_connection(alias)
|
conn = get_connection(alias)
|
||||||
conn_settings = _connection_settings[alias]
|
conn_settings = _connection_settings[alias]
|
||||||
db = conn[conn_settings['name']]
|
db = conn[conn_settings['name']]
|
||||||
|
auth_kwargs = {'source': conn_settings['authentication_source']}
|
||||||
|
if conn_settings['authentication_mechanism'] is not None:
|
||||||
|
auth_kwargs['mechanism'] = conn_settings['authentication_mechanism']
|
||||||
# Authenticate if necessary
|
# Authenticate if necessary
|
||||||
if conn_settings['username'] and conn_settings['password']:
|
if conn_settings['username'] and (conn_settings['password'] or
|
||||||
db.authenticate(conn_settings['username'],
|
conn_settings['authentication_mechanism'] == 'MONGODB-X509'):
|
||||||
conn_settings['password'],
|
db.authenticate(conn_settings['username'], conn_settings['password'], **auth_kwargs)
|
||||||
source=conn_settings['authentication_source'])
|
|
||||||
_dbs[alias] = db
|
_dbs[alias] = db
|
||||||
return _dbs[alias]
|
return _dbs[alias]
|
||||||
|
|
||||||
|
@@ -472,7 +472,7 @@ class Document(BaseDocument):
|
|||||||
Raises :class:`OperationError` if called on an object that has not yet
|
Raises :class:`OperationError` if called on an object that has not yet
|
||||||
been saved.
|
been saved.
|
||||||
"""
|
"""
|
||||||
if not self.pk:
|
if self.pk is None:
|
||||||
if kwargs.get('upsert', False):
|
if kwargs.get('upsert', False):
|
||||||
query = self.to_mongo()
|
query = self.to_mongo()
|
||||||
if "_cls" in query:
|
if "_cls" in query:
|
||||||
@@ -604,7 +604,7 @@ class Document(BaseDocument):
|
|||||||
elif "max_depth" in kwargs:
|
elif "max_depth" in kwargs:
|
||||||
max_depth = kwargs["max_depth"]
|
max_depth = kwargs["max_depth"]
|
||||||
|
|
||||||
if not self.pk:
|
if self.pk is None:
|
||||||
raise self.DoesNotExist("Document does not exist")
|
raise self.DoesNotExist("Document does not exist")
|
||||||
obj = self._qs.read_preference(ReadPreference.PRIMARY).filter(
|
obj = self._qs.read_preference(ReadPreference.PRIMARY).filter(
|
||||||
**self._object_key).only(*fields).limit(
|
**self._object_key).only(*fields).limit(
|
||||||
@@ -655,7 +655,7 @@ class Document(BaseDocument):
|
|||||||
def to_dbref(self):
|
def to_dbref(self):
|
||||||
"""Returns an instance of :class:`~bson.dbref.DBRef` useful in
|
"""Returns an instance of :class:`~bson.dbref.DBRef` useful in
|
||||||
`__raw__` queries."""
|
`__raw__` queries."""
|
||||||
if not self.pk:
|
if self.pk is None:
|
||||||
msg = "Only saved documents can have a valid dbref"
|
msg = "Only saved documents can have a valid dbref"
|
||||||
raise OperationError(msg)
|
raise OperationError(msg)
|
||||||
return DBRef(self.__class__._get_collection_name(), self.pk)
|
return DBRef(self.__class__._get_collection_name(), self.pk)
|
||||||
|
@@ -1271,9 +1271,10 @@ class BaseQuerySet(object):
|
|||||||
:param field: the field to sum over; use dot notation to refer to
|
:param field: the field to sum over; use dot notation to refer to
|
||||||
embedded document fields
|
embedded document fields
|
||||||
"""
|
"""
|
||||||
|
db_field = self._fields_to_dbfields([field]).pop()
|
||||||
pipeline = [
|
pipeline = [
|
||||||
{'$match': self._query},
|
{'$match': self._query},
|
||||||
{'$group': {'_id': 'sum', 'total': {'$sum': '$' + field}}}
|
{'$group': {'_id': 'sum', 'total': {'$sum': '$' + db_field}}}
|
||||||
]
|
]
|
||||||
|
|
||||||
# if we're performing a sum over a list field, we sum up all the
|
# if we're performing a sum over a list field, we sum up all the
|
||||||
@@ -1300,9 +1301,10 @@ class BaseQuerySet(object):
|
|||||||
:param field: the field to average over; use dot notation to refer to
|
:param field: the field to average over; use dot notation to refer to
|
||||||
embedded document fields
|
embedded document fields
|
||||||
"""
|
"""
|
||||||
|
db_field = self._fields_to_dbfields([field]).pop()
|
||||||
pipeline = [
|
pipeline = [
|
||||||
{'$match': self._query},
|
{'$match': self._query},
|
||||||
{'$group': {'_id': 'avg', 'total': {'$avg': '$' + field}}}
|
{'$group': {'_id': 'avg', 'total': {'$avg': '$' + db_field}}}
|
||||||
]
|
]
|
||||||
|
|
||||||
# if we're performing an average over a list field, we average out
|
# if we're performing an average over a list field, we average out
|
||||||
|
@@ -3202,5 +3202,20 @@ class InstanceTest(unittest.TestCase):
|
|||||||
self.assertEqual(b._instance, a)
|
self.assertEqual(b._instance, a)
|
||||||
self.assertEqual(idx, 2)
|
self.assertEqual(idx, 2)
|
||||||
|
|
||||||
|
def test_falsey_pk(self):
|
||||||
|
"""Ensure that we can create and update a document with Falsey PK.
|
||||||
|
"""
|
||||||
|
class Person(Document):
|
||||||
|
age = IntField(primary_key=True)
|
||||||
|
height = FloatField()
|
||||||
|
|
||||||
|
person = Person()
|
||||||
|
person.age = 0
|
||||||
|
person.height = 1.89
|
||||||
|
person.save()
|
||||||
|
|
||||||
|
person.update(set__height=2.0)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
@@ -2838,6 +2838,34 @@ class QuerySetTest(unittest.TestCase):
|
|||||||
sum([a for a in ages if a >= 50])
|
sum([a for a in ages if a >= 50])
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def test_sum_over_db_field(self):
|
||||||
|
"""Ensure that a field mapped to a db field with a different name
|
||||||
|
can be summed over correctly.
|
||||||
|
"""
|
||||||
|
class UserVisit(Document):
|
||||||
|
num_visits = IntField(db_field='visits')
|
||||||
|
|
||||||
|
UserVisit.drop_collection()
|
||||||
|
|
||||||
|
UserVisit.objects.create(num_visits=10)
|
||||||
|
UserVisit.objects.create(num_visits=5)
|
||||||
|
|
||||||
|
self.assertEqual(UserVisit.objects.sum('num_visits'), 15)
|
||||||
|
|
||||||
|
def test_average_over_db_field(self):
|
||||||
|
"""Ensure that a field mapped to a db field with a different name
|
||||||
|
can have its average computed correctly.
|
||||||
|
"""
|
||||||
|
class UserVisit(Document):
|
||||||
|
num_visits = IntField(db_field='visits')
|
||||||
|
|
||||||
|
UserVisit.drop_collection()
|
||||||
|
|
||||||
|
UserVisit.objects.create(num_visits=20)
|
||||||
|
UserVisit.objects.create(num_visits=10)
|
||||||
|
|
||||||
|
self.assertEqual(UserVisit.objects.average('num_visits'), 15)
|
||||||
|
|
||||||
def test_embedded_average(self):
|
def test_embedded_average(self):
|
||||||
class Pay(EmbeddedDocument):
|
class Pay(EmbeddedDocument):
|
||||||
value = DecimalField()
|
value = DecimalField()
|
||||||
|
@@ -174,19 +174,9 @@ class ConnectionTest(unittest.TestCase):
|
|||||||
c.mongoenginetest.system.users.remove({})
|
c.mongoenginetest.system.users.remove({})
|
||||||
|
|
||||||
def test_connect_uri_without_db(self):
|
def test_connect_uri_without_db(self):
|
||||||
"""Ensure connect() method works properly with uri's without database_name
|
"""Ensure connect() method works properly if the URI doesn't
|
||||||
|
include a database name.
|
||||||
"""
|
"""
|
||||||
c = connect(db='mongoenginetest', alias='admin')
|
|
||||||
c.admin.system.users.remove({})
|
|
||||||
c.mongoenginetest.system.users.remove({})
|
|
||||||
|
|
||||||
c.admin.add_user("admin", "password")
|
|
||||||
c.admin.authenticate("admin", "password")
|
|
||||||
c.mongoenginetest.add_user("username", "password")
|
|
||||||
|
|
||||||
if not IS_PYMONGO_3:
|
|
||||||
self.assertRaises(ConnectionError, connect, "testdb_uri_bad", host='mongodb://test:password@localhost')
|
|
||||||
|
|
||||||
connect("mongoenginetest", host='mongodb://localhost/')
|
connect("mongoenginetest", host='mongodb://localhost/')
|
||||||
|
|
||||||
conn = get_connection()
|
conn = get_connection()
|
||||||
@@ -196,8 +186,31 @@ class ConnectionTest(unittest.TestCase):
|
|||||||
self.assertTrue(isinstance(db, pymongo.database.Database))
|
self.assertTrue(isinstance(db, pymongo.database.Database))
|
||||||
self.assertEqual(db.name, 'mongoenginetest')
|
self.assertEqual(db.name, 'mongoenginetest')
|
||||||
|
|
||||||
c.admin.system.users.remove({})
|
def test_connect_uri_default_db(self):
|
||||||
c.mongoenginetest.system.users.remove({})
|
"""Ensure connect() defaults to the right database name if
|
||||||
|
the URI and the database_name don't explicitly specify it.
|
||||||
|
"""
|
||||||
|
connect(host='mongodb://localhost/')
|
||||||
|
|
||||||
|
conn = get_connection()
|
||||||
|
self.assertTrue(isinstance(conn, pymongo.mongo_client.MongoClient))
|
||||||
|
|
||||||
|
db = get_db()
|
||||||
|
self.assertTrue(isinstance(db, pymongo.database.Database))
|
||||||
|
self.assertEqual(db.name, 'test')
|
||||||
|
|
||||||
|
def test_uri_without_credentials_doesnt_override_conn_settings(self):
|
||||||
|
"""Ensure connect() uses the username & password params if the URI
|
||||||
|
doesn't explicitly specify them.
|
||||||
|
"""
|
||||||
|
c = connect(host='mongodb://localhost/mongoenginetest',
|
||||||
|
username='user',
|
||||||
|
password='pass')
|
||||||
|
|
||||||
|
# OperationFailure means that mongoengine attempted authentication
|
||||||
|
# w/ the provided username/password and failed - that's the desired
|
||||||
|
# behavior. If the MongoDB URI would override the credentials
|
||||||
|
self.assertRaises(OperationFailure, get_db)
|
||||||
|
|
||||||
def test_connect_uri_with_authsource(self):
|
def test_connect_uri_with_authsource(self):
|
||||||
"""Ensure that the connect() method works well with
|
"""Ensure that the connect() method works well with
|
||||||
|
Reference in New Issue
Block a user