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.

52 lines
1.8 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. from django_filters.rest_framework import DjangoFilterBackend
  2. from rest_framework import filters, generics, status
  3. from rest_framework.permissions import IsAuthenticated
  4. from rest_framework.response import Response
  5. from ..models import Comment
  6. from ..permissions import IsInProjectOrAdmin, IsOwnComment
  7. from ..serializers import CommentSerializer
  8. class CommentListDoc(generics.ListCreateAPIView):
  9. pagination_class = None
  10. permission_classes = [IsAuthenticated & IsInProjectOrAdmin]
  11. serializer_class = CommentSerializer
  12. model = Comment
  13. def get_queryset(self):
  14. queryset = self.model.objects.filter(
  15. example__project_id=self.kwargs['project_id'],
  16. example=self.kwargs['doc_id']
  17. )
  18. return queryset
  19. def perform_create(self, serializer):
  20. serializer.save(example_id=self.kwargs['doc_id'], user=self.request.user)
  21. class CommentListProject(generics.ListAPIView):
  22. pagination_class = None
  23. permission_classes = [IsAuthenticated & IsInProjectOrAdmin]
  24. serializer_class = CommentSerializer
  25. filter_backends = (DjangoFilterBackend, filters.SearchFilter)
  26. search_fields = ('text',)
  27. model = Comment
  28. def get_queryset(self):
  29. queryset = self.model.objects.filter(
  30. example__project_id=self.kwargs['project_id']
  31. )
  32. return queryset
  33. def delete(self, request, *args, **kwargs):
  34. delete_ids = request.data['ids']
  35. self.model.objects.filter(user=request.user, pk__in=delete_ids).delete()
  36. return Response(status=status.HTTP_204_NO_CONTENT)
  37. class CommentDetail(generics.RetrieveUpdateDestroyAPIView):
  38. queryset = Comment.objects.all()
  39. serializer_class = CommentSerializer
  40. lookup_url_kwarg = 'comment_id'
  41. permission_classes = [IsAuthenticated & IsInProjectOrAdmin & IsOwnComment]