def get_persisted_identity_and_traits(
    environment: Environment,
    identifier: str,
    sdk_trait_data: list[SDKTraitData],
) -> IdentityAndTraits:
    """
    Retrieve a previously persisted `Identity` instance or persist a new one.
    Traits are persisted based on the organisation-level setting or a
    `"transient"` attribute provided with each individual trait.
    """
    identity, created = Identity.objects.get_or_create(
        environment=environment,
        identifier=identifier,
    )
    persist_trait_data = environment.project.organisation.persist_trait_data
    if created:
        return identity, identity.generate_traits(
            sdk_trait_data,
            persist=persist_trait_data,
        )
    if persist_trait_data:
        return identity, identity.update_traits(sdk_trait_data)
    return identity, list(
        {
            trait.trait_key: trait
            for trait in chain(
                identity.identity_traits.all(),
                identity.generate_traits(sdk_trait_data, persist=False),
            )
        }.values()
    )

    Retrieve a previously persisted `Identity` instance or persist a new one.
    Traits are persisted based on the organisation-level setting or a
    `"transient"` attribute provided with each individual trait.
    
api/environments/sdk/services.py