Skip to content

Commit

Permalink
support queryset parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
aiscenblue committed Apr 12, 2018
1 parent 65c5a37 commit e29e65d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 12 deletions.
35 changes: 24 additions & 11 deletions mongoengine_serialize/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from mongoengine.base import BaseDocument
from bson.objectid import ObjectId
from datetime import datetime
from mongoengine.queryset.queryset import QuerySet


class JsonSerialized:
Expand Down Expand Up @@ -37,17 +38,23 @@ def __resurse_collections(self, collections):
if isinstance(collections, list):
col_list = list()
for collection in collections:
if isinstance(collection, list):
col_list.append(self.__serialize(collection))
else:
col_list.append(self.__serialize(collection))
col_list.append(self.__serialize(self.__resurse_collections(collection)))
return col_list
else:
return self.__serialize(collections)

@staticmethod
def __serialize_collection(collection):
if isinstance(collection, BaseDocument):
def __serialize_collection(self, collection):
if isinstance(collection, list):
li_array = []
for _ in collection:
li_array.append(self.__serialize_collection(_))
return li_array
elif isinstance(collection, QuerySet):
q_arr = []
for q in collection:
q_arr.append(self.__serialize_collection(q))
return q_arr
elif isinstance(collection, BaseDocument):
return collection.to_mongo()
elif isinstance(collection, JsonSerialized):
return collection
Expand All @@ -72,8 +79,12 @@ def __serialize(self, collection):
if isinstance(value, list):
val_list = list()
for index, _ in enumerate(value):
raw_collection = getattr(self.__raw_collections, key)
val_list.append(Serialize(raw_collection[index]).jsonify())
print(type(_))
if isinstance(_, ObjectId) or isinstance(_, dict) or isinstance(_, list):
raw_collection = getattr(self.__raw_collections, key)
val_list.append(Serialize(raw_collection[index]).jsonify())
else:
val_list.append(_)
setattr(json_serialized, key, val_list)
else:
serialized_attribute = self.__attribute_serialize(key, value)
Expand Down Expand Up @@ -123,9 +134,11 @@ def __dict_jsonify(self, collection):
def jsonify(self):
collections = self.__collections
if isinstance(collections, list):
list_col = list()
for collection in collections:
return self.__dict_jsonify(collection)
list_col.append(self.__dict_jsonify(collection))
return list_col
elif isinstance(collections, dict):
return self.__dict_jsonify(collections)
else:
return 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.1.3',
version='1.1.4',
description='Mongoengine serializer',
author='Jeffrey Marvin Forones',
author_email='aiscenblue@gmail.com',
Expand Down

0 comments on commit e29e65d

Please sign in to comment.