Skip to main content

django-rest-framework-role-filters

Project description

https://travis-ci.org/allisson/django-rest-framework-role-filters.svg?branch=master https://codecov.io/gh/allisson/django-rest-framework-role-filters/branch/master/graph/badge.svg

How to install

pip install djangorestframework-role-filters

How to use

Create role_filters.py with your roles definitions

from rest_framework_role_filters.role_filters import RoleFilter

from .serializers import PostSerializerForUser


class AdminRoleFilter(RoleFilter):
    role_id = 'admin'


class UserRoleFilter(RoleFilter):
    role_id = 'user'

    def get_allowed_actions(self, request, view):
        return ['create', 'list', 'retrieve', 'update', 'partial_update']

    def get_queryset(self, request, view, queryset):
        queryset = queryset.filter(user=request.user)
        return queryset

    def get_serializer_class(self, request, view):
        return PostSerializerForUser

Create viewset and override get_role_id method

from rest_framework_role_filters.role_filters import RoleFilterGroup
from rest_framework_role_filters.viewsets import RoleFilterModelViewSet

from .models import Post
from .role_filters import AdminRoleFilter, UserRoleFilter
from .serializers import PostSerializer


class PostViewSet(RoleFilterModelViewSet):
    role_filter_group = RoleFilterGroup(role_filters=[AdminRoleFilter(), UserRoleFilter()])
    queryset = Post.objects.all()
    serializer_class = PostSerializer

    def get_role_id(self, request):
        return request.user.role.role_id

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

If role_id is ‘admin’:

  • All actions is allowed

  • The default queryset is returned - Post.objects.all()

  • The default serializer_class is used - PostSerializer

If role_id is ‘user’:

  • Only actions ‘create’, ‘list’, ‘retrieve’, ‘update’, ‘partial_update’ is allowed

  • The queryset is filtered by user

  • The serializer_class PostSerializerForUser is used

Check testapp example code implementation.

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

Built Distribution

File details

Details for the file djangorestframework-role-filters-0.1.0.tar.gz.

File metadata

File hashes

Hashes for djangorestframework-role-filters-0.1.0.tar.gz
Algorithm Hash digest
SHA256 e7ecbacb6a3515391e18fc8b8d7f6e2bc8d82c9d9a9f6b9911bc88e13a78b5b8
MD5 a71c086d08a1bedd64443d33c5132d55
BLAKE2b-256 22226999c9e43ec3d8a2c263ff4978e323e19d80a61171280e0b007f9b80affb

See more details on using hashes here.

File details

Details for the file djangorestframework_role_filters-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for djangorestframework_role_filters-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4345a1d3eca5aaa68b30ac5c1891bf8b65243d57affa3e5a36faf54d4300857a
MD5 c1b10e84307fca3179f660c1a9abcecf
BLAKE2b-256 b25af41548689db7197bc24392aed07d1c9e4cf7d72de9bb7d04c9926d7ee11e

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