views.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. from rest_framework import mixins, viewsets
  2. from .models import Environment, EnvironmentProject
  3. from .permissions import EnvironmentPermission, EnvironmentProjectPermission
  4. from .serializers import EnvironmentProjectSerializer, EnvironmentSerializer
  5. class EnvironmentViewSet(viewsets.ReadOnlyModelViewSet):
  6. queryset = Environment.objects.filter(environmentproject__is_hidden=False)
  7. serializer_class = EnvironmentSerializer
  8. permission_classes = [EnvironmentPermission]
  9. def get_queryset(self):
  10. if not self.request.user.is_authenticated:
  11. return self.queryset.none()
  12. queryset = self.queryset.filter(organization__users=self.request.user)
  13. organization_slug = self.kwargs.get("organization_slug")
  14. if organization_slug:
  15. queryset = queryset.filter(organization__slug=organization_slug)
  16. return queryset
  17. class EnvironmentProjectViewSet(
  18. mixins.ListModelMixin,
  19. mixins.RetrieveModelMixin,
  20. mixins.UpdateModelMixin,
  21. viewsets.GenericViewSet,
  22. ):
  23. queryset = EnvironmentProject.objects.all()
  24. serializer_class = EnvironmentProjectSerializer
  25. permission_classes = [EnvironmentProjectPermission]
  26. lookup_field = "environment__name"
  27. def get_queryset(self):
  28. if not self.request.user.is_authenticated:
  29. return self.queryset.none()
  30. queryset = self.queryset.filter(
  31. environment__organization__users=self.request.user
  32. )
  33. organization_slug = self.kwargs.get("organization_slug")
  34. if organization_slug:
  35. queryset = queryset.filter(
  36. environment__organization__slug=organization_slug
  37. )
  38. project_slug = self.kwargs.get("project_slug")
  39. if project_slug:
  40. queryset = queryset.filter(project__slug=project_slug)
  41. return queryset.select_related("environment")