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.

48 lines
1.8 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. from django.conf import settings
  2. from rest_framework import generics, status
  3. from rest_framework.permissions import IsAdminUser, IsAuthenticated
  4. from rest_framework.response import Response
  5. from members.permissions import IsProjectAdmin, IsProjectStaffAndReadOnly
  6. from ..models import Project
  7. from ..serializers import ProjectPolymorphicSerializer
  8. class ProjectList(generics.ListCreateAPIView):
  9. serializer_class = ProjectPolymorphicSerializer
  10. pagination_class = None
  11. def get_permissions(self):
  12. if self.request.method == 'GET':
  13. self.permission_classes = [IsAuthenticated, ]
  14. else:
  15. self.permission_classes = [IsAuthenticated & IsAdminUser]
  16. return super().get_permissions()
  17. def get_queryset(self):
  18. return Project.objects.filter(role_mappings__user=self.request.user)
  19. def perform_create(self, serializer):
  20. serializer.save(created_by=self.request.user)
  21. def delete(self, request, *args, **kwargs):
  22. delete_ids = request.data['ids']
  23. projects = Project.objects.filter(
  24. role_mappings__user=self.request.user,
  25. role_mappings__role__name=settings.ROLE_PROJECT_ADMIN,
  26. pk__in=delete_ids
  27. )
  28. # Todo: I want to use bulk delete.
  29. # But it causes the constraint error.
  30. # See https://github.com/django-polymorphic/django-polymorphic/issues/229
  31. for project in projects:
  32. project.delete()
  33. return Response(status=status.HTTP_204_NO_CONTENT)
  34. class ProjectDetail(generics.RetrieveUpdateDestroyAPIView):
  35. queryset = Project.objects.all()
  36. serializer_class = ProjectPolymorphicSerializer
  37. lookup_url_kwarg = 'project_id'
  38. permission_classes = [IsAuthenticated & (IsProjectAdmin | IsProjectStaffAndReadOnly)]