You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

62 lines
2.1 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. from django.db import IntegrityError
  2. from django.shortcuts import get_object_or_404
  3. from rest_framework import generics, status
  4. from rest_framework.permissions import IsAuthenticated
  5. from rest_framework.response import Response
  6. from ..exceptions import RoleAlreadyAssignedException, RoleConstraintException
  7. from ..models import Project, Role, RoleMapping
  8. from ..permissions import IsProjectAdmin
  9. from ..serializers import RoleMappingSerializer, RoleSerializer
  10. class Roles(generics.ListCreateAPIView):
  11. serializer_class = RoleSerializer
  12. pagination_class = None
  13. permission_classes = [IsAuthenticated & IsProjectAdmin]
  14. queryset = Role.objects.all()
  15. class RoleMappingList(generics.ListCreateAPIView):
  16. serializer_class = RoleMappingSerializer
  17. pagination_class = None
  18. permission_classes = [IsAuthenticated & IsProjectAdmin]
  19. @property
  20. def project(self):
  21. return get_object_or_404(Project, pk=self.kwargs['project_id'])
  22. def get_queryset(self):
  23. return self.project.role_mappings
  24. def perform_create(self, serializer):
  25. try:
  26. serializer.save(project=self.project)
  27. except IntegrityError:
  28. raise RoleAlreadyAssignedException
  29. def delete(self, request, *args, **kwargs):
  30. delete_ids = request.data['ids']
  31. RoleMapping.objects.filter(project=self.project, pk__in=delete_ids)\
  32. .exclude(user=self.request.user)\
  33. .delete()
  34. return Response(status=status.HTTP_204_NO_CONTENT)
  35. class RoleMappingDetail(generics.RetrieveUpdateDestroyAPIView):
  36. queryset = RoleMapping.objects.all()
  37. serializer_class = RoleMappingSerializer
  38. lookup_url_kwarg = 'rolemapping_id'
  39. permission_classes = [IsAuthenticated & IsProjectAdmin]
  40. def perform_update(self, serializer):
  41. project_id = self.kwargs['project_id']
  42. id = self.kwargs['rolemapping_id']
  43. role = serializer.validated_data['role']
  44. if not RoleMapping.objects.can_update(project_id, id, role.name):
  45. raise RoleConstraintException
  46. try:
  47. super().perform_update(serializer)
  48. except IntegrityError:
  49. raise RoleAlreadyAssignedException