class Sample(BaseModel): """A sample is true some percentage of the time, but is not connected to users or requests. """ name = models.CharField(max_length=100, unique=True, help_text='The human/computer readable name.') percent = models.DecimalField(max_digits=4, decimal_places=1, help_text=( 'A number between 0.0 and 100.0 to indicate a percentage of the time ' 'this sample will be active.')) note = models.TextField(blank=True, help_text=( 'Note where this Sample is used.')) created = models.DateTimeField(default=datetime.now, db_index=True, help_text=('Date when this Sample was created.')) modified = models.DateTimeField(default=datetime.now, help_text=( 'Date when this Sample was last modified.')) objects = managers.SampleManager() SINGLE_CACHE_KEY = 'SAMPLE_CACHE_KEY' ALL_CACHE_KEY = 'ALL_SAMPLES_CACHE_KEY' def is_active(self): if not self.pk: return get_setting('SAMPLE_DEFAULT') return Decimal(str(random.uniform(0, 100))) <= self.percent
class Sample(BaseModel): """A sample of users. A sample is true some percentage of the time, but is not connected to users or requests. """ name = models.CharField( max_length=100, unique=True, help_text=_('The human/computer readable name.'), verbose_name=_('Name'), ) percent = models.DecimalField( max_digits=4, decimal_places=1, help_text=_( 'A number between 0.0 and 100.0 to indicate a percentage of the time ' 'this sample will be active.'), verbose_name=_('Percent'), ) note = models.TextField( blank=True, help_text=_('Note where this Sample is used.'), verbose_name=_('Note'), ) created = models.DateTimeField( default=timezone.now, db_index=True, help_text=_('Date when this Sample was created.'), verbose_name=_('Created'), ) modified = models.DateTimeField( default=timezone.now, help_text=_('Date when this Sample was last modified.'), verbose_name=_('Modified'), ) objects = managers.SampleManager() SINGLE_CACHE_KEY = 'SAMPLE_CACHE_KEY' ALL_CACHE_KEY = 'ALL_SAMPLES_CACHE_KEY' class Meta: verbose_name = _('Sample') verbose_name_plural = _('Samples') def is_active(self): if not self.pk: if get_setting('CREATE_MISSING_SAMPLES'): default_percent = 100 if get_setting('SAMPLE_DEFAULT') else 0 Sample.objects.get_or_create( name=self.name, defaults={'percent': default_percent}) return get_setting('SAMPLE_DEFAULT') return Decimal(str(random.uniform(0, 100))) <= self.percent