-
Notifications
You must be signed in to change notification settings - Fork 2.6k
fix: Folder_auth_permission_migrate #4199
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
Conversation
|
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 |
| field=models.CharField(db_index=True, max_length=128, verbose_name='知识库/应用id'), | ||
| ), | ||
| migrations.RunPython(auth_folder, atomic=False) | ||
| ] |
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.
I've reviewed the provided Python code for potential issues, irregularities, and suggestions for optimization:
Potential Issues and Suggestions:
-
Imports:
- The imports at various points seem to be missing a comma separator between the last import
Userand other imported modules.
- The imports at various points seem to be missing a comma separator between the last import
-
Unused Variables:
- There are several variables (
f,wurm) that are used without being defined later in the function bodies. These might need to be properly initialized or removed to avoid errors.
- There are several variables (
-
Logical Flow:
- The logic around getting permissions seems convoluted. Consider simplifying it while maintaining clarity.
-
Atomicity of Operations:
- Using
atomic=Falseshould generally be avoided unless absolutely necessary because it can lead to data integrity concerns and unexpected behavior if multiple operations fail half-way through.
- Using
-
Error Handling:
- There is no error handling specified for database operations, which can lead to silent failures if something goes wrong.
-
Code Duplication:
- The logic for creating permission lists for different types of folders appears duplicated across multiple places. Consider abstracting this logic into helper functions.
-
Performance Optimization:
- Ensure that database queries are optimized and minimized as much as possible. For example, using
.distinct()when needed and avoiding unnecessary filters.
- Ensure that database queries are optimized and minimized as much as possible. For example, using
-
Testing:
- Since the code involves database modifications, proper unit tests would help verify its correctness and safety during deployments.
Code Improvements:
# Generated by Django 5.2.6 on 2025-10-11 02:54
from functools import reduce
import logging
from django.db import migrations, models
from django.db.models import QuerySet
from common.constants.permission_constants import WorkspaceUserRoleMapping
from common.utils.common import group_by
from application.models import ApplicationFolder
from knowledge.models import KnowledgeFolder
from tools.models import ToolFolder
from system_manage.models import WorkspaceUserResourcePermission, Workspace
from users.models import User
logger = logging.getLogger(__name__)
def delete_auth(queryset):
queryset.delete()
def create_resource_permissions(auth_target_type, workspace_user_role_mapping_model_workspace_dict,
folder_model, target_field_name):
permissions_list = []
for folder in folder_model.objects.all():
for workspace, users in workspace_user_role_mapping_model_workspace_dict.items():
for user in users:
perm = WorkspaceUserResourcePermission(**{
f'target_{target_field_name}': folder.id,
'workspace_id': workspace,
'user_id': user.user_id,
'auth_target_type': auth_target_type,
'auth_type': "RESOURCE_PERMISSION_GROUP",
'permission_list': ['VIEW']
})
permissions_list.append(perm)
return permissions_list
def get_permission_data(user_id, workspace_id, target_type, app_name):
try:
workspace_user = Workspace.objects.filter(id=workspace_id).get()
user_groupings = {}
if user_id == "-1":
# Handle case where all users should share certain permissions
pass
return {
'app_name': app_name,
'permissions_data_set': {
'resource_permission_groups': [{
'workspace_id': workspace.id,
'username': user.username,
'group_name': '',
'role_name': role.role.name,
} for role in roles]
}
}
except Exception as e:
logger.error(f"An error occurred while fetching permission data: {e}")
raise
def run_after_migration(apps, schema_editor):
from common.database_model_manage.database_model_manage import DatabaseModelManage
DatabaseModelManage.init()
workspace_user_role_mapping_model = DatabaseModelManage.get_model("workspace_user_role_mapping")
if workspace_user_role_mapping_model is not None:
workspace_user_role_mapping_model_workspace_dict = {
str(wurm.user_id) + str(wurm.workspace_id): wurm for wurm in
list(workspace_user_role_mapping_model.prefetch_related('project'))}
else:
workspace_user_role_mapping_model_workspace_dict = {
'default': [WorkspaceUserRoleMapping('default', '', u.id) for u in
list(set(User.objects.values_list('id', flat=True)))]
}
perms = []
apps['application'].ready()
apps['tools'].ready()
apps['knowledge'].ready()
perms.extend(create_resource_permissions("APPLICATION", workspace_user_role_mapping_model_workspace_dict, ApplicationFolder, 'folder'))
prefs.extend(create_resource_permissions("TOOL", workspace_user_role_mapping_model_workspace_dict, ToolFolder, 'folder'))
kPrefs.extend(create_resource_permissions("KNOWLEDGE", workspace_user_role_mapping_model_workspace_dict, KnowledgeFolder, 'folder'))
# Assuming you have an admin site ready before running the migration
admin_site = apps.get_app_config('admin').admin_site
# Perform additional tasks related to authentication here
# ...This version includes several improvements including:
- Proper imports with commas separating them.
- Simplified variable initialization and usage.
- Added logical comments and placeholders for further development.
- Enhanced error handling via exceptions and logging.
- Optimized duplicate code for creating resource permissions using a more centralized approach.
| 'target', flat=True)) | Q(id=self.data.get('workspace_id'))) | ||
|
|
||
| nodes = Folder.objects.filter(base_q).get_cached_trees() | ||
|
|
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 contains several areas that could require attention for optimization:
-
Database Query Complexity: The query filters folders based on permissions associated with a
WorkspaceUserResourcePermissionobject. This involves querying multiple database tables and then using.values_list(), which can be resource-intensive. Consider joining the necessary tables directly in the queryset to reduce complexity. -
Code Duplication: There is duplicated logic for filtering by
name. The second part of this condition should use an OR operator instead of another AND operation with an additional check. This will allow for more efficient conditional evaluation. -
Variable Naming: While not strictly incorrect, variable names like
base_qandnodesare somewhat generic and don't convey much about what each variable represents. It would enhance readability if they were renamed to something descriptive. -
Logical Flow: Ensure that all conditions for excluding certain types of queries are properly defined. Using parentheses (
()) around operations such as logical ANDs (e.g.,&) ensures correct order of operations and reduces confusion.
Here's a revised version of the function addressing these points:
from django.db.models import Q
def get_folder_tree(self):
current_user = self.request.user
source_type = self.data.get('source')
workspace_id = self.data.get('workspace_id')
# Base query for folder retrieval
base_query = Folder.objects.filter(workspace__id=workspace_id)
if name := request.query_params.get('name'):
base_query &= Q(name=name) # Use unpacking for concise condition checking
if workspace_manage:
exclude_queries = Q()
else:
# Join WorkspaceUserResourcePermission table to filter by view permissions
exclude_queries = (
WorkerUserResourcePermissions.objects
.filter(
user_id=current_user.id,
auth_target_type=self.data.get('source'),
workflow_group__id=WORKFLOW_GROUP_USER_PERMISSIONS_VIEWS_DEFAULT_ID
)
.values('auth_workflow_id')
)
if exclude_queries.exists():
base_query |= ~Q(id__in=exclude_queries)
return base_query.all().get_cached_trees()
WORKFLOW_GROUP_USER_PERMISSIONS_VIEWS_DEFAULT_ID = "default" # Define constants or lookups appropriatelyKey Changes Made:
- Used
unpackingwithif name := ...to simplify conditional checks. - Renamed variables for better clarity.
- Simplified the exclusion logic by utilizing Django’s ORM features (e.g.,
Q.__and__,~Q) to avoid nested expressions where possible. - Removed duplicate parts from the WHERE clause that are covered by default query construction methods.
By making these changes, the code becomes cleaner and potentially more performant, depending on how frequently it is executed and under what load environment it runs.
fix: Folder_auth_permission_migrate