django-rest-framework-role-filters
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, 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
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 -
Post.objects.all()The default
serializer_classis used -PostSerializerThe default viewset
get_serializermethod is used
If role_id is ‘user’:
Only actions ‘create’, ‘list’, ‘retrieve’, ‘update’, ‘partial_update’ are allowed
The queryset is filtered by user
The
serializer_class=PostSerializerForUseris usedThe serializer initializing with
fieldskwargs (e.g. for modified serializer as described in DRF: Dynamically modifying fields)
Check testapp example code implementation.
Metadata
Release files for djangorestframework-role-filters 1.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| djangorestframework-role-filters-1.1.0.tar.gz | 6.0 kB | Without provenance | Details for djangorestframework-role-filters-1.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| djangorestframework_role_filters-1.1.0-py3-none-any.whl | Python 3 | none | any | Details for djangorestframework_role_filters-1.1.0-py3-none-any.whl |
Total release size: 11.8 kB
Release files / djangorestframework-role-filters-1.1.0.tar.gz
| Download URL | djangorestframework-role-filters-1.1.0.tar.gz |
|---|---|
| Size | 6.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
f2257a1036dc2db9fc29c352e869313d50db52078bfc1b2805a745c538229eb2
|
|
BLAKE2b-256 checksum How to use checksums |
2bd86bc177f4880ffb5c0bad506dc736858c737c4f548d8103ca5b1546315723
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.12.0
|
Release files / djangorestframework_role_filters-1.1.0-py3-none-any.whl
| Download URL | djangorestframework_role_filters-1.1.0-py3-none-any.whl |
|---|---|
| Size | 5.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
36c9a7f54ade0f43e9e30d7f6fe55a3ad171da447af479210dd90622844cc1a6
|
|
BLAKE2b-256 checksum How to use checksums |
2a1cc9e1823dfea26731207fb26b5e08d57d5da02bebd7e9830eb23ee8b5f46d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.12.0
|
Security for djangorestframework-role-filters 1.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.