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

user group restriction #110

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion README.md
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ See [Django Docs on the subject](https://docs.djangoproject.com/en/dev/ref/contr

### Enable Mass Edit for specific models

By default, all models registered in the admin will get `Mass Edit` action.
By default, all models registered in the admin will get `Mass Edit` action, for all site users.

If you wish to disable this, add this to settings file:

Expand All @@ -72,6 +72,19 @@ class MyModelAdmin(MassEditMixin, admin.ModelAdmin):
...
```

### Restrict Mass actions enabled users

You can restrict Mass edit action to those users belonging to a specified group name.

``` python
MASSEDIT = {
'ADD_ACTION_GLOBALLY': False,
'MASS_USERS_GROUP': 'somegroup'
PetrDlouhy marked this conversation as resolved.
Show resolved Hide resolved
}
```

Note that user user restriction has effect only in conjunction with 'ADD_ACTION_GLOBALLY' variable set to false.

### Session-based URLs

Django-mass-edit will keep IDs for selected objects in URL, e.g:
Expand Down
17 changes: 13 additions & 4 deletions massadmin/massadmin.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,16 @@ def mass_change_view(
obj=obj)


class MassEditMixin:
actions = (
mass_change_selected,
)
class MassEditMixin(object):

def get_actions(self, request):
actions = super(MassEditMixin, self).get_actions(request)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Couldn't we the Python 3+ style super().get_actions(request)?

Copy link
Author

Choose a reason for hiding this comment

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

yess. done.

Copy link
Author

Choose a reason for hiding this comment

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

basically the suggested code keep the mass edit action hidden from admin action list. Core app code is untouched.
I don't know how to test this. Suggestions are wellcome.


if settings.MASS_USERS_GROUP and settings.MASS_USERS_GROUP in [g.name for g in request.user.groups.all()]:
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why do you use a group for this rather than checking permission with has_perm()?
It seems to me, that using permission would be much more flexible - the permission can be given to more than one groups.
You can also add permission to non-managed models so this functionality can work entirely without additional settings: https://stackoverflow.com/questions/13932774/how-can-i-use-django-permissions-without-defining-a-content-type-or-model

Copy link
Author

Choose a reason for hiding this comment

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

I implemented a new "can perform mass editing" permission with a new proxy model as from stackoverflow answer.
everything seem fine.

actions[mass_change_selected.__name__] = (
mass_change_selected,
mass_change_selected.__name__,
mass_change_selected.short_description
)

return actions
2 changes: 2 additions & 0 deletions massadmin/settings.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
_default_settings = {
'ADD_ACTION_GLOBALLY': True,
'SESSION_BASED_URL_THRESHOLD': 500,
'MASS_USERS_GROUP': None,
}

_settings = getattr(settings, 'MASSEDIT', _default_settings)
Expand All @@ -15,3 +16,4 @@ def _get_value(name):

ADD_ACTION_GLOBALLY = _get_value('ADD_ACTION_GLOBALLY')
SESSION_BASED_URL_THRESHOLD = _get_value('SESSION_BASED_URL_THRESHOLD')
MASS_USERS_GROUP = _get_value('MASS_USERS_GROUP')