Skip to content

Commit

Permalink
bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
aiscenblue committed Jun 27, 2019
1 parent 26ff0c7 commit 3c24068
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 41 deletions.
58 changes: 18 additions & 40 deletions mongoengine_serialize/serializer.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from mongoengine.base import BaseDocument
from mongoengine.base.datastructures import LazyReference
from bson.objectid import ObjectId
from datetime import datetime
from mongoengine.queryset.queryset import QuerySet
Expand Down Expand Up @@ -35,21 +34,17 @@ def __call__(self, collections):

def __serialize_type_of(self, collection):
if isinstance(collection, BaseDocument):
return collection.to_mongo(), collection
elif isinstance(collection, LazyReference):
raw = collection.fetch()
return raw.to_mongo(), raw
return self.__filter_serialize(collection.to_mongo(), collection)
elif isinstance(collection, JsonSerialized):
return collection, None
return collection
else:
return collection, None
return collection

def __serialize_collection(self, collections):
if isinstance(collections, (QuerySet, list, tuple)):
if isinstance(collections, QuerySet) or isinstance(collections, list):
return [self.__serialize_collection(_) for _ in collections]
else:
type_of_mongo, type_of_raw = self.__serialize_type_of(collections)
return self.__filter_serialize(type_of_mongo, type_of_raw)
return self.__serialize_type_of(collections)

@staticmethod
def __get_raw_name(name):
Expand All @@ -63,33 +58,15 @@ def __filter_serialize(self, collections, raw):
new_collection = dict()
for index, collection in enumerate(collections.items()):
key, value = collection
# if no raw passed. get the raw collection in class level attribute as default value
raw_collection = getattr(raw, self.__get_raw_name(key), self.__raw_collections)
if isinstance(value, (list, tuple)):
serialized_list = list()
for raw_col_item in raw_collection[key]:
type_off_mongo, type_off_raw = self.__serialize_type_of(raw_col_item)
filtered = self.__filter_serialize(type_off_mongo, type_off_raw)
serialized_list.append(filtered)
new_collection.update(self.__serialize(key, serialized_list, raw_collection))
elif isinstance(value, dict):
serialized_dict = self.__filter_serialize(collection, raw_collection)
new_collection.update(self.__serialize(key, serialized_dict, raw_collection))
elif isinstance(value, ObjectId):
if "id" in collection or "_id" in collection:
new_key, new_value = self.__attribute_serialize(key, value)
new_collection.update(self.__serialize(new_key, new_value, raw_collection))
else:
# TODO:: repeat for now. will do deep serialization in the future
new_key, new_value = self.__attribute_serialize(key, value)
new_collection.update(self.__serialize(new_key, new_value, raw_collection))
# print(self.__serialize_collection(raw_collection))
# new_collection.update(self.__serialize(new_key, new_value, type_off_raw))
raw_collection = getattr(raw, self.__get_raw_name(key), collection)
if isinstance(value, list) or isinstance(value, dict):
re_serialize = Serialize(raw_collection).jsonify()
new_collection.update(dict.fromkeys((key,), re_serialize))
else:
new_collection.update(self.__serialize(key, value, raw_collection))
return new_collection
else:
return self.__to_string_attribute(collections)
return collections

def __serialize(self, key, value, raw):
serialized_attribute = self.__attribute_serialize(key, value)
Expand All @@ -115,15 +92,15 @@ def __extract_list_to_exclude(self, items, *to_exclude):
_new_dict = dict()
for item in items.items():
k, v = item
if isinstance(v, (list, tuple)):
if isinstance(v, list):
serialized_list = self.__extract_list_to_exclude(v, *to_exclude)
_new_dict.update(dict.fromkeys((k,), serialized_list))
else:
_new_dict.update(dict.fromkeys((k,), v))
serialized = JsonSerialized(**_new_dict)
serialized.exclude_attributes(*to_exclude)
return serialized.to_json()
if isinstance(items, (list, tuple)):
if isinstance(items, list):
return [self.__extract_list_to_exclude(_, *to_exclude) for _ in items]
else:
return items
Expand Down Expand Up @@ -154,9 +131,10 @@ def __dict_jsonify(collection):
return collection

def jsonify(self):
if isinstance(self.__collections, (list, tuple)):
return [self.__dict_jsonify(_) for _ in self.__collections]
elif isinstance(self.__collections, dict):
return self.__dict_jsonify(self.__collections)
collections = self.__collections
if isinstance(collections, list):
return [self.__dict_jsonify(_) for _ in collections]
elif isinstance(collections, dict):
return self.__dict_jsonify(collections)
else:
return self.__dict_jsonify(self.__collections)
return self.__dict_jsonify(collections)
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

setup(
name='mongoengine-serialize',
version='1.2.7',
version='1.2.8',
description='Mongoengine serializer',
author='Jeffrey Marvin Forones',
author_email='[email protected]',
Expand Down

0 comments on commit 3c24068

Please sign in to comment.