A Django extension to define and use Groups and Permissions from a central place, per app.
Project description
https://github.com/serioeseGmbH/serious-django-permissions
serious-django-permissions is a Django extension that makes it possible to define Permissions in each app, and Groups in a central place. It makes these Permissions import-able and checkable via user.has_perm:
from some_app.permissions import ChangeSomethingPermission def change_something(something, user): if not user.has_perm(ChangeSomethingPermission): raise PermissionDenied("You can't change this something.) else: return something.change()
Both permissions and groups can then be created in the database with a single manage.py call (see Quick start below).
Quick start
Install the package with pip:
pip install serious-django-permissions
Add “serious_django_permissions” to your INSTALLED_APPS setting like this:
INSTALLED_APPS = [ ... 'serious_django_permissions', ]
Add serious_django_permissions.permissions.PermissionModelBackend to your AUTHENTICATION_BACKENDS setting. This enables you to do permission checks like user.has_perm(SomePermission):
AUTHENTICATION_BACKENDS = [ ... 'serious_django_permissions.permissions.PermissionModelBackend', ]
In each app that should define a permission, import serious_django_permissions.permissions.Permission and create subclasses of it.
The name of your subclasses must end in Permission, and each subclass must define a description attribute. For instance, let’s say you have the file myapp/permissions.py:
from serious_django_permissions.permissions import Permission from .models import MyModel class MyPermission(Permission): model = 'MyModel' # should be a model inside myapp.models, or not defined for global permissions. description = 'A description for this permission'
Run python manage.py create_permissions to create all defined permissions on the database level.
New in version 0.7: Create all permissions (including groups) programmatically by calling setup_permissions():
from serious_django_permissions.helpers import setup_permissions setup_permissions()
If you’d like to use the Groups feature as well:
Create a file named something like some_app/groups.py inside one of your apps, or in the folder where your settings live. An example:
from serious_django_permissions.groups import Group from app_one.permissions import\ AppOnePermissionA, AppOnePermissionB from app_two.permissions import\ AppTwoPermission class GroupA(Group): permissions = [ AppOnePermissionA, AppTwoPermission ] class GroupB(Group): permissions = [ AppOnePermissionB, AppTwoPermission ]
Reference the defined groups file in your settings:
DEFAULT_GROUPS_MODULE = 'some_app.groups'
Run python manage.py create_groups to create all permissions and assign them to the groups.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for serious-django-permissions-0.15.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3a360d6087d279c14f7d20c1a99958236977d0dc7ffae775957bc2e944402ff8 |
|
MD5 | 8e941f45eefbf20efc5fff436dcb4129 |
|
BLAKE2b-256 | b6000de2ba5c64804d0bfb04160eee3ff020426116f8d09521cf3fab8c4a352c |
Hashes for serious_django_permissions-0.15-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 63978cef034a101e9f320c9f29e6f9c3a9cba8dc7a34d3b90017491d06bd178d |
|
MD5 | fb60fe4bb592ddd40bac476bb13573ca |
|
BLAKE2b-256 | 9481e2fe068dc0cb3f905646b3fa158c0aebbacf6224c93d785c057bc5108e3d |