Merge branch 'dev' into indexpatches
This commit is contained in:
@@ -747,10 +747,21 @@ class BaseDocument(object):
|
||||
if '_id' in set_data:
|
||||
del(set_data['_id'])
|
||||
|
||||
for k,v in set_data.items():
|
||||
if not v:
|
||||
del(set_data[k])
|
||||
unset_data[k] = 1
|
||||
# Determine if any changed items were actually unset.
|
||||
for path, value in set_data.items():
|
||||
if value:
|
||||
continue
|
||||
|
||||
# If we've set a value that aint the default value save it.
|
||||
if path in self._fields:
|
||||
default = self._fields[path].default
|
||||
if callable(default):
|
||||
default = default()
|
||||
if default != value:
|
||||
continue
|
||||
|
||||
del(set_data[path])
|
||||
unset_data[path] = 1
|
||||
return set_data, unset_data
|
||||
|
||||
@classmethod
|
||||
|
||||
@@ -32,6 +32,12 @@ class User(Document):
|
||||
last_login = DateTimeField(default=datetime.datetime.now)
|
||||
date_joined = DateTimeField(default=datetime.datetime.now)
|
||||
|
||||
meta = {
|
||||
'indexes': [
|
||||
{'fields': ['username'], 'unique': True}
|
||||
]
|
||||
}
|
||||
|
||||
def __unicode__(self):
|
||||
return self.username
|
||||
|
||||
|
||||
@@ -112,7 +112,7 @@ class Document(BaseDocument):
|
||||
self._collection = db[collection_name]
|
||||
return self._collection
|
||||
|
||||
def save(self, safe=True, force_insert=False, validate=True, write_options=None):
|
||||
def save(self, safe=True, force_insert=False, validate=True, write_options=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.
|
||||
@@ -131,6 +131,8 @@ class Document(BaseDocument):
|
||||
For example, ``save(..., w=2, fsync=True)`` will wait until at least two servers
|
||||
have recorded the write and will force an fsync on each server being written to.
|
||||
"""
|
||||
from fields import ReferenceField, GenericReferenceField
|
||||
|
||||
signals.pre_save.send(self.__class__, document=self)
|
||||
|
||||
if validate:
|
||||
@@ -140,6 +142,7 @@ class Document(BaseDocument):
|
||||
write_options = {}
|
||||
|
||||
doc = self.to_mongo()
|
||||
|
||||
created = '_id' not in doc
|
||||
try:
|
||||
collection = self.__class__.objects._collection
|
||||
@@ -154,6 +157,18 @@ class Document(BaseDocument):
|
||||
collection.update({'_id': object_id}, {"$set": updates}, upsert=True, safe=safe, **write_options)
|
||||
if removals:
|
||||
collection.update({'_id': object_id}, {"$unset": removals}, upsert=True, safe=safe, **write_options)
|
||||
|
||||
# Save any references / generic references
|
||||
_refs = _refs or []
|
||||
for name, cls in self._fields.items():
|
||||
if isinstance(cls, (ReferenceField, GenericReferenceField)):
|
||||
ref = getattr(self, name)
|
||||
if ref and str(ref) not in _refs:
|
||||
_refs.append(str(ref))
|
||||
ref.save(safe=safe, force_insert=force_insert,
|
||||
validate=validate, write_options=write_options,
|
||||
_refs=_refs)
|
||||
|
||||
except pymongo.errors.OperationFailure, err:
|
||||
message = 'Could not save document (%s)'
|
||||
if u'duplicate key' in unicode(err):
|
||||
|
||||
@@ -1453,7 +1453,7 @@ class QuerySet(object):
|
||||
path = '{{~%(field)s}}'.split('.');
|
||||
field = this;
|
||||
for (p in path) { field = field[path[p]]; }
|
||||
if (field.constructor == Array) {
|
||||
if (field && field.constructor == Array) {
|
||||
field.forEach(function(item) {
|
||||
emit(item, 1);
|
||||
});
|
||||
@@ -1499,7 +1499,7 @@ class QuerySet(object):
|
||||
db[collection].find(query).forEach(function(doc) {
|
||||
field = doc;
|
||||
for (p in path) { field = field[path[p]]; }
|
||||
if (field.constructor == Array) {
|
||||
if (field && field.constructor == Array) {
|
||||
total += field.length;
|
||||
} else {
|
||||
total++;
|
||||
@@ -1515,7 +1515,7 @@ class QuerySet(object):
|
||||
db[collection].find(query).forEach(function(doc) {
|
||||
field = doc;
|
||||
for (p in path) { field = field[path[p]]; }
|
||||
if (field.constructor == Array) {
|
||||
if (field && field.constructor == Array) {
|
||||
field.forEach(function(item) {
|
||||
frequencies[item] = inc + (isNaN(frequencies[item]) ? 0: frequencies[item]);
|
||||
});
|
||||
@@ -1527,8 +1527,11 @@ class QuerySet(object):
|
||||
return frequencies;
|
||||
}
|
||||
"""
|
||||
|
||||
return self.exec_js(freq_func, field, normalize=normalize)
|
||||
data = self.exec_js(freq_func, field, normalize=normalize)
|
||||
if 'undefined' in data:
|
||||
data[None] = data['undefined']
|
||||
del(data['undefined'])
|
||||
return data
|
||||
|
||||
def __repr__(self):
|
||||
limit = REPR_OUTPUT_SIZE + 1
|
||||
|
||||
Reference in New Issue
Block a user