MCPcopy Create free account
hub / github.com/Flagsmith/flagsmith / map_environment_to_engine

Function map_environment_to_engine

api/util/mappers/engine.py:193–369  ·  view source on GitHub ↗

Maps Core API's `environments.models.Environment` model instance to the flag_engine environment document. Before building the document, takes care of resolving relationships and feature versions. :param Environment environment: the environment to map :rtype EnvironmentModel

(
    environment: "Environment",
    *,
    with_integrations: bool = True,
)

Source from the content-addressed store, hash-verified

191
192
193def map_environment_to_engine(
194 environment: "Environment",
195 *,
196 with_integrations: bool = True,
197) -> EnvironmentModel:
198 """
199 Maps Core API's `environments.models.Environment` model instance to the
200 flag_engine environment document.
201 Before building the document, takes care of resolving relationships and
202 feature versions.
203
204 :param Environment environment: the environment to map
205 :rtype EnvironmentModel
206 """
207 project: "Project" = environment.project
208 organisation: "Organisation" = project.organisation
209
210 # Read relationships - grab all the data needed from the ORM here.
211
212 project_segments = [
213 ps for ps in project.segments.all() if ps.id == ps.version_of_id
214 ]
215
216 project_segment_feature_states_by_segment_id = _get_segment_feature_states(
217 project_segments,
218 environment.pk,
219 latest_environment_feature_version_uuids=(
220 {
221 efv.uuid
222 for efv in EnvironmentFeatureVersion.objects.get_latest_versions_by_environment_id(
223 environment.id
224 )
225 }
226 if environment.use_v2_feature_versioning
227 else []
228 ),
229 )
230 # Drop feature-specific segments that have no FeatureSegment in this
231 # environment — without one, they have no evaluation path here, and
232 # their rules only inflate the environment document.
233 project_segments = [
234 ps
235 for ps in project_segments
236 if ps.feature_id is None
237 or project_segment_feature_states_by_segment_id.get(ps.pk)
238 ]
239 project_segment_rules_by_segment_id: Dict[
240 int,
241 Iterable["SegmentRule"],
242 ] = {segment.pk: segment.rules.all() for segment in project_segments}
243 environment_feature_states: List["FeatureState"] = _get_prioritised_feature_states(
244 [
245 feature_state
246 for feature_state in environment.feature_states.all()
247 if feature_state.feature_segment_id is None
248 and feature_state.identity_id is None
249 ]
250 )

Calls 13

OrganisationModelClass · 0.90
SegmentModelClass · 0.90
ProjectModelClass · 0.90
EnvironmentModelClass · 0.90
allMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…