views.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. from rest_framework import viewsets, mixins
  2. from .models import Environment, EnvironmentProject
  3. from .serializers import EnvironmentSerializer, EnvironmentProjectSerializer
  4. from .permissions import EnvironmentPermission, EnvironmentProjectPermission
  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")