| 8 | |
| 9 | |
| 10 | class Trait(models.Model): |
| 11 | TRAIT_VALUE_TYPES = ( |
| 12 | (INTEGER, "Integer"), |
| 13 | (STRING, "String"), |
| 14 | (BOOLEAN, "Boolean"), |
| 15 | (FLOAT, "Float"), |
| 16 | ) |
| 17 | |
| 18 | # list of fields that should be updated when using bulk update (e.g. in Identity.update_traits()) |
| 19 | BULK_UPDATE_FIELDS = [ |
| 20 | "value_type", |
| 21 | "string_value", |
| 22 | "integer_value", |
| 23 | "float_value", |
| 24 | "boolean_value", |
| 25 | ] |
| 26 | |
| 27 | identity = models.ForeignKey( |
| 28 | "identities.Identity", related_name="identity_traits", on_delete=models.CASCADE |
| 29 | ) |
| 30 | trait_key = models.CharField(max_length=200) |
| 31 | value_type = models.CharField( |
| 32 | max_length=10, choices=TRAIT_VALUE_TYPES, default=STRING, null=True, blank=True |
| 33 | ) |
| 34 | boolean_value = models.BooleanField(null=True, blank=True) |
| 35 | integer_value = models.IntegerField(null=True, blank=True) |
| 36 | string_value = models.CharField(null=True, max_length=2000, blank=True) |
| 37 | float_value = models.FloatField(null=True, blank=True) |
| 38 | |
| 39 | created_date = models.DateTimeField("DateCreated", auto_now_add=True) |
| 40 | |
| 41 | class Meta: |
| 42 | verbose_name_plural = "User Traits" |
| 43 | unique_together = ("trait_key", "identity") |
| 44 | ordering = ["id"] |
| 45 | # hard code the table name after moving from the environments app to prevent |
| 46 | # issues with production deployment due to multi server configuration. |
| 47 | db_table = "environments_trait" |
| 48 | |
| 49 | def natural_key(self): # type: ignore[no-untyped-def] |
| 50 | return ( |
| 51 | self.trait_key, |
| 52 | self.identity.identifier, |
| 53 | self.identity.environment.api_key, |
| 54 | ) |
| 55 | |
| 56 | @property |
| 57 | def trait_value(self): # type: ignore[no-untyped-def] |
| 58 | return self.get_trait_value() # type: ignore[no-untyped-call] |
| 59 | |
| 60 | @property |
| 61 | def transient(self) -> bool: |
| 62 | return getattr(self, "_transient", False) |
| 63 | |
| 64 | @transient.setter |
| 65 | def transient(self, transient: bool) -> None: |
| 66 | self._transient = transient |
| 67 |
no outgoing calls
searching dependent graphs…