|
| 1 | +from django.db import router |
| 2 | +from rest_framework import serializers, status |
| 3 | +from rest_framework.request import Request |
| 4 | +from rest_framework.response import Response |
| 5 | + |
| 6 | +from sentry import features |
| 7 | +from sentry.api.api_owners import ApiOwner |
| 8 | +from sentry.api.api_publish_status import ApiPublishStatus |
| 9 | +from sentry.api.base import region_silo_endpoint |
| 10 | +from sentry.api.bases.organization import OrganizationEndpoint, OrganizationPermission |
| 11 | +from sentry.insights.models import InsightsStarredSegment |
| 12 | +from sentry.models.organization import Organization |
| 13 | +from sentry.utils.db import atomic_transaction |
| 14 | + |
| 15 | + |
| 16 | +class StarSegmentSerializer(serializers.Serializer): |
| 17 | + segment_name = serializers.CharField(required=True) |
| 18 | + project_id = serializers.IntegerField(required=True) |
| 19 | + |
| 20 | + |
| 21 | +class MemberPermission(OrganizationPermission): |
| 22 | + scope_map = { |
| 23 | + "POST": ["member:read", "member:write"], |
| 24 | + "DELETE": ["member:read", "member:write"], |
| 25 | + } |
| 26 | + |
| 27 | + |
| 28 | +@region_silo_endpoint |
| 29 | +class InsightsStarredSegmentsEndpoint(OrganizationEndpoint): |
| 30 | + publish_status = { |
| 31 | + "POST": ApiPublishStatus.EXPERIMENTAL, |
| 32 | + "DELETE": ApiPublishStatus.EXPERIMENTAL, |
| 33 | + } |
| 34 | + owner = ApiOwner.PERFORMANCE |
| 35 | + permission_classes = (MemberPermission,) |
| 36 | + |
| 37 | + def has_feature(self, organization, request): |
| 38 | + return features.has( |
| 39 | + "organizations:insights-modules-use-eap", organization, actor=request.user |
| 40 | + ) |
| 41 | + |
| 42 | + def post(self, request: Request, organization: Organization) -> Response: |
| 43 | + """ |
| 44 | + Star a segment for the current organization member. |
| 45 | + """ |
| 46 | + if not self.has_feature(organization, request): |
| 47 | + return self.respond(status=404) |
| 48 | + |
| 49 | + serializer = StarSegmentSerializer(data=request.data) |
| 50 | + if not serializer.is_valid(): |
| 51 | + return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) |
| 52 | + |
| 53 | + segment_name = serializer.validated_data["segment_name"] |
| 54 | + project_id = serializer.validated_data["project_id"] |
| 55 | + with atomic_transaction(using=router.db_for_write(InsightsStarredSegment)): |
| 56 | + _, created = InsightsStarredSegment.objects.get_or_create( |
| 57 | + organization=organization, |
| 58 | + project_id=project_id, |
| 59 | + user_id=request.user.id, |
| 60 | + segment_name=segment_name, |
| 61 | + ) |
| 62 | + |
| 63 | + if not created: |
| 64 | + return Response(status=status.HTTP_403_FORBIDDEN) |
| 65 | + |
| 66 | + return Response(status=status.HTTP_200_OK) |
| 67 | + |
| 68 | + def delete(self, request: Request, organization: Organization) -> Response: |
| 69 | + """ |
| 70 | + Delete a starred segment for the current organization member. |
| 71 | + """ |
| 72 | + if not self.has_feature(organization, request): |
| 73 | + return self.respond(status=404) |
| 74 | + |
| 75 | + serializer = StarSegmentSerializer(data=request.data) |
| 76 | + if not serializer.is_valid(): |
| 77 | + return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) |
| 78 | + |
| 79 | + segment_name = serializer.validated_data["segment_name"] |
| 80 | + project_id = serializer.validated_data["project_id"] |
| 81 | + |
| 82 | + InsightsStarredSegment.objects.filter( |
| 83 | + organization=organization, |
| 84 | + user_id=request.user.id, |
| 85 | + project_id=project_id, |
| 86 | + segment_name=segment_name, |
| 87 | + ).delete() |
| 88 | + |
| 89 | + return Response(status=status.HTTP_200_OK) |
0 commit comments