123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- from rest_framework import serializers
- from organizations_ext.serializers.base_serializers import (
- OrganizationReferenceSerializer,
- )
- from teams.serializers import RelatedTeamSerializer
- from ..models import ProjectKey
- from .base_serializers import ProjectReferenceSerializer
- class ProjectKeySerializer(serializers.ModelSerializer):
- dateCreated = serializers.DateTimeField(source="created", read_only=True)
- id = serializers.CharField(source="public_key_hex", read_only=True)
- dsn = serializers.SerializerMethodField(read_only=True)
- public = serializers.CharField(source="public_key_hex", read_only=True)
- projectId = serializers.PrimaryKeyRelatedField(source="project", read_only=True)
- class Meta:
- model = ProjectKey
- fields = ("dateCreated", "dsn", "id", "label", "public", "projectId")
- def get_dsn(self, obj):
- return {
- "public": obj.get_dsn(),
- "secret": obj.get_dsn(), # Deprecated but required for @sentry/wizard
- "security": obj.get_dsn_security(),
- }
- class BaseProjectSerializer(ProjectReferenceSerializer):
- avatar = serializers.SerializerMethodField()
- color = serializers.SerializerMethodField()
- dateCreated = serializers.DateTimeField(source="created", read_only=True)
- features = serializers.SerializerMethodField()
- firstEvent = serializers.DateTimeField(source="first_event", read_only=True)
- hasAccess = serializers.SerializerMethodField()
- id = serializers.CharField(read_only=True)
- isBookmarked = serializers.SerializerMethodField()
- isInternal = serializers.SerializerMethodField()
- isMember = serializers.SerializerMethodField()
- isPublic = serializers.SerializerMethodField()
- scrubIPAddresses = serializers.BooleanField(
- source="scrub_ip_addresses", required=False
- )
- eventThrottleRate = serializers.IntegerField(
- source="event_throttle_rate",
- min_value=0,
- max_value=100,
- required=False,
- ) # GlitchTip field, not in Sentry OSS
- class Meta(ProjectReferenceSerializer.Meta):
- fields = (
- "avatar",
- "color",
- "features",
- "firstEvent",
- "hasAccess",
- "id",
- "isBookmarked",
- "isInternal",
- "isMember",
- "isPublic",
- "name",
- "scrubIPAddresses",
- "slug",
- "dateCreated",
- "platform",
- "eventThrottleRate",
- )
- read_only_fields = ("slug", "date_added")
- def get_avatar(self, obj):
- return {"avatarType": "", "avatarUuid": None}
- def get_color(self, obj):
- return ""
- def get_features(self, obj):
- return []
- def get_hasAccess(self, obj):
- return True
- def get_isBookmarked(self, obj):
- return False
- def get_isInternal(self, obj):
- return False
- def get_isMember(self, obj):
- user_id = self.context["request"].user.id
- teams = obj.team_set.all()
- # This is actually more performant than:
- # return obj.team_set.filter(members=user).exists()
- for team in teams:
- if user_id in team.members.all().values_list("user_id", flat=True):
- return True
- return False
- def get_isPublic(self, obj):
- return False
- class ProjectSerializer(BaseProjectSerializer):
- organization = OrganizationReferenceSerializer(read_only=True)
- class Meta(BaseProjectSerializer.Meta):
- fields = BaseProjectSerializer.Meta.fields + ("organization",)
- class ProjectDetailSerializer(ProjectSerializer):
- teams = RelatedTeamSerializer(source="team_set", read_only=True, many=True)
- class Meta(ProjectSerializer.Meta):
- fields = ProjectSerializer.Meta.fields + ("teams",)
- class OrganizationProjectSerializer(BaseProjectSerializer):
- teams = RelatedTeamSerializer(source="team_set", read_only=True, many=True)
- class Meta(BaseProjectSerializer.Meta):
- fields = BaseProjectSerializer.Meta.fields + ("teams",)
- class ProjectWithKeysSerializer(ProjectSerializer):
- keys = ProjectKeySerializer(many=True, source="projectkey_set")
- class Meta(ProjectSerializer.Meta):
- fields = ProjectSerializer.Meta.fields + ("keys",)
|