From 0cdc8ae004a6d18f366203c9b13946f38e57bb18 Mon Sep 17 00:00:00 2001 From: Yohann D'ANELLO Date: Thu, 2 Apr 2020 16:06:58 +0200 Subject: [PATCH] RolePermissions were missing in the API --- apps/permission/api/serializers.py | 13 ++++++++++++- apps/permission/api/urls.py | 5 +++-- apps/permission/api/views.py | 20 ++++++++++++++++---- 3 files changed, 31 insertions(+), 7 deletions(-) diff --git a/apps/permission/api/serializers.py b/apps/permission/api/serializers.py index 0a52f4fe..e30ed7dc 100644 --- a/apps/permission/api/serializers.py +++ b/apps/permission/api/serializers.py @@ -3,7 +3,7 @@ from rest_framework import serializers -from ..models import Permission +from ..models import Permission, RolePermissions class PermissionSerializer(serializers.ModelSerializer): @@ -15,3 +15,14 @@ class PermissionSerializer(serializers.ModelSerializer): class Meta: model = Permission fields = '__all__' + + +class RolePermissionsSerializer(serializers.ModelSerializer): + """ + REST API Serializer for RolePermissions types. + The djangorestframework plugin will analyse the model `RolePermissions` and parse all fields in the API. + """ + + class Meta: + model = RolePermissions + fields = '__all__' diff --git a/apps/permission/api/urls.py b/apps/permission/api/urls.py index d50344ea..b5d53466 100644 --- a/apps/permission/api/urls.py +++ b/apps/permission/api/urls.py @@ -1,11 +1,12 @@ # Copyright (C) 2018-2020 by BDE ENS Paris-Saclay # SPDX-License-Identifier: GPL-3.0-or-later -from .views import PermissionViewSet +from .views import PermissionViewSet, RolePermissionsViewSet def register_permission_urls(router, path): """ Configure router for permission REST API. """ - router.register(path, PermissionViewSet) + router.register(path + "/permission", PermissionViewSet) + router.register(path + "/roles", RolePermissionsViewSet) diff --git a/apps/permission/api/views.py b/apps/permission/api/views.py index 965e82c9..6a068225 100644 --- a/apps/permission/api/views.py +++ b/apps/permission/api/views.py @@ -4,17 +4,29 @@ from django_filters.rest_framework import DjangoFilterBackend from api.viewsets import ReadOnlyProtectedModelViewSet -from .serializers import PermissionSerializer -from ..models import Permission +from .serializers import PermissionSerializer, RolePermissionsSerializer +from ..models import Permission, RolePermissions class PermissionViewSet(ReadOnlyProtectedModelViewSet): """ REST API View set. - The djangorestframework plugin will get all `Changelog` objects, serialize it to JSON with the given serializer, - then render it on /api/logs/ + The djangorestframework plugin will get all `Permission` objects, serialize it to JSON with the given serializer, + then render it on /api/permission/permission/ """ queryset = Permission.objects.all() serializer_class = PermissionSerializer filter_backends = [DjangoFilterBackend] filterset_fields = ['model', 'type', ] + + +class RolePermissionsViewSet(ReadOnlyProtectedModelViewSet): + """ + REST API View set. + The djangorestframework plugin will get all `RolePermission` objects, serialize it to JSON with the given serializer + then render it on /api/permission/roles/ + """ + queryset = RolePermissions.objects.all() + serializer_class = RolePermissionsSerializer + filter_backends = [DjangoFilterBackend] + filterset_fields = ['role', ]