Chore: refactor permission checks to use queryset.exists()

This commit is contained in:
shamoon 2025-12-08 15:53:10 -08:00
parent 3f47900f06
commit 8efc998687
No known key found for this signature in database

View File

@ -85,12 +85,12 @@ def set_permissions_for_object(permissions: list[str], object, *, merge: bool =
if not merge
else User.objects.none()
)
if len(users_to_add) > 0 and len(users_to_remove) > 0:
if users_to_add.exists() and users_to_remove.exists():
users_to_remove = users_to_remove.exclude(id__in=users_to_add)
if len(users_to_remove) > 0:
if users_to_remove.exists():
for user in users_to_remove:
remove_perm(permission, user, object)
if len(users_to_add) > 0:
if users_to_add.exists():
for user in users_to_add:
assign_perm(permission, user, object)
if action == "change":
@ -111,12 +111,12 @@ def set_permissions_for_object(permissions: list[str], object, *, merge: bool =
if not merge
else Group.objects.none()
)
if len(groups_to_add) > 0 and len(groups_to_remove) > 0:
if groups_to_add.exists() and groups_to_remove.exists():
groups_to_remove = groups_to_remove.exclude(id__in=groups_to_add)
if len(groups_to_remove) > 0:
if groups_to_remove.exists():
for group in groups_to_remove:
remove_perm(permission, group, object)
if len(groups_to_add) > 0:
if groups_to_add.exists():
for group in groups_to_add:
assign_perm(permission, group, object)
if action == "change":