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.

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