Use multiple permission classes in django class based views.
Project description
Django multiple permissions
Usage
- Install the package.
$ pip install multiple-permissions
- Add
multiple_permissionsto installed apps.
INSTALLED_APPS = [
...,
"multiple_permissions",
...,
]
- Add
PermissionMiddlewareto MIDDLEWARE list.
MIDDLEWARE = [
...,
"multiple_permissions.middlewares.PermissionMiddleware",
]
- Set
permission_classesattribute to view classes. - Set
exception_classattribute to view classes.
from django.views.generic import ListView, CreateView
from multiple_permissions.permissions import IsAuthenticated, IsSuperuser, IsManager
class PollsListView(ListView):
multiple_permissions = (IsAuthenticated,)
# if exception_class is missing will raise django.core.exceptions.PermissionDenied
...
class PollsCreateView(CreateView):
multiple_permissions = (IsSuperuser, IsManager)
exception_class = PermissionError
...
Creating new permissions
-
create new file in your apps named
permissions.pyand write the followng code -
note that your user should have
is_managerattribute or model field otherwise you'll catch AttributeError
from multiple_permissions.permissions import BasePermission
class IsManager(BasePermission):
def has_permission(self, request, view=None):
if request.user.is_authenticated and request.user.is_active and request.user.is_manager:
return True
return False
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file multiple_permissions-0.2.2.tar.gz.
File metadata
- Download URL: multiple_permissions-0.2.2.tar.gz
- Upload date:
- Size: 3.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.20.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.8.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cf0764509a26af9cbf71f71fe7bc05c5ae17cf5cee7b9bc78ef1d037dcc2841d
|
|
| MD5 |
97e34f3cd0f060f9eeb6bad935ae4b11
|
|
| BLAKE2b-256 |
de42e590efa31185fa9a33dfc8179d9da41520a5a8d1f39479e7d3b5f04fca21
|
File details
Details for the file multiple_permissions-0.2.2-py3-none-any.whl.
File metadata
- Download URL: multiple_permissions-0.2.2-py3-none-any.whl
- Upload date:
- Size: 4.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.20.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.8.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
821f28941f7e9dee69b59b28d36df97da7ad1a68d299c1732d5954e28716da61
|
|
| MD5 |
a3397e19872893d0bf831c30d534a449
|
|
| BLAKE2b-256 |
4a8c1d42bc711b0ab38b5f784a8503290d193b0ea669c75ed9ed9556ddb4c132
|