(
identity: "Identity",
*,
with_overrides: bool = True,
with_traits: bool = True,
)
| 386 | |
| 387 | |
| 388 | def map_identity_to_engine( |
| 389 | identity: "Identity", |
| 390 | *, |
| 391 | with_overrides: bool = True, |
| 392 | with_traits: bool = True, |
| 393 | ) -> IdentityModel: |
| 394 | environment_api_key = identity.environment.api_key |
| 395 | |
| 396 | # Read relationships - grab all the data needed from the ORM here. |
| 397 | if with_overrides: |
| 398 | identity_feature_states: List["FeatureState"] = _get_prioritised_feature_states( |
| 399 | identity.identity_features.all(), |
| 400 | ) |
| 401 | multivariate_feature_state_values_by_feature_state_id = { |
| 402 | feature_state.pk: feature_state.multivariate_feature_state_values.all() |
| 403 | for feature_state in identity_feature_states |
| 404 | } |
| 405 | else: |
| 406 | identity_feature_states = [] |
| 407 | multivariate_feature_state_values_by_feature_state_id = {} |
| 408 | |
| 409 | identity_traits: Iterable["Trait"] = ( |
| 410 | identity.identity_traits.all() if with_traits else [] |
| 411 | ) |
| 412 | |
| 413 | # Prepare relationships. |
| 414 | identity_feature_state_models = [ |
| 415 | map_feature_state_to_engine( |
| 416 | feature_state, |
| 417 | mv_fs_values=multivariate_feature_state_values_by_feature_state_id.pop( |
| 418 | feature_state.pk, |
| 419 | ), |
| 420 | ) |
| 421 | for feature_state in identity_feature_states |
| 422 | ] |
| 423 | identity_trait_models = map_traits_to_engine(identity_traits) |
| 424 | |
| 425 | return IdentityModel( |
| 426 | # Attributes: |
| 427 | identifier=identity.identifier, |
| 428 | environment_api_key=environment_api_key, |
| 429 | created_date=identity.created_date, |
| 430 | django_id=identity.pk, |
| 431 | # |
| 432 | # Relationships: |
| 433 | identity_features=identity_feature_state_models, # type: ignore[arg-type] |
| 434 | identity_traits=identity_trait_models, |
| 435 | ) |
| 436 | |
| 437 | |
| 438 | _rule_type_adapter: TypeAdapter[RuleType] = TypeAdapter(RuleType) |
searching dependent graphs…