views.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. from rest_framework import viewsets
  2. from rest_framework.filters import OrderingFilter
  3. from django_filters.rest_framework import DjangoFilterBackend
  4. from projects.models import Project
  5. from .models import TransactionEvent, TransactionGroup, Span
  6. from .serializers import (
  7. TransactionSerializer,
  8. TransactionDetailSerializer,
  9. TransactionGroupSerializer,
  10. SpanSerializer,
  11. )
  12. from .filters import TransactionGroupFilter
  13. class TransactionGroupViewSet(viewsets.ReadOnlyModelViewSet):
  14. queryset = TransactionGroup.objects.all()
  15. serializer_class = TransactionGroupSerializer
  16. filter_backends = [DjangoFilterBackend, OrderingFilter]
  17. filterset_class = TransactionGroupFilter
  18. ordering = ["-created"]
  19. ordering_fields = ["created", "avg_duration", "transaction_count"]
  20. def get_queryset(self):
  21. if not self.request.user.is_authenticated:
  22. return self.queryset.none()
  23. # Performance optimization, force two queries
  24. projects = list(
  25. Project.objects.filter(team__members__user=self.request.user).values_list(
  26. "pk", flat=True
  27. )
  28. )
  29. qs = super().get_queryset().filter(project__pk__in=projects)
  30. if "organization_slug" in self.kwargs:
  31. qs = qs.filter(
  32. project__organization__slug=self.kwargs["organization_slug"],
  33. )
  34. return qs
  35. class TransactionViewSet(viewsets.ReadOnlyModelViewSet):
  36. queryset = TransactionEvent.objects.all()
  37. serializer_class = TransactionSerializer
  38. def get_serializer_class(self):
  39. if self.action in ["retrieve"]:
  40. return TransactionDetailSerializer
  41. return super().get_serializer_class()
  42. def get_queryset(self):
  43. if not self.request.user.is_authenticated:
  44. return self.queryset.none()
  45. # Performance optimization, force two queries
  46. projects = list(
  47. Project.objects.filter(team__members__user=self.request.user).values_list(
  48. "pk", flat=True
  49. )
  50. )
  51. qs = super().get_queryset().filter(group__project__pk__in=projects)
  52. if "organization_slug" in self.kwargs:
  53. qs = qs.filter(
  54. group__project__organization__slug=self.kwargs["organization_slug"],
  55. )
  56. qs = qs.select_related("group")
  57. return qs
  58. class SpanViewSet(viewsets.ReadOnlyModelViewSet):
  59. queryset = Span.objects.all()
  60. serializer_class = SpanSerializer
  61. def get_queryset(self):
  62. if not self.request.user.is_authenticated:
  63. return self.queryset.none()
  64. # Performance optimization, force two queries
  65. projects = list(
  66. Project.objects.filter(team__members__user=self.request.user).values_list(
  67. "pk", flat=True
  68. )
  69. )
  70. qs = super().get_queryset().filter(transaction__group__project__pk__in=projects)
  71. if "organization_slug" in self.kwargs:
  72. qs = qs.filter(
  73. transaction__group__project__organization__slug=self.kwargs[
  74. "organization_slug"
  75. ],
  76. )
  77. return qs