Skip to main content

Use multiple permission classes in django class based views.

Project description

Django multiple permissions

Python License

Usage

  • Install the package.
$ pip install multiple-permissions
  • Add multiple_permissions to installed apps.
INSTALLED_APPS = [
    ...,
    "multiple_permissions",
    ...,
]
  • Add PermissionMiddleware to MIDDLEWARE list.
MIDDLEWARE = [
    ...,
    "multiple_permissions.middlewares.PermissionMiddleware",
]
  • Set permission_classes attribute to view classes.
  • Set exception_class attribute 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.py and write the followng code

  • note that your user should have is_manager attribute 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

multiple_permissions-0.2.2.tar.gz (3.0 kB view details)

Uploaded Source

Built Distribution

multiple_permissions-0.2.2-py3-none-any.whl (4.5 kB view details)

Uploaded Python 3

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

Hashes for multiple_permissions-0.2.2.tar.gz
Algorithm Hash digest
SHA256 cf0764509a26af9cbf71f71fe7bc05c5ae17cf5cee7b9bc78ef1d037dcc2841d
MD5 97e34f3cd0f060f9eeb6bad935ae4b11
BLAKE2b-256 de42e590efa31185fa9a33dfc8179d9da41520a5a8d1f39479e7d3b5f04fca21

See more details on using hashes here.

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

Hashes for multiple_permissions-0.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 821f28941f7e9dee69b59b28d36df97da7ad1a68d299c1732d5954e28716da61
MD5 a3397e19872893d0bf831c30d534a449
BLAKE2b-256 4a8c1d42bc711b0ab38b5f784a8503290d193b0ea669c75ed9ed9556ddb4c132

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page