Skip to main content

Use multiple permission classes in django class based views.

Project description

Django multiple permissions

Python License

Usage

  • install package
$ pip install multiple-permissions
  • add apps.core to installed apps
INSTALLED_APPS = [
    ...,
    "multiple_permissions",
    ...,
]
  • add apps.core.middlewares to MIDDLEWARE list
MIDDLEWARE = [
    ...,
    "multiple_permissions.middlewares.PermissionMiddleware",
]
  • Add "permission_classes" attribute to view classes
from django.views.generic import ListView, CreateView

from multiple_permissions.permissions import IsAuthenticated, IsSuperuser, IsManager


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


class PollsCreateView(CreateView):
    permission_classes = (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_moderator attribute or model field otherwise you'll catch AttributeError

from multiple_permissions.permissions import BasePermission


class IsModerator(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.6
Filename, size File type Python version Upload date Hashes
Filename, size multiple_permissions-0.1.6.tar.gz (2.7 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page