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.

32 lines
1.2 KiB

  1. from django.shortcuts import get_object_or_404
  2. from rest_framework import generics
  3. from rest_framework.permissions import IsAuthenticated
  4. from members.permissions import IsInProjectOrAdmin
  5. from ..models import Example, ExampleState, Project
  6. from ..serializers import ExampleStateSerializer
  7. class ExampleStateList(generics.ListCreateAPIView):
  8. serializer_class = ExampleStateSerializer
  9. permission_classes = [IsAuthenticated & IsInProjectOrAdmin]
  10. @property
  11. def can_confirm_per_user(self):
  12. project = get_object_or_404(Project, pk=self.kwargs['project_id'])
  13. return not project.collaborative_annotation
  14. def get_queryset(self):
  15. queryset = ExampleState.objects.filter(example=self.kwargs['example_id'])
  16. if self.can_confirm_per_user:
  17. queryset = queryset.filter(confirmed_by=self.request.user)
  18. return queryset
  19. def perform_create(self, serializer):
  20. queryset = self.get_queryset()
  21. if queryset.exists():
  22. queryset.delete()
  23. else:
  24. example = get_object_or_404(Example, pk=self.kwargs['example_id'])
  25. serializer.save(example=example, confirmed_by=self.request.user)