-
Notifications
You must be signed in to change notification settings - Fork 2.6k
fix: Query by permission in system resource management #3936
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 |
|---|---|---|
|
|
@@ -54,7 +54,7 @@ def get(self, request: Request, workspace_id: str, user_id: str, resource: str): | |
| return result.success(UserResourcePermissionSerializer( | ||
| data={'workspace_id': workspace_id, 'user_id': user_id, 'auth_target_type': resource} | ||
| ).list({'name': request.query_params.get('name'), | ||
| 'permission': request.query_params.getlist('permission')}, request.user)) | ||
| 'permission': request.query_params.getlist('permission[]')}, request.user)) | ||
|
|
||
| @extend_schema( | ||
| methods=['PUT'], | ||
|
|
@@ -99,7 +99,7 @@ def get(self, request: Request, workspace_id: str, user_id: str, resource: str, | |
| return result.success(UserResourcePermissionSerializer( | ||
| data={'workspace_id': workspace_id, 'user_id': user_id, 'auth_target_type': resource} | ||
| ).page({'name': request.query_params.get('name'), | ||
| 'permission': request.query_params.getlist('permission')}, current_page, page_size, request.user)) | ||
| 'permission': request.query_params.getlist('permission[]')}, current_page, page_size, request.user)) | ||
|
|
||
|
|
||
| class WorkspaceResourceUserPermissionView(APIView): | ||
|
|
@@ -132,7 +132,7 @@ def get(self, request: Request, workspace_id: str, target: str, resource: str): | |
| data={'workspace_id': workspace_id, "target": target, 'auth_target_type': resource, | ||
| }).list( | ||
| {'username': request.query_params.get("username"), 'nick_name': request.query_params.get("nick_name"), | ||
| 'permission': request.query_params.getlist("permission") | ||
| 'permission': request.query_params.getlist("permission[]") | ||
| })) | ||
|
|
||
| @extend_schema( | ||
|
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 snippet includes several potential issues and areas for optimization:
Here’s an optimized version after addressing these points: from drf_yasg.utils import swagger_auto_schema
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from rest_framework.pagination import PageNumberPagination
class UserResourcePermissionListApiHandler:
@swagger_auto_schema(
operations_summary='Get user-resource-permission list',
responses={200: UserResourcePermissionSerializer(many=True)}
)
def get(self, request: Request, workspace_id: str, user_id: str, resource: str) -> Response:
queryset = UserResourcePermission.objects.filter(workspace=workspace_id, user=user_id)
permission_querystring_value = request.query_params.get('permission', '').split(',')
filtered_queryset = queryset.filter(permission__in=permission_querystring_value)
paginator = PageNumberPagination()
paginated_response = paginator.paginate_queryset(filtered_queryset, request)
return paginator.get_paginated_response(UserResourcePermissionSerializer(paginated_response).data)
# Example usage:
# class WorkspaceResourcePermissionsListView(BaseWorkplaceResourceViewMixin, BaseWorkplaceResourcesAccessControlView):
# ...In summary, focusing on clean code by reducing duplication, consistent parameter management, and optimizing variable usage can significantly enhance the robustness and ease of maintenance of codebases. |
||
|
|
||
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 a small issue with the parameter definition:
Issue:
The
nameattribute should use snake_case instead of camelCase and should be prefixed with an underscore to adhere to typical naming conventions for query parameters.Optimization Suggestions:
Corrected Code:
Note: Depending on your specific needs, you might want to use
str | list[str]as thetype.