class ExternalTeamSerializer(CamelSnakeModelSerializer):
    team_id = serializers.IntegerField(required=True)
    external_name = serializers.CharField(required=True)
    provider = serializers.ChoiceField(
        choices=list(EXTERNAL_PROVIDERS.values()))

    class Meta:
        model = ExternalTeam
        fields = ["team_id", "external_name", "provider"]

    def validate_provider(self, provider):
        if provider not in EXTERNAL_PROVIDERS.values():
            raise serializers.ValidationError(
                f'The provider "{provider}" is not supported. We currently accept GitHub and GitLab team identities.'
            )
        return ExternalTeam.get_provider_enum(provider)

    def create(self, validated_data):
        return ExternalTeam.objects.get_or_create(**validated_data)

    def update(self, instance, validated_data):
        if "id" in validated_data:
            validated_data.pop("id")
        for key, value in validated_data.items():
            setattr(self.instance, key, value)
        try:
            self.instance.save()
            return self.instance
        except IntegrityError:
            raise serializers.ValidationError(
                "There already exists an external team association with this external_name and provider."
            )
Esempio n. 2
0
class ExternalUserSerializer(CamelSnakeModelSerializer):
    member_id = serializers.IntegerField(required=True)
    external_name = serializers.CharField(required=True)
    provider = serializers.ChoiceField(choices=list(EXTERNAL_PROVIDERS.values()))

    class Meta:
        model = ExternalUser
        fields = ["member_id", "external_name", "provider"]

    def validate_provider(self, provider):
        if provider not in EXTERNAL_PROVIDERS.values():
            raise serializers.ValidationError(
                f'The provider "{provider}" is not supported. We currently accept Github and Gitlab user identities.'
            )
        return ExternalUser.get_provider_enum(provider)

    def validate_member_id(self, member_id):
        try:
            return OrganizationMember.objects.get(
                id=member_id, organization=self.context["organization"]
            )
        except OrganizationMember.DoesNotExists:
            raise serializers.ValidationError("This member does not exist.")

    def create(self, validated_data):
        organizationmember = validated_data.pop("member_id", None)
        return ExternalUser.objects.get_or_create(
            organizationmember=organizationmember, **validated_data
        )

    def update(self, instance, validated_data):
        if "id" in validated_data:
            validated_data.pop("id")
        for key, value in validated_data.items():
            setattr(self.instance, key, value)
        try:
            self.instance.save()
            return self.instance
        except IntegrityError:
            raise serializers.ValidationError(
                "There already exists an external user association with this external_name and provider."
            )
Esempio n. 3
0
 def validate_provider(self, provider):
     if provider not in EXTERNAL_PROVIDERS.values():
         raise serializers.ValidationError(
             f'The provider "{provider}" is not supported. We currently accept Github and Gitlab user identities.'
         )
     return ExternalUser.get_provider_enum(provider)
Esempio n. 4
0
 def get_provider_string(self, provider):
     return EXTERNAL_PROVIDERS.get(ExternalProviders(provider), "unknown")