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 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):
        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.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

  • The default viewset get_serializer method is used

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

  • The serializer initializing with fields kwargs

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

File metadata

  • Download URL: djangorestframework-role-filters-0.2.3.tar.gz
  • Upload date:
  • Size: 4.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.20.1 setuptools/39.0.1 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.7

File hashes

Hashes for djangorestframework-role-filters-0.2.3.tar.gz
Algorithm Hash digest
SHA256 bfdd3d05114d1a9592f03f34b9e1dc04ac85b503416f391f3e4e70dee1265b10
MD5 f4fcdf5ce6c6d350a81a65ac08ae6dd7
BLAKE2b-256 33fdbb03b60976c8ea1d92dfe111262870d4acc7f185344b6863a4b69f92d5fb

See more details on using hashes here.

File details

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

File metadata

  • Download URL: djangorestframework_role_filters-0.2.3-py3-none-any.whl
  • Upload date:
  • Size: 4.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.20.1 setuptools/39.0.1 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.7

File hashes

Hashes for djangorestframework_role_filters-0.2.3-py3-none-any.whl
Algorithm Hash digest
SHA256 fd707f243aa007eecf73e330ba1f95099aec2d89f2da11ea31aabac148aad4f7
MD5 747b86ea26b5266f68423915cf798ecd
BLAKE2b-256 e5920fefa954faa6f60d5db9f8dffe69345e738196515689906878509a08c21c

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