Skip to main content
https://travis-ci.org/apirobot/django-rest-action-permissions.svg?branch=master https://codecov.io/gh/apirobot/django-rest-action-permissions/branch/master/graph/badge.svg https://badge.fury.io/py/django-rest-action-permissions.svg

Django REST Action Permissions

django-rest-action-permissions allows you to define permissions for each action provided by your ViewSet class.

Installation

Install using pip:

$ pip install django-rest-action-permissions

Usage

This library lets you define permissions like so:

# permissions.py
from rest_framework.permissions import (
    AllowAny, BasePermission, IsAdminUser, IsAuthenticated
)
from rest_action_permissions.permissions import ActionPermission


class IsTweetOwner(BasePermission):

    def has_object_permission(self, request, view, obj):
        return obj.owner == request.user


class TweetPermission(ActionPermission):
    # The admin user has all permissions.
    enough_perms = IsAdminUser

    # Corresponding permissions for each action.
    create_perms = IsAuthenticated
    retrieve_perms = AllowAny
    list_perms = AllowAny
    update_perms = IsTweetOwner
    delete_perms = IsTweetOwner
    retweet_perms = IsAuthenticated
    undo_retweet_perms = IsAuthenticated

    # General read/write permissions.
    # Used if corresponding action permission hasn't been specified.
    read_perms = AllowAny
    write_perms = IsAuthenticated & IsTweetOwner

Corresponding ViewSet for the permissions defined above:

# views.py
from rest_framework import viewsets
from rest_framework.decorators import detail_route
from .models import Tweet
from .permissions import TweetPermission
from .serializers import TweetSerializer


class TweetViewSet(viewsets.ModelViewSet):
    queryset = Tweet.objects.all()
    serializer_class = TweetSerializer
    permission_classes = (TweetPermission, )

    def perform_create(self, serializer):
        serializer.save(owner=self.request.user)

    @detail_route(methods=['POST'])
    def retweet(self, request, *args, **kwargs):
        ...

    @detail_route(methods=['POST'])
    def undo_retweet(self, request, *args, **kwargs):
        ...

Credits

The interface of this library was inspired by taiga project.

Metadata

Release files for django-rest-action-permissions 2.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-rest-action-permissions 2.0.0
File Size Uploaded
django-rest-action-permissions-2.0.0.tar.gz 4.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-rest-action-permissions 2.0.0
File Interpreter ABI Platform
django_rest_action_permissions-2.0.0-py2.py3-none-any.whl Python 3, Python 2 none any Details

Total release size: 10.0 kB

Release files / django-rest-action-permissions-2.0.0.tar.gz

Download URL django-rest-action-permissions-2.0.0.tar.gz
Size 4.9 kB
Tags Source
SHA-256 checksum
How to use checksums
ffd33367a2e836f1e5c7b9bda00d82095ac10cbc62afbe9aea19b5fb8fc51a3f
BLAKE2b-256 checksum
How to use checksums
278d30034b1fac43ce57fa88acba2d1b050bcb6c06fb4c2a20e7cf4ea5aeb12f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.3

Release files / django_rest_action_permissions-2.0.0-py2.py3-none-any.whl

Download URL django_rest_action_permissions-2.0.0-py2.py3-none-any.whl
Size 5.1 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
910736301849f5d3fe22087a8514ce5ef0734ff8991f424a6fb4fc2414d14103
BLAKE2b-256 checksum
How to use checksums
f8db7250df7ed5ef0af5236ad826cedccfd1d910bd8faaa1b37cea19ec58d86b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.3

Release history Release notifications | RSS feed

This release

2.0.0 This release

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page