All Projects → allisson → Django Rest Framework Role Filters

allisson / Django Rest Framework Role Filters

Licence: mit
Simple role filtering for django-rest-framework

Programming Languages

python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to Django Rest Framework Role Filters

Django Chinese Docs 18
📖 [译] django 中文文档协作翻译计划
Stars: ✭ 61 (-8.96%)
Mutual labels:  django
Sapl
Sistema de Apoio ao Processo Legislativo
Stars: ✭ 63 (-5.97%)
Mutual labels:  django
Osis
Open Student Information System Backoffice
Stars: ✭ 65 (-2.99%)
Mutual labels:  django
Django Taggit Labels
Clickable label widget for django-taggit
Stars: ✭ 62 (-7.46%)
Mutual labels:  django
Best Of Web Python
🏆 A ranked list of awesome python libraries for web development. Updated weekly.
Stars: ✭ 1,118 (+1568.66%)
Mutual labels:  django
Mrdoc
online document system developed based on python. It is suitable for individuals and small teams to manage documents, wiki, knowledge and notes. like gitbook.
Stars: ✭ 1,129 (+1585.07%)
Mutual labels:  django
Cookiecutter Django Rest
Build best practiced apis fast with Python3
Stars: ✭ 1,108 (+1553.73%)
Mutual labels:  django
Facial Recognition Python Django
Face detection and facial recognition along with recognized persons information fetched from database.
Stars: ✭ 66 (-1.49%)
Mutual labels:  django
Webterminal
ssh rdp vnc telnet sftp bastion/jump web putty xshell terminal jumpserver audit realtime monitor rz/sz 堡垒机 云桌面 linux devops sftp websocket file management rz/sz otp 自动化运维 审计 录像 文件管理 sftp上传 实时监控 录像回放 网页版rz/sz上传下载/动态口令 django
Stars: ✭ 1,124 (+1577.61%)
Mutual labels:  django
Thinkdiff
My open source project links, programming and software development related code and tutorials are in this repo. Content types: Python, JavaScript, Dart | Django, React, Flutter, React-Native etc.
Stars: ✭ 65 (-2.99%)
Mutual labels:  django
Lagom
📦 Autowiring dependency injection container for python 3
Stars: ✭ 61 (-8.96%)
Mutual labels:  django
Django Crm
A Simple Django CRM (Customer Relationship Management) RESTful API Project
Stars: ✭ 63 (-5.97%)
Mutual labels:  django
Django Qsessions
Extended session backends for Django (Sessions store IP, User Agent, and foreign key to User)
Stars: ✭ 64 (-4.48%)
Mutual labels:  django
Djaoapp
User login, billing, access control as part of a session proxy
Stars: ✭ 61 (-8.96%)
Mutual labels:  django
Django Polymorphic
Improved Django model inheritance with automatic downcasting
Stars: ✭ 1,135 (+1594.03%)
Mutual labels:  django
Django Anymail
Django email backends and webhooks for Amazon SES, Mailgun, Mailjet, Postmark, SendGrid, Sendinblue, SparkPost and more
Stars: ✭ 1,109 (+1555.22%)
Mutual labels:  django
Taggit Selectize
Auto-complete/auto-suggestion for django-taggit (django-taggit + selectize.js)
Stars: ✭ 63 (-5.97%)
Mutual labels:  django
Djangoevents
Stars: ✭ 66 (-1.49%)
Mutual labels:  django
Wemake Django Template
Bleeding edge django template focused on code quality and security.
Stars: ✭ 1,141 (+1602.99%)
Mutual labels:  django
Django Statsy
Statistics for your Django project
Stars: ✭ 64 (-4.48%)
Mutual labels:  django

django-rest-framework-role-filters

.. image:: https://github.com/allisson/django-rest-framework-role-filters/workflows/tests/badge.svg :target: https://github.com/allisson/django-rest-framework-role-filters/actions

.. image:: https://img.shields.io/pypi/v/djangorestframework-role-filters.svg :target: https://pypi.python.org/pypi/djangorestframework-role-filters

.. image:: https://img.shields.io/github/license/allisson/django-rest-framework-role-filters.svg :target: https://pypi.python.org/pypi/djangorestframework-role-filters

.. image:: https://img.shields.io/pypi/pyversions/djangorestframework-role-filters.svg :target: https://pypi.python.org/pypi/djangorestframework-role-filters

How to install

.. code:: shell

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

.. code:: python

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

.. code:: python

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 - :code:Post.objects.all()
  • The default :code:serializer_class is used - :code:PostSerializer
  • The default viewset :code: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 :code:serializer_class=PostSerializerForUser is used
  • The serializer initializing with :code:fields kwargs (e.g. for modified serializer as described in DRF: Dynamically modifying fields <https://www.django-rest-framework.org/api-guide/serializers/#dynamically-modifying-fields>_)

Check testapp example <https://github.com/allisson/django-rest-framework-role-filters/tree/master/testproject/testapp>_ code implementation.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].