Created
June 18, 2021 07:47
-
-
Save silenius/7d3043d64fddaa8474dcd062e23ced44 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
def get_children_containers(dbsession, folder_id, max_depth=None): | |
root = dbsession.query( | |
Folder, sql.literal(1, type_=Integer).label('level') | |
).filter( | |
sql.and_( | |
Folder.exclude_nav == False, | |
Folder.container_id == folder_id | |
) | |
).cte( | |
name='parents', recursive=True | |
) | |
filters = sql.and_( | |
Folder.exclude_nav == False, | |
Folder.container_id != None | |
) | |
if max_depth: | |
filters.append( | |
root.c.level <= max_depth | |
) | |
root = root.union_all( | |
dbsession.query(Folder, root.c.level + 1).join( | |
root, root.c.id == Folder.container_id | |
).filter(filters)) | |
tabs = dbsession.query( | |
Folder | |
).join( | |
root, root.c.id == Folder.id | |
).add_columns( | |
root.c.level.label('level') | |
).order_by( | |
root.c.level, root.c.container_id, root.c.weight.desc() | |
).all() | |
return FolderHierarchy(tabs) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment