MCPcopy Index your code
hub / github.com/Flagsmith/flagsmith / get_segments

Method get_segments

api/environments/identities/models.py:138–170  ·  view source on GitHub ↗

Get the list of segments this identity is a part of. :param traits: override the identity's traits when evaluating segments :param overrides_only: only retrieve the segments which have a valid override in the environment :return: List of matching segments

(
        self,
        traits: list[Trait] | None = None,
        overrides_only: bool = False,
    )

Source from the content-addressed store, hash-verified

136 return {fs.feature_id: fs for fs in self.identity_features.all()}
137
138 def get_segments(
139 self,
140 traits: list[Trait] | None = None,
141 overrides_only: bool = False,
142 ) -> list[Segment]:
143 """
144 Get the list of segments this identity is a part of.
145
146 :param traits: override the identity's traits when evaluating segments
147 :param overrides_only: only retrieve the segments which have a valid override in the environment
148 :return: List of matching segments
149 """
150 db_traits = (
151 self.identity_traits.all() if (traits is None and self.id) else traits or []
152 )
153
154 if overrides_only:
155 all_segments = self.environment.get_segments_from_cache()
156 else:
157 all_segments = self.environment.project.get_segments_from_cache()
158
159 segments_by_pk = {segment.pk: segment for segment in all_segments}
160 context = map_environment_to_evaluation_context(
161 identity=self,
162 environment=self.environment,
163 traits=db_traits,
164 segments=all_segments,
165 )
166 result = get_evaluation_result(context)
167 return [
168 segments_by_pk[segment_result["metadata"]["pk"]]
169 for segment_result in result["segments"]
170 ]
171
172 def get_all_user_traits(self): # type: ignore[no-untyped-def]
173 # this is pointless, we should probably replace all uses with the below code

Calls 3

allMethod · 0.45