Skip to content
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

Allow batch assignment of multiple permissions to multiple users/groups for multiple objects #777

Open
wants to merge 14 commits into
base: devel
Choose a base branch
from

Conversation

JCourt1
Copy link

@JCourt1 JCourt1 commented May 9, 2022

Currently, the methods in the BaseObjectPermissionManager do not allow assigning multiple permissions for multiple users and for multiple objects. Likewise, the assign_perm function in shortcuts does not even allow assigning multiple permissions at once. The problem with this is that permission assignments are very slow, because they cannot be properly batched together.

This PR adds in methods to the object permission manager, and also adds two new shortcuts: bulk_assign_perms and bulk_remove_perms. In addition, I have also added a commit parameter, which allows the user of the shortcuts to go even further with the batching, and accumulate permissions so that they can all be persisted in one fell swoop. I have ensured that previous behaviour is left intact, and the new behaviour is well tested.

Having applied these changes in a work project which relies heavily on django guardian, I have seen a big decrease in the time taken for the entire test suite to complete (down to ~20 seconds from 3 minutes).

@JCourt1 JCourt1 changed the title Feature/multipermassign Allow assigning multiple permissions to multiple users/groups for multiple objects May 9, 2022
@JCourt1 JCourt1 changed the title Allow assigning multiple permissions to multiple users/groups for multiple objects Allow batch assignment of multiple permissions to multiple users/groups for multiple objects May 9, 2022
tests cover the different possible argument types for bulk_remove_perms

These shortcuts also have a "commit" parameter, meaning the user can eg.
bulk create certain permissions for a certain set of groups, other perms
for another set, then save them all at the same time.
without this change, there is an error on postgres whenever the object's
pk is not varchar. This is due to the fact that guardian's object
permission models have a varchar pk, but postgres doesn't automatically
convert eg. ints to varchars
@JCourt1 JCourt1 force-pushed the feature/multipermassign branch 2 times, most recently from cef55f6 to f0ca148 Compare May 25, 2022 08:29
This fixes a bug where empty Q object was causing a deepcopy in django/db/models/query_utils.py
This was causing a bug in certain versions of django when the query involves a model which
had a BinaryField inside it.

The bug is described here: noripyt/django-cachalot#115
@Laityned
Copy link

I like this future, could this be merged?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants