Created
February 25, 2021 12:47
-
-
Save silenius/df2776e5e179053b56726abde43edc48 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(...) | |
tabs = dbsession.query( | |
Folder | |
).join( | |
root, root.c.id == Folder.id | |
).add_columns( | |
root.c.level.label('level') | |
).order_by( | |
root.c.container_id, root.c.level.desc(), root.c.weight.desc() | |
).all() | |
per_level = itertools.groupby(reversed(tabs), lambda x: x.level) | |
def _merge(a, b): | |
if not b: | |
b.extend(a) | |
else: | |
while a: | |
a_ = a.pop() | |
visited = [] | |
for b_ in b: | |
if a_['folder'] is b_['folder'].parent: | |
a_['children'].append(b_) | |
visited.append(b_) | |
for v in visited: | |
b.remove(v) | |
b.append(a_) | |
sorted_tabs = [] | |
for level, level_tabs in per_level: | |
level_tabs = [{'folder': f.Folder, 'children': []} for f in level_tabs] | |
_merge(level_tabs, sorted_tabs) | |
return sorted_tabs |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment