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.

50 lines
2.0 KiB

3 years ago
3 years ago
3 years ago
2 years ago
3 years ago
2 years ago
3 years ago
3 years ago
3 years ago
2 years ago
3 years ago
2 years ago
  1. from django.shortcuts import get_object_or_404
  2. from django_filters.rest_framework import DjangoFilterBackend
  3. from rest_framework import filters, generics, status
  4. from rest_framework.permissions import IsAuthenticated
  5. from rest_framework.response import Response
  6. from examples.filters import ExampleFilter
  7. from examples.models import Example
  8. from examples.serializers import ExampleSerializer
  9. from projects.models import Member, Project
  10. from projects.permissions import IsProjectAdmin, IsProjectStaffAndReadOnly
  11. class ExampleList(generics.ListCreateAPIView):
  12. serializer_class = ExampleSerializer
  13. permission_classes = [IsAuthenticated & (IsProjectAdmin | IsProjectStaffAndReadOnly)]
  14. filter_backends = (DjangoFilterBackend, filters.SearchFilter, filters.OrderingFilter)
  15. ordering_fields = ("created_at", "updated_at", "score")
  16. search_fields = ("text", "filename")
  17. model = Example
  18. filterset_class = ExampleFilter
  19. @property
  20. def project(self):
  21. return get_object_or_404(Project, pk=self.kwargs["project_id"])
  22. def get_queryset(self):
  23. member = get_object_or_404(Member, project=self.project, user=self.request.user)
  24. if member.is_admin():
  25. return self.model.objects.filter(project=self.project)
  26. return self.model.objects.filter(project=self.project, assignments__assignee=self.request.user)
  27. def perform_create(self, serializer):
  28. serializer.save(project=self.project)
  29. def delete(self, request, *args, **kwargs):
  30. queryset = self.project.examples
  31. delete_ids = request.data["ids"]
  32. if delete_ids:
  33. queryset.filter(pk__in=delete_ids).delete()
  34. else:
  35. queryset.all().delete()
  36. return Response(status=status.HTTP_204_NO_CONTENT)
  37. class ExampleDetail(generics.RetrieveUpdateDestroyAPIView):
  38. queryset = Example.objects.all()
  39. serializer_class = ExampleSerializer
  40. lookup_url_kwarg = "example_id"
  41. permission_classes = [IsAuthenticated & (IsProjectAdmin | IsProjectStaffAndReadOnly)]