-
Notifications
You must be signed in to change notification settings - Fork 2.1k
feat: add ToolTreeView and ToolTreeReadAPI for retrieving tools by workspace and module #2928
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,7 @@ | |
from django.utils.translation import gettext_lazy as _ | ||
from rest_framework import serializers | ||
|
||
from tools.models import Tool, ToolScope | ||
from tools.models import Tool, ToolScope, ToolModule | ||
|
||
|
||
class ToolModelSerializer(serializers.ModelSerializer): | ||
|
@@ -63,7 +63,7 @@ class ToolCreateRequest(serializers.Serializer): | |
class ToolSerializer(serializers.Serializer): | ||
class Create(serializers.Serializer): | ||
user_id = serializers.UUIDField(required=True, label=_('user id')) | ||
workspace_id = serializers.UUIDField(required=True, label=_('workspace id')) | ||
workspace_id = serializers.CharField(required=True, label=_('workspace id')) | ||
|
||
def insert(self, instance, with_valid=True): | ||
if with_valid: | ||
|
@@ -77,6 +77,7 @@ def insert(self, instance, with_valid=True): | |
input_field_list=instance.get('input_field_list', []), | ||
init_field_list=instance.get('init_field_list', []), | ||
scope=ToolScope.WORKSPACE, | ||
module_id=instance.get('module_id', 'root'), | ||
is_active=False) | ||
tool.save() | ||
return ToolModelSerializer(tool).data | ||
|
@@ -109,3 +110,20 @@ def one(self): | |
self.is_valid(raise_exception=True) | ||
tool = QuerySet(Tool).filter(id=self.data.get('id')).first() | ||
return ToolModelSerializer(tool).data | ||
|
||
|
||
class ToolTreeSerializer(serializers.Serializer): | ||
workspace_id = serializers.CharField(required=True, label=_('workspace id')) | ||
|
||
def get_tools(self, module_id): | ||
self.is_valid(raise_exception=True) | ||
if not module_id: | ||
module_id = 'root' | ||
root = ToolModule.objects.filter(id=module_id).first() | ||
if not root: | ||
raise serializers.ValidationError(_('Module not found')) | ||
# 使用MPTT的get_family()方法获取所有相关节点 | ||
all_modules = root.get_descendants(include_self=True) | ||
|
||
tools = QuerySet(Tool).filter(workspace_id=self.data.get('workspace_id'), module_id__in=all_modules) | ||
return ToolModelSerializer(tools, many=True).data | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Review of Code
Overall, the changes address the introduction of the new |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,8 +7,8 @@ | |
from common.auth.authentication import has_permissions | ||
from common.constants.permission_constants import PermissionConstants | ||
from common.result import result | ||
from tools.api.tool import ToolCreateAPI, ToolEditAPI, ToolReadAPI, ToolDeleteAPI | ||
from tools.serializers.tool import ToolSerializer | ||
from tools.api.tool import ToolCreateAPI, ToolEditAPI, ToolReadAPI, ToolDeleteAPI, ToolTreeReadAPI | ||
from tools.serializers.tool import ToolSerializer, ToolTreeSerializer | ||
|
||
|
||
class ToolView(APIView): | ||
|
@@ -45,8 +45,8 @@ def put(self, request: Request, workspace_id: str, tool_id: str): | |
).edit(request.data)) | ||
|
||
@extend_schema(methods=['GET'], | ||
description=_('Update tool'), | ||
operation_id=_('Update tool'), | ||
description=_('Get tool'), | ||
operation_id=_('Get tool'), | ||
parameters=ToolReadAPI.get_parameters(), | ||
responses=ToolReadAPI.get_response(), | ||
tags=[_('Tool')]) | ||
|
@@ -66,3 +66,19 @@ def delete(self, request: Request, workspace_id: str, tool_id: str): | |
return result.success(ToolSerializer.Operate( | ||
data={'id': tool_id, 'workspace_id': workspace_id} | ||
).delete()) | ||
|
||
|
||
class ToolTreeView(APIView): | ||
authentication_classes = [TokenAuth] | ||
|
||
@extend_schema(methods=['GET'], | ||
description=_('Get tool'), | ||
operation_id=_('Get tool'), | ||
parameters=ToolTreeReadAPI.get_parameters(), | ||
responses=ToolTreeReadAPI.get_response(), | ||
tags=[_('Tool')]) | ||
@has_permissions(PermissionConstants.TOOL_READ.get_workspace_permission()) | ||
def get(self, request: Request, workspace_id: str): | ||
return result.success(ToolTreeSerializer( | ||
data={'workspace_id': workspace_id} | ||
).get_tools(request.query_params.get('module_id'))) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Code Review
Optimizations Suggestions:
These points can improve robustness, performance, and user experience by making your API more reliable, efficient, and flexible. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The provided code snippet has several small improvements that can be made:
Docstring Fix: Add a docstring to the
get_parameters
method ofToolTreeReadAPI
, detailing what it does.Indentation Consistency: Ensure consistent indentation throughout the code, especially after defining functions and adding parameters inside them.
Here is the revised version with these improvements:
These changes improve readability, clarity, and maintainability of the code while addressing minimal issues.