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

Method update_traits

api/environments/identities/models.py:217–305  ·  view source on GitHub ↗

Given a list of traits, update any that already exist and create any new ones. Return the full list of traits for the given identity after these changes. :param trait_data_items: list of dictionaries validated by TraitSerializerFull :return: queryset of updated trai

(
        self,
        trait_data_items: list[SDKTraitData],
    )

Source from the content-addressed store, hash-verified

215 return trait_models
216
217 def update_traits(
218 self,
219 trait_data_items: list[SDKTraitData],
220 ) -> list[Trait]:
221 """
222 Given a list of traits, update any that already exist and create any new ones.
223 Return the full list of traits for the given identity after these changes.
224
225 :param trait_data_items: list of dictionaries validated by TraitSerializerFull
226 :return: queryset of updated trait models
227 """
228 current_traits = {t.trait_key: t for t in self.identity_traits.all()}
229
230 keys_to_delete = set()
231 new_traits = []
232 updated_traits = []
233 transient_traits = []
234
235 for trait_data_item in trait_data_items:
236 trait_key = trait_data_item["trait_key"]
237 trait_value = trait_data_item["trait_value"]
238 transient = trait_data_item.get("transient")
239
240 if transient:
241 trait = Trait(
242 **Trait.generate_trait_value_data(trait_value),
243 trait_key=trait_key,
244 identity=self,
245 )
246 trait.transient = True
247 transient_traits.append(trait)
248 continue
249
250 if trait_value is None:
251 # build a list of trait keys to delete having been nulled by the
252 # input data
253 keys_to_delete.add(trait_key)
254 continue
255
256 if trait_key in current_traits:
257 current_trait = current_traits[trait_key]
258 # Don't update the trait if the value hasn't changed
259 if current_trait.trait_value == trait_value:
260 continue
261
262 for attr, value in Trait.generate_trait_value_data(trait_value).items():
263 setattr(current_trait, attr, value)
264 updated_traits.append(current_trait)
265 continue
266
267 new_traits.append(
268 Trait(
269 **Trait.generate_trait_value_data(trait_value),
270 trait_key=trait_key,
271 identity=self,
272 )
273 )
274

Calls 8

TraitClass · 0.90
bulk_createMethod · 0.80
setFunction · 0.50
allMethod · 0.45
getMethod · 0.45
appendMethod · 0.45
deleteMethod · 0.45