@@ -24,6 +24,9 @@ def get_module_type(source):
2424 return None
2525
2626
27+ MODULE_DEPTH = 3 # Module 不能超过3层
28+
29+
2730class ModuleSerializer (serializers .Serializer ):
2831 id = serializers .CharField (required = True , label = _ ('module id' ))
2932 name = serializers .CharField (required = True , label = _ ('module name' ))
@@ -47,6 +50,8 @@ def insert(self, instance, with_valid=True):
4750 Module = get_module_type (self .data .get ('source' ))
4851 if QuerySet (Module ).filter (name = name , workspace_id = workspace_id , parent_id = parent_id ).exists ():
4952 raise serializers .ValidationError (_ ('Module name already exists' ))
53+ # Module 不能超过3层
54+ self .check_depth (parent_id )
5055
5156 module = Module (
5257 id = uuid .uuid7 (),
@@ -58,6 +63,27 @@ def insert(self, instance, with_valid=True):
5863 module .save ()
5964 return ModuleSerializer (module ).data
6065
66+ def check_depth (self , parent_id ):
67+ # Module 不能超过3层
68+ Module = get_module_type (self .data .get ('source' ))
69+
70+ if parent_id != 'root' :
71+ # 计算当前层级
72+ depth = 1 # 当前要创建的节点算一层
73+ current_parent_id = parent_id
74+
75+ # 向上追溯父节点
76+ while current_parent_id != 'root' :
77+ depth += 1
78+ parent_node = QuerySet (Module ).filter (id = current_parent_id ).first ()
79+ if parent_node is None :
80+ break
81+ current_parent_id = parent_node .parent_id
82+
83+ # 验证层级深度
84+ if depth > MODULE_DEPTH :
85+ raise serializers .ValidationError (_ ('Module depth cannot exceed 3 levels' ))
86+
6187 class Operate (serializers .Serializer ):
6288 id = serializers .CharField (required = True , label = _ ('module id' ))
6389 workspace_id = serializers .CharField (required = True , allow_null = True , allow_blank = True , label = _ ('workspace id' ))
0 commit comments