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.

53 lines
2.0 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  1. from django.db import IntegrityError
  2. from django_filters.rest_framework import DjangoFilterBackend
  3. from rest_framework import generics, status
  4. from rest_framework.permissions import IsAuthenticated
  5. from rest_framework.response import Response
  6. from projects.exceptions import RoleAlreadyAssignedException, RoleConstraintException
  7. from projects.models import Member
  8. from projects.permissions import IsProjectAdmin
  9. from projects.serializers import MemberSerializer
  10. class MemberList(generics.ListCreateAPIView):
  11. filter_backends = [DjangoFilterBackend]
  12. filterset_fields = ["user"]
  13. queryset = Member.objects.all()
  14. serializer_class = MemberSerializer
  15. pagination_class = None
  16. permission_classes = [IsAuthenticated & IsProjectAdmin]
  17. def filter_queryset(self, queryset):
  18. queryset = queryset.filter(project=self.kwargs["project_id"])
  19. return super().filter_queryset(queryset)
  20. def perform_create(self, serializer):
  21. try:
  22. serializer.save(project_id=self.kwargs["project_id"])
  23. except IntegrityError:
  24. raise RoleAlreadyAssignedException
  25. def delete(self, request, *args, **kwargs):
  26. delete_ids = request.data["ids"]
  27. project_id = self.kwargs["project_id"]
  28. Member.objects.filter(project=project_id, pk__in=delete_ids).exclude(user=self.request.user).delete()
  29. return Response(status=status.HTTP_204_NO_CONTENT)
  30. class MemberDetail(generics.RetrieveUpdateAPIView):
  31. queryset = Member.objects.all()
  32. serializer_class = MemberSerializer
  33. lookup_url_kwarg = "member_id"
  34. permission_classes = [IsAuthenticated & IsProjectAdmin]
  35. def perform_update(self, serializer):
  36. project_id = self.kwargs["project_id"]
  37. member_id = self.kwargs["member_id"]
  38. role = serializer.validated_data["role"]
  39. if not Member.objects.can_update(project_id, member_id, role.name):
  40. raise RoleConstraintException
  41. try:
  42. super().perform_update(serializer)
  43. except IntegrityError:
  44. raise RoleAlreadyAssignedException