-
Notifications
You must be signed in to change notification settings - Fork 2.8k
fix: improve module tree query to use Q object for filtering by workspace_id #2923
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 |
|---|---|---|
| @@ -1,7 +1,7 @@ | ||
| # -*- coding: utf-8 -*- | ||
|
|
||
| import uuid_utils.compat as uuid | ||
| from django.db.models import QuerySet | ||
| from django.db.models import QuerySet, Q | ||
| from django.utils.translation import gettext_lazy as _ | ||
| from rest_framework import serializers | ||
|
|
||
|
|
@@ -83,6 +83,13 @@ def one(self): | |
| module = QuerySet(Module).filter(id=self.data.get('id')).first() | ||
| return ModuleSerializer(module).data | ||
|
|
||
| def delete(self): | ||
| self.is_valid(raise_exception=True) | ||
| if self.data.get('id') == 'root': | ||
| raise serializers.ValidationError(_('Cannot delete root module')) | ||
| Module = get_module_type(self.data.get('source')) | ||
| QuerySet(Module).filter(id=self.data.get('id')).delete() | ||
|
|
||
|
|
||
| class ModuleTreeSerializer(serializers.Serializer): | ||
| workspace_id = serializers.CharField(required=True, allow_null=True, allow_blank=True, label=_('workspace id')) | ||
|
|
@@ -91,6 +98,6 @@ class ModuleTreeSerializer(serializers.Serializer): | |
| def get_module_tree(self): | ||
| self.is_valid(raise_exception=True) | ||
| Module = get_module_type(self.data.get('source')) | ||
| nodes = Module.objects.filter(workspace_id=self.data.get('workspace_id')).get_cached_trees() | ||
| nodes = Module.objects.filter(Q(workspace_id=self.data.get('workspace_id'))).get_cached_trees() | ||
| serializer = ToolModuleTreeSerializer(nodes, many=True) | ||
| return serializer.data # 这是可序列化的字典 | ||
|
Contributor
Author
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. No major issues were found. Some minor suggestions for improvement:
In summary, this code does not have serious flaws but follows best practices for handling queries in Django ORM and making it easier to expand upon. |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,7 +7,7 @@ | |
| from common.auth.authentication import has_permissions | ||
| from common.constants.permission_constants import Permission, Group, Operate | ||
| from common.result import result | ||
| from modules.api.module import ModuleCreateAPI, ModuleEditAPI, ModuleReadAPI, ModuleTreeReadAPI | ||
| from modules.api.module import ModuleCreateAPI, ModuleEditAPI, ModuleReadAPI, ModuleTreeReadAPI, ModuleDeleteAPI | ||
| from modules.serializers.module import ModuleSerializer, ModuleTreeSerializer | ||
|
|
||
|
|
||
|
|
@@ -61,6 +61,18 @@ def get(self, request: Request, workspace_id: str, source: str, module_id: str): | |
| data={'id': module_id, 'workspace_id': workspace_id, 'source': source} | ||
| ).one()) | ||
|
|
||
| @extend_schema(methods=['DELETE'], | ||
| description=_('Delete module'), | ||
| operation_id=_('Delete module'), | ||
| parameters=ModuleDeleteAPI.get_parameters(), | ||
| tags=[_('Module')]) | ||
| @has_permissions(lambda r, kwargs: Permission(group=Group(kwargs.get('source')), operate=Operate.DELETE, | ||
| resource_path=f"/WORKSPACE/{kwargs.get('workspace_id')}")) | ||
| def delete(self, request: Request, workspace_id: str, source: str, module_id: str): | ||
| return result.success(ModuleSerializer.Operate( | ||
| data={'id': module_id, 'workspace_id': workspace_id, 'source': source} | ||
| ).delete()) | ||
|
|
||
|
|
||
| class ModuleTreeView(APIView): | ||
| authentication_classes = [TokenAuth] | ||
|
Contributor
Author
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. The provided code appears to be incomplete and contains a few irregularities and potential issues. Here's my analysis: Irregularities and Potential Issues
Optimization Suggestions
Here's a minimal example demonstrating how you might structure these changes: from django.contrib.auth.decorators import permission_required
from rest_framework.decorators import api_view
from rest_framework.response import Response
from rest_framework.status import HTTP_200_OK, HTTP_404_NOT_FOUND
# Assume ModuleSerializer and Module queryset are defined elsewhere
@permission_required('your_permission_name', raise_exception=True)
def delete_module(request, workspace_id, source, module_id):
try:
module = Module.objects.filter(workspace__uuid=workspace_id, type=source, uuid(module_id)).first()
if module:
module.delete()
return Response(status=HTTP_200_OK)
else:
return Response({'error': 'Module does not exist'}, status=HTTP_404_NOT_FOUND)
except Exception as e:
return Response({'error': str(e)}, status=500)This version assumes that:
|
||
|
|
||
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 appears to be incomplete and lacks some necessary components, such as imports and a main execution point. However, I can offer some general suggestions for improvement:
Key Suggestions:
importstatements at the beginning of your script.APIMixin.get_parameters()to handle different request types (e.g., POST, PUT, DELETE).@app.route) to define how you want to serve endpoints.This setup provides a basic structure that can be expanded with additional features as needed.