2017-12-29 14:56:47 +00:00
|
|
|
from django.db import models
|
|
|
|
from django.utils import timezone
|
|
|
|
from django.utils.translation import ugettext_lazy as _
|
|
|
|
|
|
|
|
|
|
|
|
class Disableable(models.Model):
|
|
|
|
"""
|
|
|
|
Mixin for models that can be disabled with a specified reason.
|
|
|
|
"""
|
|
|
|
disabled = models.BooleanField(_('disabled state'), default=False)
|
|
|
|
disabled_date = models.DateTimeField(_('disabled on'),
|
|
|
|
default=None,
|
|
|
|
blank=True,
|
|
|
|
null=True)
|
|
|
|
disabled_reason = models.TextField(_('reason for disabling'), blank=True)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
abstract = True
|
|
|
|
|
|
|
|
def disable(self, reason=''):
|
|
|
|
self.disabled = True
|
|
|
|
self.disabled_date = timezone.now()
|
|
|
|
self.disabled_reason = reason
|
|
|
|
self.save()
|
|
|
|
|
|
|
|
def enable(self):
|
|
|
|
self.disabled = False
|
|
|
|
self.disabled_date = None
|
|
|
|
self.disabled_reason = ''
|
|
|
|
self.save()
|
|
|
|
|
2018-01-04 21:28:59 +00:00
|
|
|
def _is_enabled(self):
|
|
|
|
return not self.disabled
|
|
|
|
_is_enabled.boolean = True
|
|
|
|
is_enabled = property(_is_enabled)
|
|
|
|
|
2017-12-29 14:56:47 +00:00
|
|
|
|
|
|
|
class Publishable(models.Model):
|
|
|
|
"""
|
|
|
|
Mixin for models that can be published to restrict accessibility before an
|
|
|
|
appointed date/time.
|
|
|
|
"""
|
|
|
|
published_date = models.DateTimeField(_('published for listening'),
|
|
|
|
default=None,
|
|
|
|
blank=True,
|
|
|
|
null=True)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
abstract = True
|
|
|
|
|
|
|
|
def publish(self, date=None):
|
|
|
|
if date is None:
|
|
|
|
date = timezone.now()
|
|
|
|
self.published_date = date
|
|
|
|
self.save()
|
|
|
|
|
2018-01-04 21:28:59 +00:00
|
|
|
def _is_published(self):
|
2017-12-29 14:56:47 +00:00
|
|
|
if self.published_date is not None:
|
|
|
|
return self.published_date < timezone.now()
|
|
|
|
return False
|
2018-01-04 21:28:59 +00:00
|
|
|
_is_published.boolean = True
|
|
|
|
is_published = property(_is_published)
|