Пример #1
0
class TraitSerializerBasic(serializers.ModelSerializer):
    trait_value = TraitValueField()

    class Meta:
        model = Trait
        fields = ("id", "trait_key", "trait_value")
        read_only_fields = ("id",)
Пример #2
0
class SDKCreateUpdateTraitSerializer(serializers.ModelSerializer):
    identity = IdentifierOnlyIdentitySerializer()
    trait_value = TraitValueField()
    trait_key = serializers.CharField()

    class Meta:
        model = Trait
        fields = ('identity', 'trait_value', 'trait_key')

    def create(self, validated_data):
        identity = self._get_identity(validated_data['identity']['identifier'])

        trait_key = validated_data['trait_key']
        trait_value = validated_data['trait_value']['value']
        trait_value_type = validated_data['trait_value']['type']

        value_key = Trait.get_trait_value_key_name(trait_value_type)

        defaults = {
            value_key:
            trait_value,
            'value_type':
            trait_value_type
            if trait_value_type in [FLOAT, INTEGER, BOOLEAN] else STRING
        }

        return Trait.objects.update_or_create(identity=identity,
                                              trait_key=trait_key,
                                              defaults=defaults)[0]

    def _get_identity(self, identifier):
        return Identity.objects.get_or_create(
            identifier=identifier, environment=self.context['environment'])[0]
Пример #3
0
class SDKCreateUpdateTraitSerializer(serializers.ModelSerializer):
    identity = IdentifierOnlyIdentitySerializer()
    trait_value = TraitValueField()
    trait_key = serializers.CharField()

    class Meta:
        model = Trait
        fields = ("identity", "trait_value", "trait_key")

    def create(self, validated_data):
        identity = self._get_identity(validated_data["identity"]["identifier"])

        trait_key = validated_data["trait_key"]
        trait_value = validated_data["trait_value"]["value"]
        trait_value_type = validated_data["trait_value"]["type"]

        value_key = Trait.get_trait_value_key_name(trait_value_type)

        defaults = {
            value_key:
            trait_value,
            "value_type":
            trait_value_type
            if trait_value_type in [FLOAT, INTEGER, BOOLEAN] else STRING,
        }

        return Trait.objects.update_or_create(identity=identity,
                                              trait_key=trait_key,
                                              defaults=defaults)[0]

    def _get_identity(self, identifier):
        return Identity.objects.get_or_create(
            identifier=identifier, environment=self.context["environment"])[0]
Пример #4
0
class SDKBulkCreateUpdateTraitSerializer(SDKCreateUpdateTraitSerializer):
    trait_value = TraitValueField(allow_null=True)