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 hashes)

Uploaded source

Built Distribution

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page