Merge pull request #474 from leonelcamara/master

Added an optional render argument to as_trees that if True causes it to ...
This commit is contained in:
mdipierro
2014-08-01 01:43:36 -05:00
2 changed files with 184 additions and 166 deletions
+17 -5
View File
@@ -7535,7 +7535,7 @@ class Row(object):
def as_dict(self, datetime_to_str=False, custom_types=None):
SERIALIZABLE_TYPES = [str, unicode, int, long, float, bool, list, dict]
if isinstance(custom_types, (list, tuple, set)):
SERIALIZABLE_TYPES += custom_types
SERIALIZABLE_TYPES += list(custom_types)
elif custom_types:
SERIALIZABLE_TYPES.append(custom_types)
d = dict(self)
@@ -11189,7 +11189,7 @@ class Rows(object):
items = [item.as_dict(datetime_to_str, custom_types) for item in self]
else:
items = [item for item in self]
self.compact = compact
self.compact = oc
return items
def as_dict(self,
@@ -11233,13 +11233,25 @@ class Rows(object):
else:
return dict([(key(r), r) for r in rows])
def as_trees(self, parent_name='parent_id', children_name='children'):
def as_trees(self, parent_name='parent_id', children_name='children', render=False):
"""
returns the data as list of trees.
:param parent_name: the name of the field to holding the reference to
the parent (default parent_id).
:param children_name: the name where the children of each row will be
stored as a list (default children).
:param render: whether we will render the fields using their represent
(default False) can be a list of fields to render or
True to render all.
"""
roots = []
drows = {}
for row in self:
rows = list(self.render(fields=None if render is True else render)) if render else self
for row in rows:
drows[row.id] = row
row[children_name] = []
for row in self:
for row in rows:
parent = row[parent_name]
if parent is None:
roots.append(row)