Skip to main content

django-rest-framework-role-filters

Project description

https://github.com/allisson/django-rest-framework-role-filters/workflows/tests/badge.svg https://img.shields.io/pypi/v/djangorestframework-role-filters.svg https://img.shields.io/github/license/allisson/django-rest-framework-role-filters.svg https://img.shields.io/pypi/pyversions/djangorestframework-role-filters.svg

How to install

pip install djangorestframework-role-filters

Why i wrote this project?

I want work easily with roles without multiple ifs in code

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, obj=None):
        # This example returns same list both for "global permissions" check,
        # and for "object" permissions, but different list may be returned
        # if `obj` argument is not None, and this list will be used to check
        # if action is allowed during call to `ViewSet.check_object_permissions`
        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

    def get_serializer(self, request, view, serializer_class, *args, **kwargs):
        fields = (
            'body',
            'created_at',
            'id',
            'serializer_name',
            'title',
            'updated_at',
            'user',
        )
        return serializer_class(*args, fields=fields, **kwargs)

Create viewset and override get_role_id method

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):
    queryset = Post.objects.all()
    serializer_class = PostSerializer
    role_filter_classes = [AdminRoleFilter, UserRoleFilter]

    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 are allowed

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

  • The default serializer_class is used - PostSerializer

  • The default viewset get_serializer method is used

If role_id is ‘user’:

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

  • The queryset is filtered by user

  • The serializer_class=PostSerializerForUser is used

  • The serializer initializing with fields kwargs (e.g. for modified serializer as described in DRF: Dynamically modifying fields)

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-1.0.0.tar.gz.

File metadata

  • Download URL: djangorestframework-role-filters-1.0.0.tar.gz
  • Upload date:
  • Size: 5.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.8.2

File hashes

Hashes for djangorestframework-role-filters-1.0.0.tar.gz
Algorithm Hash digest
SHA256 b83434a22981837ea2019af4a70d4c5d0bdede1377b55f75471e841f7b79b618
MD5 4d355e6cab748c069a2392e5d6c40c93
BLAKE2b-256 75ef50c98bc011be6c61a0eb91d34bee71688692910e10338d876df5710b64c8

See more details on using hashes here.

File details

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

File metadata

  • Download URL: djangorestframework_role_filters-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 5.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.8.2

File hashes

Hashes for djangorestframework_role_filters-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1a841184b4468115ea4278e1b1129d127a60c8e731f7fb3507a2b6080f5a57ad
MD5 71418a662829e5662208aad381438777
BLAKE2b-256 69a487ddf3876e7d9e2b25e99d5196f994d0f0eca55ed73123c55e4a2d07d0f7

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