views.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 = ["-avg_duration"]
  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 = Project.objects.filter(team__members__user=self.request.user)
  25. if "organization_slug" in self.kwargs:
  26. projects = projects.filter(
  27. organization__slug=self.kwargs["organization_slug"],
  28. )
  29. qs = (
  30. super()
  31. .get_queryset()
  32. .filter(project__pk__in=list(projects.values_list("pk", flat=True)))
  33. .defer("search_vector", "created")
  34. )
  35. return qs
  36. class TransactionViewSet(viewsets.ReadOnlyModelViewSet):
  37. queryset = TransactionEvent.objects.all()
  38. serializer_class = TransactionSerializer
  39. def get_serializer_class(self):
  40. if self.action in ["retrieve"]:
  41. return TransactionDetailSerializer
  42. return super().get_serializer_class()
  43. def get_queryset(self):
  44. if not self.request.user.is_authenticated:
  45. return self.queryset.none()
  46. # Performance optimization, force two queries
  47. projects = list(
  48. Project.objects.filter(team__members__user=self.request.user).values_list(
  49. "pk", flat=True
  50. )
  51. )
  52. qs = super().get_queryset().filter(group__project__pk__in=projects)
  53. if "organization_slug" in self.kwargs:
  54. qs = qs.filter(
  55. group__project__organization__slug=self.kwargs["organization_slug"],
  56. )
  57. qs = qs.select_related("group")
  58. return qs
  59. class SpanViewSet(viewsets.ReadOnlyModelViewSet):
  60. queryset = Span.objects.all()
  61. serializer_class = SpanSerializer
  62. def get_queryset(self):
  63. if not self.request.user.is_authenticated:
  64. return self.queryset.none()
  65. # Performance optimization, force two queries
  66. projects = list(
  67. Project.objects.filter(team__members__user=self.request.user).values_list(
  68. "pk", flat=True
  69. )
  70. )
  71. qs = super().get_queryset().filter(transaction__group__project__pk__in=projects)
  72. if "organization_slug" in self.kwargs:
  73. qs = qs.filter(
  74. transaction__group__project__organization__slug=self.kwargs[
  75. "organization_slug"
  76. ],
  77. )
  78. return qs