-
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
fix: Query by permission in system resource management #3936
Conversation
--bug=1060785 --user=张展玮 【资源授权】-通过权限查询资源无效 https://www.tapd.cn/62980211/s/1761243
|
Adding the "do-not-merge/release-note-label-needed" label because no release-note block was detected, please follow our release note process to remove it. DetailsInstructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository. |
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
| 'permission': request.query_params.getlist("permission[]") | ||
| })) | ||
|
|
||
| @extend_schema( |
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 includes several potential issues and areas for optimization:
-
Duplicate Code: There are multiple instances where the
UserResourcePermissionSerializeris used with similar logic but with different query parameter names and handling (request.query_params.get()vsrequest.query_params.getlist()) onpermission. This could be consolidated into a single method to reduce redundancy. -
Consistent Query Parameter Handling: The use of
'permission'for both single-value and list values is inconsistent. When using'permission', it should typically expect a comma-separated string if you intend it as a list-like input (e.g.,'read,write,execute'). Ensure that this usage aligns across all calls to the serializer. -
Variable Names: The variable
current_pagewas defined but not subsequently used within the function scope. Since it's only referenced outside the method, consider removing or ensuring its reuse. -
APIView Consistency: While API views like
WorkspaceResourcePermissionsListViewandWorkspaceResourceUserPermissionViewextend fromAPIView, they have separate paths and handlers. Consider combining them under a common base class (e.g.,BaseWorkplaceResourceView) to share common behavior and improve maintainability. -
Extend Schema Usage: In some cases, extending schema metadata might not add significant value unless detailed documentation or extra validation rules are required. If no additional schema information needs to be added, remove these decorators.
-
Type Annotations: It would be beneficial to add type annotations to the parameters and return types in each function to enhance readability and catch errors at runtime.
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.
| name="permission[]", | ||
| description="权限", | ||
| type=OpenApiTypes.STR, | ||
| location='query', |
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:
def get_parameters():
return [
OpenApiParameter(
name="permission[]", # This is incorrect. Parameter names should not contain square brackets []
description="权限",
type=OpenApiTypes.STR,
location='query'
)
]Issue:
The name attribute 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:
- Use Proper Naming Convention: Ensure that all names, including query parameters, follow Python's conventional snake_case naming style to make them more readable and maintainable.
- Documentation Consistency: Make sure that the documentation (description) matches the actual usage of the parameter if it differs from its internal structure or behavior.
Corrected Code:
def get_parameters():
return [
OpenApiParameter(
name="_permission",
description="权限",
type=OpenApiTypes.SLICE_OF_STRINGS, # Adjust types based on intended functionality
location='query'
)
]Note: Depending on your specific needs, you might want to use str | list[str] as the type.
fix: Query by permission in system resource management --bug=1060785 --user=张展玮 【资源授权】-通过权限查询资源无效 https://www.tapd.cn/62980211/s/1761243