MongoEngine/mongoengine #1217: Curry **kwargs through to_mongo on fields
This commit is contained in:
committed by
Bryan Bennett
parent
decd70eb23
commit
6c9e1799c7
@@ -325,7 +325,7 @@ class DecimalField(BaseField):
|
||||
return value
|
||||
return value.quantize(decimal.Decimal(".%s" % ("0" * self.precision)), rounding=self.rounding)
|
||||
|
||||
def to_mongo(self, value, use_db_field=True):
|
||||
def to_mongo(self, value, **kwargs):
|
||||
if value is None:
|
||||
return value
|
||||
if self.force_string:
|
||||
@@ -388,7 +388,7 @@ class DateTimeField(BaseField):
|
||||
if not isinstance(new_value, (datetime.datetime, datetime.date)):
|
||||
self.error(u'cannot parse date "%s"' % value)
|
||||
|
||||
def to_mongo(self, value):
|
||||
def to_mongo(self, value, **kwargs):
|
||||
if value is None:
|
||||
return value
|
||||
if isinstance(value, datetime.datetime):
|
||||
@@ -511,7 +511,7 @@ class ComplexDateTimeField(StringField):
|
||||
except:
|
||||
return original_value
|
||||
|
||||
def to_mongo(self, value):
|
||||
def to_mongo(self, value, **kwargs):
|
||||
value = self.to_python(value)
|
||||
return self._convert_from_datetime(value)
|
||||
|
||||
@@ -546,11 +546,10 @@ class EmbeddedDocumentField(BaseField):
|
||||
return self.document_type._from_son(value, _auto_dereference=self._auto_dereference)
|
||||
return value
|
||||
|
||||
def to_mongo(self, value, use_db_field=True, fields=[]):
|
||||
def to_mongo(self, value, **kwargs):
|
||||
if not isinstance(value, self.document_type):
|
||||
return value
|
||||
return self.document_type.to_mongo(value, use_db_field,
|
||||
fields=fields)
|
||||
return self.document_type.to_mongo(value, **kwargs)
|
||||
|
||||
def validate(self, value, clean=True):
|
||||
"""Make sure that the document instance is an instance of the
|
||||
@@ -600,11 +599,11 @@ class GenericEmbeddedDocumentField(BaseField):
|
||||
|
||||
value.validate(clean=clean)
|
||||
|
||||
def to_mongo(self, document, use_db_field=True):
|
||||
def to_mongo(self, document, **kwargs):
|
||||
if document is None:
|
||||
return None
|
||||
|
||||
data = document.to_mongo(use_db_field)
|
||||
data = document.to_mongo(**kwargs)
|
||||
if '_cls' not in data:
|
||||
data['_cls'] = document._class_name
|
||||
return data
|
||||
@@ -616,7 +615,7 @@ class DynamicField(BaseField):
|
||||
|
||||
Used by :class:`~mongoengine.DynamicDocument` to handle dynamic data"""
|
||||
|
||||
def to_mongo(self, value):
|
||||
def to_mongo(self, value, **kwargs):
|
||||
"""Convert a Python type to a MongoDB compatible type.
|
||||
"""
|
||||
|
||||
@@ -625,7 +624,7 @@ class DynamicField(BaseField):
|
||||
|
||||
if hasattr(value, 'to_mongo'):
|
||||
cls = value.__class__
|
||||
val = value.to_mongo()
|
||||
val = value.to_mongo(**kwargs)
|
||||
# If we its a document thats not inherited add _cls
|
||||
if isinstance(value, Document):
|
||||
val = {"_ref": value.to_dbref(), "_cls": cls.__name__}
|
||||
@@ -643,7 +642,7 @@ class DynamicField(BaseField):
|
||||
|
||||
data = {}
|
||||
for k, v in value.iteritems():
|
||||
data[k] = self.to_mongo(v)
|
||||
data[k] = self.to_mongo(v, **kwargs)
|
||||
|
||||
value = data
|
||||
if is_list: # Convert back to a list
|
||||
@@ -755,8 +754,8 @@ class SortedListField(ListField):
|
||||
self._order_reverse = kwargs.pop('reverse')
|
||||
super(SortedListField, self).__init__(field, **kwargs)
|
||||
|
||||
def to_mongo(self, value):
|
||||
value = super(SortedListField, self).to_mongo(value)
|
||||
def to_mongo(self, value, **kwargs):
|
||||
value = super(SortedListField, self).to_mongo(value, **kwargs)
|
||||
if self._ordering is not None:
|
||||
return sorted(value, key=itemgetter(self._ordering),
|
||||
reverse=self._order_reverse)
|
||||
@@ -942,7 +941,7 @@ class ReferenceField(BaseField):
|
||||
|
||||
return super(ReferenceField, self).__get__(instance, owner)
|
||||
|
||||
def to_mongo(self, document):
|
||||
def to_mongo(self, document, **kwargs):
|
||||
if isinstance(document, DBRef):
|
||||
if not self.dbref:
|
||||
return document.id
|
||||
@@ -965,7 +964,7 @@ class ReferenceField(BaseField):
|
||||
id_field_name = cls._meta['id_field']
|
||||
id_field = cls._fields[id_field_name]
|
||||
|
||||
id_ = id_field.to_mongo(id_)
|
||||
id_ = id_field.to_mongo(id_, **kwargs)
|
||||
if self.document_type._meta.get('abstract'):
|
||||
collection = cls._get_collection_name()
|
||||
return DBRef(collection, id_, cls=cls._class_name)
|
||||
@@ -1088,7 +1087,7 @@ class CachedReferenceField(BaseField):
|
||||
|
||||
return super(CachedReferenceField, self).__get__(instance, owner)
|
||||
|
||||
def to_mongo(self, document):
|
||||
def to_mongo(self, document, **kwargs):
|
||||
id_field_name = self.document_type._meta['id_field']
|
||||
id_field = self.document_type._fields[id_field_name]
|
||||
|
||||
@@ -1103,10 +1102,11 @@ class CachedReferenceField(BaseField):
|
||||
# TODO: should raise here or will fail next statement
|
||||
|
||||
value = SON((
|
||||
("_id", id_field.to_mongo(id_)),
|
||||
("_id", id_field.to_mongo(id_, **kwargs)),
|
||||
))
|
||||
|
||||
value.update(dict(document.to_mongo(fields=self.fields)))
|
||||
kwargs['fields'] = self.fields
|
||||
value.update(dict(document.to_mongo(**kwargs)))
|
||||
return value
|
||||
|
||||
def prepare_query_value(self, op, value):
|
||||
@@ -1222,7 +1222,7 @@ class GenericReferenceField(BaseField):
|
||||
doc = doc_cls._from_son(doc)
|
||||
return doc
|
||||
|
||||
def to_mongo(self, document, use_db_field=True):
|
||||
def to_mongo(self, document, **kwargs):
|
||||
if document is None:
|
||||
return None
|
||||
|
||||
@@ -1241,7 +1241,7 @@ class GenericReferenceField(BaseField):
|
||||
else:
|
||||
id_ = document
|
||||
|
||||
id_ = id_field.to_mongo(id_)
|
||||
id_ = id_field.to_mongo(id_, **kwargs)
|
||||
collection = document._get_collection_name()
|
||||
ref = DBRef(collection, id_)
|
||||
return SON((
|
||||
@@ -1270,7 +1270,7 @@ class BinaryField(BaseField):
|
||||
value = bin_type(value)
|
||||
return super(BinaryField, self).__set__(instance, value)
|
||||
|
||||
def to_mongo(self, value):
|
||||
def to_mongo(self, value, **kwargs):
|
||||
return Binary(value)
|
||||
|
||||
def validate(self, value):
|
||||
@@ -1495,7 +1495,7 @@ class FileField(BaseField):
|
||||
db_alias=db_alias,
|
||||
collection_name=collection_name)
|
||||
|
||||
def to_mongo(self, value):
|
||||
def to_mongo(self, value, **kwargs):
|
||||
# Store the GridFS file id in MongoDB
|
||||
if isinstance(value, self.proxy_class) and value.grid_id is not None:
|
||||
return value.grid_id
|
||||
@@ -1845,7 +1845,7 @@ class UUIDField(BaseField):
|
||||
return original_value
|
||||
return value
|
||||
|
||||
def to_mongo(self, value):
|
||||
def to_mongo(self, value, **kwargs):
|
||||
if not self._binary:
|
||||
return unicode(value)
|
||||
elif isinstance(value, basestring):
|
||||
|
Reference in New Issue
Block a user