Exemplo n.º 1
0
class IdentitySerializerFull(serializers.ModelSerializer):
    identity_features = FeatureStateSerializerFull(many=True)
    environment = EnvironmentSerializerFull()

    class Meta:
        model = Identity
        fields = ("id", "identifier", "identity_features", "environment")
Exemplo n.º 2
0
class IdentitySerializerFull(serializers.ModelSerializer):
    identity_features = FeatureStateSerializerFull(many=True)
    environment = EnvironmentSerializerFull()

    class Meta:
        model = Identity
        fields = ('id', 'identifier', 'identity_features', 'environment')
Exemplo n.º 3
0
class EnvironmentSerializerFull(serializers.ModelSerializer):
    feature_states = FeatureStateSerializerFull(many=True)
    project = ProjectSerializer()

    class Meta:
        model = Environment
        fields = ('id', 'name', 'feature_states', 'project', 'api_key')
Exemplo n.º 4
0
class EnvironmentSerializerFull(serializers.ModelSerializer):
    feature_states = FeatureStateSerializerFull(many=True)
    project = ProjectSerializer()

    class Meta:
        model = Environment
        fields = ("id", "name", "feature_states", "project", "api_key")
Exemplo n.º 5
0
    def _get_all_feature_states_for_user_response(self,
                                                  identity,
                                                  trait_models=None):
        """
        Get all feature states for an identity

        :param identity: Identity model to return feature states for
        :param trait_models: optional list of trait_models to pass in for organisations that don't persist them
        :return: Response containing lists of both serialized flags and traits
        """
        all_feature_states = identity.get_all_feature_states()
        serialized_flags = FeatureStateSerializerFull(all_feature_states,
                                                      many=True)
        serialized_traits = TraitSerializerBasic(
            identity.identity_traits.all(), many=True)

        # If we have an amplitude configured, send the flags viewed by the user to their API
        if (hasattr(identity.environment, "amplitude_config")
                and identity.environment.amplitude_config.api_key):
            amplitude = AmplitudeWrapper(
                identity.environment.amplitude_config.api_key)
            user_data = amplitude.generate_user_data(
                user_id=identity.identifier, feature_states=all_feature_states)
            amplitude.identify_user_async(user_data=user_data)

        response = {
            "flags": serialized_flags.data,
            "traits": serialized_traits.data
        }

        return Response(data=response, status=status.HTTP_200_OK)
Exemplo n.º 6
0
    def _get_single_feature_state_response(self, identity, feature_name):
        for feature_state in identity.get_all_feature_states():
            if feature_state.feature.name == feature_name:
                serializer = FeatureStateSerializerFull(feature_state)
                return Response(data=serializer.data,
                                status=status.HTTP_200_OK)

        return Response({"detail": "Given feature not found"},
                        status=status.HTTP_404_NOT_FOUND)
Exemplo n.º 7
0
class IdentitySerializerWithTraitsAndSegments(serializers.Serializer):
    def update(self, instance, validated_data):
        pass

    def create(self, validated_data):
        pass

    flags = FeatureStateSerializerFull(many=True)
    traits = TraitSerializerBasic(many=True)
    segments = SegmentSerializerBasic(many=True)
Exemplo n.º 8
0
    def _get_all_feature_states_for_user_response(self, identity):
        serialized_flags = FeatureStateSerializerFull(
            identity.get_all_feature_states(), many=True)
        serialized_traits = TraitSerializerBasic(
            identity.get_all_user_traits(), many=True)

        response = {
            "flags": serialized_flags.data,
            "traits": serialized_traits.data
        }

        return Response(data=response, status=status.HTTP_200_OK)
Exemplo n.º 9
0
class IdentifyWithTraitsSerializer(serializers.Serializer):
    identifier = serializers.CharField(write_only=True, required=True)
    traits = TraitSerializerBasic(required=False, many=True)
    flags = FeatureStateSerializerFull(read_only=True, many=True)

    def create(self, validated_data):
        """
        Create the identity with the associated traits
        (optionally store traits if flag set on org)
        """
        environment = self.context["environment"]
        identity, created = Identity.objects.get_or_create(
            identifier=validated_data["identifier"], environment=environment)

        if not created and environment.project.organisation.persist_trait_data:
            # if this is an update and we're persisting traits, then we need to
            # partially update any traits and return the full list
            return self.update(instance=identity,
                               validated_data=validated_data)

        # generate traits for the identity and store them if configured to do so
        trait_models = identity.generate_traits(
            validated_data.get("traits", []),
            persist=environment.project.organisation.persist_trait_data,
        )

        return {
            "identity": identity,
            "traits": trait_models,
            "flags": identity.get_all_feature_states(traits=trait_models),
        }

    def update(self, instance, validated_data):
        """ partially update any traits and return the full list of traits and flags """
        trait_data_items = validated_data.get("traits", [])
        updated_traits = instance.update_traits(trait_data_items)

        return {
            "identity": instance,
            "traits": updated_traits,
            "flags": instance.get_all_feature_states(traits=updated_traits),
        }
Exemplo n.º 10
0
    def _get_feature_states_for_user_response(self,
                                              identity,
                                              trait_models=None,
                                              features=None):
        """
        Get all feature (or a subset of them) states for an identity

        :param identity: Identity model to return feature states for
        :param trait_models: optional list of trait_models to apply over top of any already persisted traits for the identity
        :return: Response containing lists of both serialized flags and traits
        """
        shadowed_keys = [] if trait_models is None else map(
            lambda t: t.trait_key, trait_models)

        traits = identity.identity_traits.all(
        ) if trait_models is None else list(
            identity.identity_traits.all().exclude(
                trait_key__in=shadowed_keys)) + trait_models

        if features is None:
            feature_states = identity.get_all_feature_states(traits)
        else:
            feature_states = identity.get_feature_states(features, traits)

        serialized_flags = FeatureStateSerializerFull(feature_states,
                                                      many=True)

        serialized_traits = TraitSerializerBasic(traits, many=True)

        identify_integrations(identity, feature_states)

        response = {
            "flags": serialized_flags.data,
            "traits": serialized_traits.data
        }

        return Response(data=response, status=status.HTTP_200_OK)
Exemplo n.º 11
0
    def _get_all_feature_states_for_user_response(self,
                                                  identity,
                                                  trait_models=None):
        """
        Get all feature states for an identity

        :param identity: Identity model to return feature states for
        :param trait_models: optional list of trait_models to pass in for organisations that don't persist them
        :return: Response containing lists of both serialized flags and traits
        """
        all_feature_states = identity.get_all_feature_states()
        serialized_flags = FeatureStateSerializerFull(all_feature_states,
                                                      many=True)
        serialized_traits = TraitSerializerBasic(
            identity.identity_traits.all(), many=True)

        identify_integrations(identity, all_feature_states)

        response = {
            "flags": serialized_flags.data,
            "traits": serialized_traits.data
        }

        return Response(data=response, status=status.HTTP_200_OK)
Exemplo n.º 12
0
class IdentitySerializerTraitFlags(serializers.Serializer):
    flags = FeatureStateSerializerFull(many=True)
    traits = TraitSerializerBasic(many=True)