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.
from django.views.generic import ListView, CreateView

from multiple_permissions.permissions import IsAuthenticated, IsSuperuser, IsManager


class PollsListView(ListView):
    multiple_permissions = (IsAuthenticated,)
    ...


class PollsCreateView(CreateView):
    multiple_permissions = (IsSuperuser, IsManager)
    ...

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.

Files for multiple-permissions, version 0.1.9
Filename, size File type Python version Upload date Hashes
Filename, size multiple_permissions-0.1.9-py3-none-any.whl (4.4 kB) File type Wheel Python version py3 Upload date Hashes View
Filename, size multiple_permissions-0.1.9.tar.gz (2.9 kB) File type Source Python version None Upload date Hashes View

Supported by

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