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.
Raw data
{
"_id": null,
"home_page": "https://github.com/allisson/django-rest-framework-role-filters",
"name": "djangorestframework-role-filters",
"maintainer": "",
"docs_url": null,
"requires_python": "",
"maintainer_email": "",
"keywords": "djangorestframework rest role permission",
"author": "Allisson Azevedo",
"author_email": "allisson@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/2b/d8/6bc177f4880ffb5c0bad506dc736858c737c4f548d8103ca5b1546315723/djangorestframework-role-filters-1.1.0.tar.gz",
"platform": null,
"description": "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 <https://www.django-rest-framework.org/api-guide/serializers/#dynamically-modifying-fields>`_)\n\nCheck `testapp example <https://github.com/allisson/django-rest-framework-role-filters/tree/master/testproject/testapp>`_ code implementation.\n",
"bugtrack_url": null,
"license": "",
"summary": "django-rest-framework-role-filters",
"version": "1.1.0",
"project_urls": {
"Homepage": "https://github.com/allisson/django-rest-framework-role-filters"
},
"split_keywords": [
"djangorestframework",
"rest",
"role",
"permission"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "2a1cc9e1823dfea26731207fb26b5e08d57d5da02bebd7e9830eb23ee8b5f46d",
"md5": "16a14bd91e45fee7ff97e5790a0c2a8d",
"sha256": "36c9a7f54ade0f43e9e30d7f6fe55a3ad171da447af479210dd90622844cc1a6"
},
"downloads": -1,
"filename": "djangorestframework_role_filters-1.1.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "16a14bd91e45fee7ff97e5790a0c2a8d",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": null,
"size": 5773,
"upload_time": "2023-10-06T13:35:05",
"upload_time_iso_8601": "2023-10-06T13:35:05.614969Z",
"url": "https://files.pythonhosted.org/packages/2a/1c/c9e1823dfea26731207fb26b5e08d57d5da02bebd7e9830eb23ee8b5f46d/djangorestframework_role_filters-1.1.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "2bd86bc177f4880ffb5c0bad506dc736858c737c4f548d8103ca5b1546315723",
"md5": "9633ebc403814877f56cc9d3004ea5e7",
"sha256": "f2257a1036dc2db9fc29c352e869313d50db52078bfc1b2805a745c538229eb2"
},
"downloads": -1,
"filename": "djangorestframework-role-filters-1.1.0.tar.gz",
"has_sig": false,
"md5_digest": "9633ebc403814877f56cc9d3004ea5e7",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 5986,
"upload_time": "2023-10-06T13:35:07",
"upload_time_iso_8601": "2023-10-06T13:35:07.577058Z",
"url": "https://files.pythonhosted.org/packages/2b/d8/6bc177f4880ffb5c0bad506dc736858c737c4f548d8103ca5b1546315723/djangorestframework-role-filters-1.1.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-10-06 13:35:07",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "allisson",
"github_project": "django-rest-framework-role-filters",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"requirements": [],
"lcname": "djangorestframework-role-filters"
}