Skip to content

Commit fdb7aeb

Browse files
committed
feat: implement module depth validation and refactor check_depth method
1 parent 3205ff2 commit fdb7aeb

File tree

1 file changed

+37
-24
lines changed

1 file changed

+37
-24
lines changed

apps/modules/serializers/module.py

Lines changed: 37 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# -*- coding: utf-8 -*-
22

33
import uuid_utils.compat as uuid
4+
from django.db import transaction
45
from django.db.models import QuerySet, Q
56
from django.utils.translation import gettext_lazy as _
67
from rest_framework import serializers
@@ -27,6 +28,28 @@ def get_module_type(source):
2728
MODULE_DEPTH = 3 # Module 不能超过3层
2829

2930

31+
def check_depth(source, parent_id):
32+
# Module 不能超过3层
33+
Module = get_module_type(source)
34+
35+
if parent_id != 'root':
36+
# 计算当前层级
37+
depth = 1 # 当前要创建的节点算一层
38+
current_parent_id = parent_id
39+
40+
# 向上追溯父节点
41+
while current_parent_id != 'root':
42+
depth += 1
43+
parent_node = QuerySet(Module).filter(id=current_parent_id).first()
44+
if parent_node is None:
45+
break
46+
current_parent_id = parent_node.parent_id
47+
48+
# 验证层级深度
49+
if depth > MODULE_DEPTH:
50+
raise serializers.ValidationError(_('Module depth cannot exceed 3 levels'))
51+
52+
3053
class ModuleSerializer(serializers.Serializer):
3154
id = serializers.CharField(required=True, label=_('module id'))
3255
name = serializers.CharField(required=True, label=_('module name'))
@@ -51,7 +74,7 @@ def insert(self, instance, with_valid=True):
5174
if QuerySet(Module).filter(name=name, workspace_id=workspace_id, parent_id=parent_id).exists():
5275
raise serializers.ValidationError(_('Module name already exists'))
5376
# Module 不能超过3层
54-
self.check_depth(parent_id)
77+
check_depth(self.data.get('source'), parent_id)
5578

5679
module = Module(
5780
id=uuid.uuid7(),
@@ -63,43 +86,33 @@ def insert(self, instance, with_valid=True):
6386
module.save()
6487
return ModuleSerializer(module).data
6588

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-
8789
class Operate(serializers.Serializer):
8890
id = serializers.CharField(required=True, label=_('module id'))
8991
workspace_id = serializers.CharField(required=True, allow_null=True, allow_blank=True, label=_('workspace id'))
9092
source = serializers.CharField(required=True, label=_('source'))
9193

94+
@transaction.atomic
9295
def edit(self, instance):
9396
self.is_valid(raise_exception=True)
9497
Module = get_module_type(self.data.get('source'))
95-
if not QuerySet(Module).filter(id=self.data.get('id')).exists():
98+
current_id = self.data.get('id')
99+
current_node = Module.objects.get(id=current_id)
100+
if current_node is None:
96101
raise serializers.ValidationError(_('Module does not exist'))
97102

98103
edit_field_list = ['name']
99104
edit_dict = {field: instance.get(field) for field in edit_field_list if (
100105
field in instance and instance.get(field) is not None)}
101106

102-
QuerySet(Module).filter(id=self.data.get('id')).update(**edit_dict)
107+
QuerySet(Module).filter(id=current_id).update(**edit_dict)
108+
109+
# 模块间的移动
110+
parent_id = instance.get('parent_id')
111+
if parent_id is not None and current_id != 'root':
112+
# Module 不能超过3层
113+
check_depth(self.data.get('source'), parent_id)
114+
parent = Module.objects.get(id=parent_id)
115+
current_node.move_to(parent)
103116

104117
return self.one()
105118

0 commit comments

Comments
 (0)