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.

36 lines
1.4 KiB

3 years ago
3 years ago
2 years ago
3 years ago
3 years ago
2 years ago
3 years ago
2 years ago
3 years ago
2 years ago
3 years ago
2 years ago
3 years ago
2 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 examples.models import Comment
  6. from examples.permissions import IsOwnComment
  7. from examples.serializers import CommentSerializer
  8. from projects.permissions import IsProjectMember
  9. class CommentList(generics.ListCreateAPIView):
  10. permission_classes = [IsAuthenticated & IsProjectMember]
  11. serializer_class = CommentSerializer
  12. filter_backends = (DjangoFilterBackend, filters.SearchFilter)
  13. filterset_fields = ["example"]
  14. search_fields = ("text",)
  15. def get_queryset(self):
  16. queryset = Comment.objects.filter(example__project_id=self.kwargs["project_id"])
  17. return queryset
  18. def perform_create(self, serializer):
  19. serializer.save(example_id=self.request.query_params.get("example"), user=self.request.user)
  20. def delete(self, request, *args, **kwargs):
  21. delete_ids = request.data["ids"]
  22. Comment.objects.filter(user=request.user, pk__in=delete_ids).delete()
  23. return Response(status=status.HTTP_204_NO_CONTENT)
  24. class CommentDetail(generics.RetrieveUpdateDestroyAPIView):
  25. queryset = Comment.objects.all()
  26. serializer_class = CommentSerializer
  27. lookup_url_kwarg = "comment_id"
  28. permission_classes = [IsAuthenticated & IsProjectMember & IsOwnComment]