-
Notifications
You must be signed in to change notification settings - Fork 2.6k
feat: Update django migration #4229
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 |
| 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()], []) | ||
|
|
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 code provided contains a few issues:
-
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. -
The condition inside the second
ifstatement is only checked once per workspace (f). Ifwurm.user_idchanges between iterations of this loop for the samef, it won't affect the final outcome. -
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.
feat: Update django migration