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.

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