mirror of
https://github.com/fastapiadmin/FastapiAdmin.git
synced 2026-09-21 12:52:26 +00:00
feat: Add role code to system_role.json and update system_users.json creator_id to null
fix: Refactor initialize.py to handle nested children data during initialization feat: Implement tree structure traversal functions in common_util.py chore: Update requirements.txt to specify sqlalchemy-crud-plus version and add rich refactor: Change API endpoints in dept.ts and menu.ts to return tree structure feat: Add code field to role, dept, and menu interfaces in respective TypeScript files fix: Update dept and role Vue components to display and handle code field docs: Add comprehensive project documentation for FastAPI Vue3 Admin
This commit is contained in:
@@ -126,6 +126,62 @@ def get_child_recursion(
|
||||
get_child_recursion(child, id_map, ids)
|
||||
return ids
|
||||
|
||||
|
||||
def traversal_to_tree(nodes: list[dict[str, Any]]) -> list[dict[str, Any]]:
|
||||
"""
|
||||
通过遍历算法构造树形结构
|
||||
|
||||
:param nodes: 树节点列表
|
||||
:return:
|
||||
"""
|
||||
tree: list[dict[str, Any]] = []
|
||||
node_dict = {node['id']: node for node in nodes}
|
||||
|
||||
for node in nodes:
|
||||
# 确保每个节点都有children字段,即使没有子节点也设置为null
|
||||
if 'children' not in node:
|
||||
node['children'] = None
|
||||
|
||||
parent_id = node['parent_id']
|
||||
if parent_id is None:
|
||||
tree.append(node)
|
||||
else:
|
||||
parent_node = node_dict.get(parent_id)
|
||||
if parent_node is not None:
|
||||
if 'children' not in parent_node or parent_node['children'] is None:
|
||||
parent_node['children'] = []
|
||||
if node not in parent_node['children']:
|
||||
parent_node['children'].append(node)
|
||||
else:
|
||||
if node not in tree:
|
||||
tree.append(node)
|
||||
|
||||
# 确保所有节点都有children字段
|
||||
for node in tree:
|
||||
if 'children' not in node:
|
||||
node['children'] = None
|
||||
|
||||
return tree
|
||||
|
||||
|
||||
def recursive_to_tree(nodes: list[dict[str, Any]], *, parent_id: int | None = None) -> list[dict[str, Any]]:
|
||||
"""
|
||||
通过递归算法构造树形结构(性能影响较大)
|
||||
|
||||
:param nodes: 树节点列表
|
||||
:param parent_id: 父节点 ID,默认为 None 表示根节点
|
||||
:return:
|
||||
"""
|
||||
tree: list[dict[str, Any]] = []
|
||||
for node in nodes:
|
||||
if node['parent_id'] == parent_id:
|
||||
child_nodes = recursive_to_tree(nodes, parent_id=node['id'])
|
||||
if child_nodes:
|
||||
node['children'] = child_nodes
|
||||
tree.append(node)
|
||||
return tree
|
||||
|
||||
|
||||
def bytes2human(n: int, format_str: str = '%(value).1f%(symbol)s') -> str:
|
||||
"""
|
||||
字节数转人类可读格式
|
||||
|
||||
Reference in New Issue
Block a user