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

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