From 2dd8ed35942831846bab31b61430617465e0ef06 Mon Sep 17 00:00:00 2001 From: Yoki Date: Mon, 21 Sep 2015 14:19:21 +0800 Subject: [PATCH 1/3] Add embedded document field. --- mongotor/orm/field.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/mongotor/orm/field.py b/mongotor/orm/field.py index ff57dc4..be06c46 100644 --- a/mongotor/orm/field.py +++ b/mongotor/orm/field.py @@ -225,3 +225,22 @@ def _validate(self, value): raise(TypeError("The Sha1 hash should be a 20byte hash value")) return value + + +class EmbeddedField(Field): + def __init__(self, *args, **kwargs): + super(EmbeddedField, self).__init__(*args, **kwargs) + + def _validate(self, value): + from mongotor.orm.collection import Collection + + # use 'issubclass' instead? + if value is not None and not isinstance(value, Collection): + raise(TypeError("The Embedded Field must be a subclass of Collection")) + return value + + def __get__(self, instance, owner): + embedded_document = instance._data.get(self.name) + if embedded_document is None: + return self.default() if callable(self.default) else self.default + return embedded_document.as_dict() \ No newline at end of file From d169593a9ae39ce2b9b93155e7c3d41283a91d85 Mon Sep 17 00:00:00 2001 From: Yoki Date: Mon, 21 Sep 2015 15:10:53 +0800 Subject: [PATCH 2/3] Fixed errors in EmbeddedField class --- mongotor/orm/field.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/mongotor/orm/field.py b/mongotor/orm/field.py index be06c46..cb84e1d 100644 --- a/mongotor/orm/field.py +++ b/mongotor/orm/field.py @@ -224,18 +224,18 @@ def _validate(self, value): except: raise(TypeError("The Sha1 hash should be a 20byte hash value")) - return value + return value class EmbeddedField(Field): - def __init__(self, *args, **kwargs): + def __init__(self, embedded_document=None, *args, **kwargs): super(EmbeddedField, self).__init__(*args, **kwargs) + self.embedded_class = embedded_document def _validate(self, value): from mongotor.orm.collection import Collection - # use 'issubclass' instead? - if value is not None and not isinstance(value, Collection): + if value is not None and not issubclass(self.embedded_class, Collection): raise(TypeError("The Embedded Field must be a subclass of Collection")) return value @@ -243,4 +243,4 @@ def __get__(self, instance, owner): embedded_document = instance._data.get(self.name) if embedded_document is None: return self.default() if callable(self.default) else self.default - return embedded_document.as_dict() \ No newline at end of file + return embedded_document.as_dict() From 22d143835392033f61606c938d17bcff11f8db68 Mon Sep 17 00:00:00 2001 From: Yoki Date: Thu, 5 Nov 2015 14:44:17 +0800 Subject: [PATCH 3/3] Update field.py Fix the EmbeddedField. --- mongotor/orm/field.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/mongotor/orm/field.py b/mongotor/orm/field.py index cb84e1d..208dcbe 100644 --- a/mongotor/orm/field.py +++ b/mongotor/orm/field.py @@ -233,14 +233,14 @@ def __init__(self, embedded_document=None, *args, **kwargs): self.embedded_class = embedded_document def _validate(self, value): - from mongotor.orm.collection import Collection - - if value is not None and not issubclass(self.embedded_class, Collection): - raise(TypeError("The Embedded Field must be a subclass of Collection")) + if value is not None: + if isinstance(value, dict): + embedded_obj = self.embedded_class() + for k, v in value.iteritems(): + setattr(embedded_obj, k, v) + return embedded_obj + elif isinstance(value, self.embedded_class): + return value.as_dict() + else: + raise (TypeError("The EmbeddedField value should be a instance of sub Collection class")) return value - - def __get__(self, instance, owner): - embedded_document = instance._data.get(self.name) - if embedded_document is None: - return self.default() if callable(self.default) else self.default - return embedded_document.as_dict()