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.

37 lines
1.5 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, filters.OrderingFilter)
  13. filterset_fields = ["example"]
  14. search_fields = ("text",)
  15. ordering_fields = ("created_at", "example")
  16. def get_queryset(self):
  17. queryset = Comment.objects.filter(example__project_id=self.kwargs["project_id"])
  18. return queryset
  19. def perform_create(self, serializer):
  20. serializer.save(example_id=self.request.query_params.get("example"), user=self.request.user)
  21. def delete(self, request, *args, **kwargs):
  22. delete_ids = request.data["ids"]
  23. Comment.objects.filter(user=request.user, pk__in=delete_ids).delete()
  24. return Response(status=status.HTTP_204_NO_CONTENT)
  25. class CommentDetail(generics.RetrieveUpdateDestroyAPIView):
  26. queryset = Comment.objects.all()
  27. serializer_class = CommentSerializer
  28. lookup_url_kwarg = "comment_id"
  29. permission_classes = [IsAuthenticated & IsProjectMember & IsOwnComment]