views.py 918 B

12345678910111213141516171819202122232425
  1. from rest_framework import viewsets
  2. from projects.models import Project
  3. from .models import TransactionEvent
  4. from .serializers import TransactionSerializer
  5. class TransactionViewSet(viewsets.ReadOnlyModelViewSet):
  6. queryset = TransactionEvent.objects.all()
  7. serializer_class = TransactionSerializer
  8. def get_queryset(self):
  9. if not self.request.user.is_authenticated:
  10. return self.queryset.none()
  11. # Performance optimization, override ORM to force two queries
  12. projects = list(
  13. Project.objects.filter(team__members__user=self.request.user).values_list(
  14. "pk", flat=True
  15. )
  16. )
  17. qs = super().get_queryset().filter(project__pk__in=projects)
  18. if "organization_slug" in self.kwargs:
  19. qs = qs.filter(
  20. project__organization__slug=self.kwargs["organization_slug"],
  21. )
  22. return qs