views.py 1.2 KB

12345678910111213141516171819202122232425262728293031
  1. from rest_framework import viewsets, exceptions
  2. from projects.models import Project
  3. from .models import ProjectAlert
  4. from .serializers import ProjectAlertSerializer
  5. from .permissions import ProjectAlertPermission
  6. class ProjectAlertViewSet(viewsets.ModelViewSet):
  7. queryset = ProjectAlert.objects.all()
  8. serializer_class = ProjectAlertSerializer
  9. permission_classes = [ProjectAlertPermission]
  10. def get_queryset(self):
  11. if not self.request.user.is_authenticated:
  12. return self.queryset.none()
  13. return self.queryset.filter(
  14. project__slug=self.kwargs.get("project_slug"),
  15. project__team__members__user=self.request.user,
  16. project__organization__slug=self.kwargs.get("organization_slug"),
  17. )
  18. def perform_create(self, serializer):
  19. try:
  20. project = Project.objects.get(
  21. slug=self.kwargs.get("project_slug"),
  22. team__members__user=self.request.user,
  23. organization__slug=self.kwargs.get("organization_slug"),
  24. )
  25. except Project.DoesNotExist:
  26. raise exceptions.ValidationError("Organization does not exist")
  27. serializer.save(project=project)