GithubHelp home page GithubHelp logo

jtszalay / django-rest-framework-role-filters Goto Github PK

View Code? Open in Web Editor NEW

This project forked from allisson/django-rest-framework-role-filters

0.0 1.0 0.0 20 KB

Simple role filtering for django-rest-framework

License: MIT License

Makefile 2.44% Python 97.56%

django-rest-framework-role-filters's Introduction

django-rest-framework-role-filters

https://travis-ci.org/allisson/django-rest-framework-role-filters.svg?branch=master

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.

django-rest-framework-role-filters's People

Contributors

allisson avatar jtszalay avatar thecoons avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.