class Switch(BaseModel): """A feature switch. Switches are active, or inactive, globally. """ name = models.CharField(max_length=100, unique=True, help_text='The human/computer readable name.') active = models.BooleanField(default=False, help_text=('Is this switch active?')) note = models.TextField(blank=True, help_text=('Note where this Switch is used.')) created = models.DateTimeField( default=datetime.now, db_index=True, help_text=('Date when this Switch was created.')) modified = models.DateTimeField( default=datetime.now, help_text=('Date when this Switch was last modified.')) objects = managers.SwitchManager() SINGLE_CACHE_KEY = 'SWITCH_CACHE_KEY' ALL_CACHE_KEY = 'ALL_SWITCHES_CACHE_KEY' class Meta: verbose_name_plural = 'Switches' def is_active(self): if not self.pk: return get_setting('SWITCH_DEFAULT') return self.active
class Switch(BaseModel): """A feature switch. Switches are active, or inactive, globally. """ name = models.CharField( max_length=100, unique=True, help_text=_('The human/computer readable name.'), verbose_name=_('Name'), ) active = models.BooleanField( default=False, help_text=_('Is this switch active?'), verbose_name=_('Active'), ) note = models.TextField( blank=True, help_text=_('Note where this Switch is used.'), verbose_name=_('Note'), ) created = models.DateTimeField( default=timezone.now, db_index=True, help_text=_('Date when this Switch was created.'), verbose_name=_('Created'), ) modified = models.DateTimeField( default=timezone.now, help_text=_('Date when this Switch was last modified.'), verbose_name=_('Modified'), ) objects = managers.SwitchManager() SINGLE_CACHE_KEY = 'SWITCH_CACHE_KEY' ALL_CACHE_KEY = 'ALL_SWITCHES_CACHE_KEY' class Meta: verbose_name = _('Switch') verbose_name_plural = _('Switches') def is_active(self): if not self.pk: log_level = get_setting('LOG_MISSING_SWITCHES') if log_level: logger.log(log_level, 'Switch %s not found', self.name) if get_setting('CREATE_MISSING_SWITCHES'): switch, _created = Switch.objects.get_or_create( name=self.name, defaults={'active': get_setting('SWITCH_DEFAULT')}) cache = get_cache() cache.set(self._cache_key(self.name), switch) return get_setting('SWITCH_DEFAULT') return self.active