Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getting tests passing #7

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion mongoengine/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
__all__ = (document.__all__ + fields.__all__ + connection.__all__ +
queryset.__all__ + signals.__all__)

VERSION = (0, 6, 14)
VERSION = (0, 6, 22)


def get_version():
Expand Down
10 changes: 6 additions & 4 deletions mongoengine/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1143,12 +1143,14 @@ def __str__(self):

def __eq__(self, other):
if isinstance(other, self.__class__) and hasattr(other, 'id'):
if self.id == other.id:
return True
return False
return self.id == other.id
return NotImplemented

def __ne__(self, other):
return not self.__eq__(other)
res = self.__eq__(other)
if res == NotImplemented:
return NotImplemented
return not res

def __hash__(self):
if self.pk is None:
Expand Down
17 changes: 6 additions & 11 deletions mongoengine/connection.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import pymongo
from pymongo import Connection, ReplicaSetConnection, uri_parser
from pymongo import MongoClient, uri_parser
from pymongo.read_preferences import ReadPreference


__all__ = ['ConnectionError', 'connect', 'register_connection',
Expand All @@ -22,8 +23,8 @@ class ConnectionError(Exception):


def register_connection(alias, host='localhost', port=27017,
is_slave=False, read_preference=False, slaves=None,
username=None, password=None, **kwargs):
is_slave=False, read_preference=ReadPreference.PRIMARY,
slaves=None, username=None, password=None, **kwargs):
"""Add a connection.

:param alias: the name that will be used to refer to this connection
Expand Down Expand Up @@ -71,9 +72,7 @@ def disconnect(alias=DEFAULT_CONNECTION_NAME):

if alias in _connections:
conn = get_connection(alias=alias)
conn.disconnect()
if hasattr(conn, 'close'):
conn.close()
conn.close()
del _connections[alias]
if alias in _dbs:
del _dbs[alias]
Expand Down Expand Up @@ -107,12 +106,8 @@ def get_connection(alias=DEFAULT_CONNECTION_NAME, reconnect=False):
conn_settings['slaves'] = slaves
conn_settings.pop('read_preference')

connection_class = Connection
if 'replicaSet' in conn_settings:
conn_settings['hosts_or_uri'] = conn_settings.pop('host', None)
connection_class = ReplicaSetConnection
try:
_connections[alias] = connection_class(**conn_settings)
_connections[alias] = MongoClient(**conn_settings)
except Exception, e:
raise ConnectionError("Cannot connect to database %s :\n%s" % (alias, e))
return _connections[alias]
Expand Down
23 changes: 8 additions & 15 deletions mongoengine/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,16 +138,12 @@ def _get_collection(cls):
cls._collection = db[collection_name]
return cls._collection

def save(self, safe=True, force_insert=False, validate=True, write_options=None,
def save(self, force_insert=False, validate=True, write_options=None,
cascade=None, cascade_kwargs=None, _refs=None):
"""Save the :class:`~mongoengine.Document` to the database. If the
document already exists, it will be updated, otherwise it will be
created.

If ``safe=True`` and the operation is unsuccessful, an
:class:`~mongoengine.OperationError` will be raised.

:param safe: check if the operation succeeded before returning
:param force_insert: only try to create a new document, don't allow
updates of existing documents
:param validate: validates the document; set to ``False`` to skip.
Expand Down Expand Up @@ -179,7 +175,7 @@ def save(self, safe=True, force_insert=False, validate=True, write_options=None,
self.validate()

if not write_options:
write_options = {}
write_options = {"w": 1}

doc = self.to_mongo()

Expand All @@ -189,9 +185,9 @@ def save(self, safe=True, force_insert=False, validate=True, write_options=None,
collection = self.__class__.objects._collection
if created:
if force_insert:
object_id = collection.insert(doc, safe=safe, **write_options)
object_id = collection.insert(doc, **write_options)
else:
object_id = collection.save(doc, safe=safe, **write_options)
object_id = collection.save(doc, **write_options)
else:
object_id = doc['_id']
updates, removals = self._delta()
Expand All @@ -205,14 +201,13 @@ def save(self, safe=True, force_insert=False, validate=True, write_options=None,

upsert = self._created
if updates:
collection.update(select_dict, {"$set": updates}, upsert=upsert, safe=safe, **write_options)
collection.update(select_dict, {"$set": updates}, upsert=upsert, **write_options)
if removals:
collection.update(select_dict, {"$unset": removals}, upsert=upsert, safe=safe, **write_options)
collection.update(select_dict, {"$unset": removals}, upsert=upsert, **write_options)

cascade = self._meta.get('cascade', True) if cascade is None else cascade
if cascade:
kwargs = {
"safe": safe,
"force_insert": force_insert,
"validate": validate,
"write_options": write_options,
Expand Down Expand Up @@ -269,16 +264,14 @@ def update(self, **kwargs):
select_dict[k] = getattr(self, k)
return self.__class__.objects(**select_dict).update_one(**kwargs)

def delete(self, safe=False):
def delete(self, w=1):
"""Delete the :class:`~mongoengine.Document` from the database. This
will only take effect if the document has been previously saved.

:param safe: check if the operation succeeded before returning
"""
signals.pre_delete.send(self.__class__, document=self)

try:
self.__class__.objects(pk=self.pk).delete(safe=safe)
self.__class__.objects(pk=self.pk).delete(w=w)
except pymongo.errors.OperationFailure, err:
message = u'Could not delete document (%s)' % err.message
raise OperationError(message)
Expand Down
Loading