{"id":13765532,"url":"https://github.com/allisson/django-rest-framework-role-filters","last_synced_at":"2025-05-10T21:31:10.965Z","repository":{"id":50386515,"uuid":"120088976","full_name":"allisson/django-rest-framework-role-filters","owner":"allisson","description":"Simple role filtering for django-rest-framework","archived":false,"fork":false,"pushed_at":"2023-10-06T13:34:34.000Z","size":43,"stargazers_count":96,"open_issues_count":0,"forks_count":11,"subscribers_count":5,"default_branch":"master","last_synced_at":"2024-11-02T01:06:36.051Z","etag":null,"topics":["django","python","rest-framework","role"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/allisson.png","metadata":{"files":{"readme":"README.rst","changelog":"CHANGES.rst","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2018-02-03T12:26:40.000Z","updated_at":"2024-08-22T15:32:22.000Z","dependencies_parsed_at":"2024-04-18T17:57:58.370Z","dependency_job_id":"d0b15624-0e8f-4e00-8f13-4d6b88f84338","html_url":"https://github.com/allisson/django-rest-framework-role-filters","commit_stats":{"total_commits":29,"total_committers":5,"mean_commits":5.8,"dds":"0.13793103448275867","last_synced_commit":"716512f0184de69faa441df409b430359d426e57"},"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/allisson%2Fdjango-rest-framework-role-filters","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/allisson%2Fdjango-rest-framework-role-filters/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/allisson%2Fdjango-rest-framework-role-filters/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/allisson%2Fdjango-rest-framework-role-filters/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/allisson","download_url":"https://codeload.github.com/allisson/django-rest-framework-role-filters/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224989341,"owners_count":17403408,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["django","python","rest-framework","role"],"created_at":"2024-08-03T16:00:40.835Z","updated_at":"2024-11-17T01:31:09.557Z","avatar_url":"https://github.com/allisson.png","language":"Python","funding_links":[],"categories":["Packages"],"sub_categories":["Authorization"],"readme":"django-rest-framework-role-filters\n==================================\n\n.. image:: https://github.com/allisson/django-rest-framework-role-filters/workflows/tests/badge.svg\n    :target: https://github.com/allisson/django-rest-framework-role-filters/actions\n\n.. image:: https://img.shields.io/pypi/v/djangorestframework-role-filters.svg\n        :target: https://pypi.python.org/pypi/djangorestframework-role-filters\n\n.. image:: https://img.shields.io/github/license/allisson/django-rest-framework-role-filters.svg\n        :target: https://pypi.python.org/pypi/djangorestframework-role-filters\n\n.. image:: https://img.shields.io/pypi/pyversions/djangorestframework-role-filters.svg\n        :target: https://pypi.python.org/pypi/djangorestframework-role-filters\n\nHow to install\n--------------\n\n.. code:: shell\n\n    pip install djangorestframework-role-filters\n\nWhy i wrote this project?\n-------------------------\n\nI want work easily with roles without multiple ifs in code\n\nHow to use\n----------\n\nCreate role_filters.py with your roles definitions\n\n.. code:: python\n\n    from rest_framework_role_filters.role_filters import RoleFilter\n\n    from .serializers import PostSerializerForUser\n\n\n    class AdminRoleFilter(RoleFilter):\n        role_id = 'admin'\n\n\n    class UserRoleFilter(RoleFilter):\n        role_id = 'user'\n\n        def get_allowed_actions(self, request, view, obj=None):\n            # This example returns same list both for \"global permissions\" check,\n            # and for \"object\" permissions, but different list may be returned\n            # if `obj` argument is not None, and this list will be used to check\n            # if action is allowed during call to `ViewSet.check_object_permissions`\n            return ['create', 'list', 'retrieve', 'update', 'partial_update']\n\n        def get_queryset(self, request, view, queryset):\n            queryset = queryset.filter(user=request.user)\n            return queryset\n\n        def get_serializer_class(self, request, view):\n            return PostSerializerForUser\n\n        def get_serializer(self, request, view, serializer_class, *args, **kwargs):\n            fields = (\n                'body',\n                'created_at',\n                'id',\n                'serializer_name',\n                'title',\n                'updated_at',\n                'user',\n            )\n            return serializer_class(*args, fields=fields, **kwargs)\n\nCreate viewset and override get_role_id method\n\n.. code:: python\n\n    from rest_framework_role_filters.viewsets import RoleFilterModelViewSet\n\n    from .models import Post\n    from .role_filters import AdminRoleFilter, UserRoleFilter\n    from .serializers import PostSerializer\n\n\n    class PostViewSet(RoleFilterModelViewSet):\n        queryset = Post.objects.all()\n        serializer_class = PostSerializer\n        role_filter_classes = [AdminRoleFilter, UserRoleFilter]\n\n        def get_role_id(self, request):\n            return request.user.role.role_id\n\n        def perform_create(self, serializer):\n            serializer.save(user=self.request.user)\n\nIf role_id is 'admin':\n\n* All actions are allowed\n* The default queryset is returned - :code:`Post.objects.all()`\n* The default :code:`serializer_class` is used - :code:`PostSerializer`\n* The default viewset :code:`get_serializer` method is used\n\nIf role_id is 'user':\n\n* Only actions 'create', 'list', 'retrieve', 'update', 'partial_update' are allowed\n* The queryset is filtered by user\n* The :code:`serializer_class=PostSerializerForUser` is used\n* The serializer initializing with :code:`fields` kwargs  (e.g. for modified serializer as described in\n  `DRF: Dynamically modifying fields \u003chttps://www.django-rest-framework.org/api-guide/serializers/#dynamically-modifying-fields\u003e`_)\n\nCheck `testapp example \u003chttps://github.com/allisson/django-rest-framework-role-filters/tree/master/testproject/testapp\u003e`_ code implementation.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fallisson%2Fdjango-rest-framework-role-filters","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fallisson%2Fdjango-rest-framework-role-filters","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fallisson%2Fdjango-rest-framework-role-filters/lists"}