Flexible action level permissions for Django REST framework
Project description
drf-action-permissions
Release | CI | Coverage |
---|---|---|
Flexible ability to add action permissions on view level for Django REST framework. Permissions can be as complex or simple as you want. It can be a plain string or a function.
Requirements
- Python (3.6+)
- Django (1.11.x, 2.0+)
- Django REST Framework (3.7+)
Installation
$ pip install drf-common-exceptions
You cound define common permissions class for whole project:
REST_FRAMEWORK = {
...
"DEFAULT_PERMISSION_CLASSES": (
"drf_action_permissions.DjangoActionPermissions",
)
...
}
Or use it just for particular view or viewset in combination with others:
from rest_framework.permissions import IsAuthenticated
from drf_action_permissions import DjangoActionPermissions
class MyView(APIView):
permission_classes = (IsAuthenticated, DjangoActionPermissions)
perms_map_action = {
'retrieve': ['users.view_user'],
}
Usage examples
Permission as string template or plain string:
class PostViewSet(ModelViewSet):
permission_classes = (IsAuthenticated, DjangoActionPermissions)
perms_map_action = {
'likes': ['%(app_label)s.view_%(model_name)s_list',
'%(app_label)s.view_like_list'],
}
Permission as function with current object access:
def can_view_application(user, _view, obj):
"""Can view only archived applications."""
if obj.is_archived:
return user.has_perm('applications.view_archived_application')
return user.has_perm('applications.view_application')
class ApplicationView(ModelViewSet):
permission_classes = (IsAuthenticated, DjangoActionPermissions)
perms_map_action_obj = {
'retrieve': [can_view_application],
}
Development
Install poetry and requirements:
$ curl -sSL https://raw.githubusercontent.com/sdispater/poetry/master/get-poetry.py | python
$ python3 -m venv path/to/venv
$ source path/to/venv/bin/activate
$ poetry install
Run main commands:
$ make test
$ make watch
$ make clean
$ make lint
Publish to pypi by default patch version:
$ make publish
or any level you want:
$ make publish minor
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
Close
Hashes for drf-action-permissions-0.1.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 611ed66e3d0c4dd9a74ae1fd417b814dd2c4ab53b414b4e85b8043c97b7b6af0 |
|
MD5 | 1aa83afef3f4e7721dee71d497aef243 |
|
BLAKE2b-256 | 99067725fe53620bd46c38914900b6ac1c3d05a9a8f02289be0e190e8d79a3fb |
Close
Hashes for drf_action_permissions-0.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6cd46d0c8e25ae33eaecbfef2ccb89143bad6483e731edc003c9f090eb5eaaee |
|
MD5 | 32b608f03b79c88520ac3921c3760caa |
|
BLAKE2b-256 | ab99d5deb5e43f15ac301e0008bc5456934e558ce0e975504a7db1bd36f44a0b |