Skip to content

Commit bb4b35b

Browse files
committed
feat: enhance module depth validation with current depth tracking
1 parent f7a4aca commit bb4b35b

File tree

1 file changed

+22
-3
lines changed

1 file changed

+22
-3
lines changed

apps/modules/serializers/module.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def get_module_type(source):
2828
MODULE_DEPTH = 3 # Module 不能超过3层
2929

3030

31-
def check_depth(source, parent_id):
31+
def check_depth(source, parent_id, current_depth=0):
3232
# Module 不能超过3层
3333
Module = get_module_type(source)
3434

@@ -46,10 +46,28 @@ def check_depth(source, parent_id):
4646
current_parent_id = parent_node.parent_id
4747

4848
# 验证层级深度
49-
if depth > MODULE_DEPTH:
49+
if depth + current_depth > MODULE_DEPTH:
5050
raise serializers.ValidationError(_('Module depth cannot exceed 3 levels'))
5151

5252

53+
def get_max_depth(current_node):
54+
if not current_node:
55+
return 0
56+
57+
# 获取所有后代节点
58+
descendants = current_node.get_descendants()
59+
60+
if not descendants.exists():
61+
return 0
62+
63+
# 获取最大深度
64+
max_level = descendants.order_by('-level').first().level
65+
current_level = current_node.level
66+
max_depth = max_level - current_level
67+
68+
return max_depth
69+
70+
5371
class ModuleSerializer(serializers.Serializer):
5472
id = serializers.CharField(required=True, label=_('module id'))
5573
name = serializers.CharField(required=True, label=_('module name'))
@@ -110,7 +128,8 @@ def edit(self, instance):
110128
parent_id = instance.get('parent_id')
111129
if parent_id is not None and current_id != 'root':
112130
# Module 不能超过3层
113-
check_depth(self.data.get('source'), parent_id)
131+
current_depth = get_max_depth(current_node)
132+
check_depth(self.data.get('source'), parent_id, current_depth)
114133
parent = Module.objects.get(id=parent_id)
115134
current_node.move_to(parent)
116135

0 commit comments

Comments
 (0)