|
6 | 6 | import uuid_utils.compat as uuid
|
7 | 7 | from django.core import validators
|
8 | 8 | from django.db import transaction
|
9 |
| -from django.db.models import QuerySet |
| 9 | +from django.db.models import QuerySet, Q |
10 | 10 | from django.http import HttpResponse
|
11 | 11 | from django.utils.translation import gettext_lazy as _
|
12 | 12 | from rest_framework import serializers, status
|
@@ -57,7 +57,7 @@ class ToolModelSerializer(serializers.ModelSerializer):
|
57 | 57 | class Meta:
|
58 | 58 | model = Tool
|
59 | 59 | fields = ['id', 'name', 'icon', 'desc', 'code', 'input_field_list', 'init_field_list', 'init_params',
|
60 |
| - 'scope', 'is_active', 'user_id', 'template_id', 'workspace_id', 'module_id', |
| 60 | + 'scope', 'is_active', 'user_id', 'template_id', 'workspace_id', 'module_id', 'tool_type', |
61 | 61 | 'create_time', 'update_time']
|
62 | 62 |
|
63 | 63 |
|
@@ -291,8 +291,35 @@ def get_tools(self, module_id):
|
291 | 291 | root = ToolModule.objects.filter(id=module_id).first()
|
292 | 292 | if not root:
|
293 | 293 | raise serializers.ValidationError(_('Module not found'))
|
294 |
| - # 使用MPTT的get_family()方法获取所有相关节点 |
| 294 | + # 使用MPTT的get_descendants()方法获取所有相关节点 |
295 | 295 | all_modules = root.get_descendants(include_self=True)
|
296 | 296 |
|
297 | 297 | tools = QuerySet(Tool).filter(workspace_id=self.data.get('workspace_id'), module_id__in=all_modules)
|
298 | 298 | return ToolModelSerializer(tools, many=True).data
|
| 299 | + |
| 300 | + class Query(serializers.Serializer): |
| 301 | + workspace_id = serializers.CharField(required=True, label=_('workspace id')) |
| 302 | + module_id = serializers.CharField(required=True, label=_('module id')) |
| 303 | + name = serializers.CharField(required=False, allow_null=True, allow_blank=True, label=_('tool name')) |
| 304 | + tool_type = serializers.CharField(required=True, label=_('tool type')) |
| 305 | + |
| 306 | + def page(self, current_page: int, page_size: int): |
| 307 | + self.is_valid(raise_exception=True) |
| 308 | + |
| 309 | + module_id = self.data.get('module_id', 'root') |
| 310 | + root = ToolModule.objects.filter(id=module_id).first() |
| 311 | + if not root: |
| 312 | + raise serializers.ValidationError(_('Module not found')) |
| 313 | + # 使用MPTT的get_descendants()方法获取所有相关节点 |
| 314 | + all_modules = root.get_descendants(include_self=True) |
| 315 | + |
| 316 | + if self.data.get('name'): |
| 317 | + tools = QuerySet(Tool).filter(Q(workspace_id=self.data.get('workspace_id')) & |
| 318 | + Q(module_id__in=all_modules) & |
| 319 | + Q(tool_type=self.data.get('tool_type')) & |
| 320 | + Q(name__contains=self.data.get('name'))) |
| 321 | + else: |
| 322 | + tools = QuerySet(Tool).filter(Q(workspace_id=self.data.get('workspace_id')) & |
| 323 | + Q(module_id__in=all_modules) & |
| 324 | + Q(tool_type=self.data.get('tool_type'))) |
| 325 | + return ToolModelSerializer(tools, many=True).data |
0 commit comments