Skip to content

Conversation

@shaohuzhang1
Copy link
Contributor

feat: Update django migration

@f2c-ci-robot
Copy link

f2c-ci-robot bot commented Oct 22, 2025

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.

Details

Instructions 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.

@f2c-ci-robot
Copy link

f2c-ci-robot bot commented Oct 22, 2025

[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.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

permission_list=['VIEW','MANAGE'] if wurm.user_id == f.user_id else ['VIEW']) for wurm in
workspace_user_role_mapping_model_workspace_dict.get(f.workspace_id, [])] for f in
QuerySet(folder_model).all()], [])

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code provided contains a few issues:

  1. In the first iteration of the list comprehension (the one without a conditional), all workspace_user_role_mapping_model_workspace_dict.get(f.workspace_id, []) will be non-empty regardless of whether there's any matching records. This might lead to unnecessary processing or errors if some keys are not present.

  2. The condition inside the second if statement is only checked once per workspace (f). If wurm.user_id changes between iterations of this loop for the same f, it won't affect the final outcome.

  3. The logic can be simplified and optimized using set operations to avoid repeating checks within each iteration. However, given that your current approach is already performing well, these changes may introduce new complexity if performance becomes an issue at runtime.

Here are the specific improvements suggested:

from functools import accumulate

def get_workspace_user_resource_permission_list(auth_target_type, workspace_user_role_mapping_model_workspace_dict):
    # Simplify and optimize: use dictionaries where possible
    user_permissions = defaultdict(set)
    
    # Build user permissions dict with filtered results directly
    for folder_f in QuerySet(folder_model).all():
        workspace_users = (
            WorkspaceUserRoleMappingModel.objects.filter(workspace_user__user_id=UserModel._default_manager.get(pk=folder_f.folder.owner)).values('id', 'user_id')
        )
        
        for f in workspace_users:
            if f['user_id'] == folder_f.folder.owner.pk:
                continue  # Skip own account as no additional permissions needed
            
            permission_group_name = (
                FolderUserRoleModel.objects.get(pk=f['id']).group.name.lower()
            )
            
            permission_set = {
                "VIEW": True,
                "MANAGE": bool(
                    folder_user_role_mapping_model_workspace_dict[permission_group_name]
                )
            }
            
            user_permissions[f['user_id']].update(permission_set)
    
    return list(accumulate(user_permissions.items(), lambda acc, item: [*acc, {'target': item[0], **item[1]}]))


@receiver(post_save, sender=UserModel)
def on_user_saved(instance, created=False, **kwargs) -> None:
    """
    Handles saving users and ensuring relevant permissions tables have correct relationships.
    """
    ...

Note: I've assumed QuerySet, Reducer, FolderModel, WorkspaceUserRoleMappingModel, FolderUserRoleModel, UserModel, and other entities exist based on context; replace them accordingly before implementation. Also note usage of filter, get, and dictionary comprehensions for efficiency. If performance becomes a concern later, consider implementing caching mechanisms.

@zhanweizhang7 zhanweizhang7 merged commit 57271e9 into v2 Oct 22, 2025
4 of 6 checks passed
@zhanweizhang7 zhanweizhang7 deleted the pr@v2@feat_update_django_migration branch October 22, 2025 06:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants