from django.shortcuts import get_object_or_404 from rest_framework import generics, status from rest_framework.permissions import IsAuthenticated from rest_framework.response import Response from ..models import Project, Role, RoleMapping from ..permissions import IsProjectAdmin from ..serializers import RoleMappingSerializer, RoleSerializer class Roles(generics.ListCreateAPIView): serializer_class = RoleSerializer pagination_class = None permission_classes = [IsAuthenticated & IsProjectAdmin] queryset = Role.objects.all() class RoleMappingList(generics.ListCreateAPIView): serializer_class = RoleMappingSerializer pagination_class = None permission_classes = [IsAuthenticated & IsProjectAdmin] def get_queryset(self): project = get_object_or_404(Project, pk=self.kwargs['project_id']) return project.role_mappings def perform_create(self, serializer): project = get_object_or_404(Project, pk=self.kwargs['project_id']) serializer.save(project=project) def delete(self, request, *args, **kwargs): delete_ids = request.data['ids'] RoleMapping.objects.filter(pk__in=delete_ids).delete() return Response(status=status.HTTP_204_NO_CONTENT) class RoleMappingDetail(generics.RetrieveUpdateDestroyAPIView): queryset = RoleMapping.objects.all() serializer_class = RoleMappingSerializer lookup_url_kwarg = 'rolemapping_id' permission_classes = [IsAuthenticated & IsProjectAdmin]