2013-04-06 22:55:42 +00:00
|
|
|
from __future__ import unicode_literals
|
2010-04-15 02:53:55 +00:00
|
|
|
|
2015-10-28 12:47:33 +00:00
|
|
|
import django
|
2016-11-17 08:56:59 +00:00
|
|
|
from django.core.exceptions import ImproperlyConfigured
|
2009-07-02 18:03:02 +00:00
|
|
|
from django.db import models
|
2010-04-15 02:49:51 +00:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2015-10-28 12:47:33 +00:00
|
|
|
if django.VERSION >= (1, 9, 0):
|
|
|
|
|
from django.db.models.functions import Now
|
|
|
|
|
now = Now()
|
|
|
|
|
else:
|
|
|
|
|
from django.utils.timezone import now
|
2009-07-02 18:03:02 +00:00
|
|
|
|
2016-09-12 11:50:03 +00:00
|
|
|
from model_utils.managers import QueryManager, SoftDeletableManager
|
2010-04-15 02:53:55 +00:00
|
|
|
from model_utils.fields import AutoCreatedField, AutoLastModifiedField, \
|
2010-04-16 03:47:28 +00:00
|
|
|
StatusField, MonitorField
|
2009-07-02 20:14:29 +00:00
|
|
|
|
|
|
|
|
|
2010-04-15 02:43:27 +00:00
|
|
|
class TimeStampedModel(models.Model):
|
2009-07-02 20:14:29 +00:00
|
|
|
"""
|
|
|
|
|
An abstract base class model that provides self-updating
|
|
|
|
|
``created`` and ``modified`` fields.
|
|
|
|
|
|
|
|
|
|
"""
|
2010-04-15 02:49:51 +00:00
|
|
|
created = AutoCreatedField(_('created'))
|
|
|
|
|
modified = AutoLastModifiedField(_('modified'))
|
2009-07-02 20:14:29 +00:00
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
|
abstract = True
|
|
|
|
|
|
2010-04-15 02:53:55 +00:00
|
|
|
|
2010-04-15 22:01:38 +00:00
|
|
|
class TimeFramedModel(models.Model):
|
2010-04-15 02:53:55 +00:00
|
|
|
"""
|
2010-04-15 22:01:38 +00:00
|
|
|
An abstract base class model that provides ``start``
|
|
|
|
|
and ``end`` fields to record a timeframe.
|
2010-04-15 02:53:55 +00:00
|
|
|
|
|
|
|
|
"""
|
2010-04-15 22:01:38 +00:00
|
|
|
start = models.DateTimeField(_('start'), null=True, blank=True)
|
|
|
|
|
end = models.DateTimeField(_('end'), null=True, blank=True)
|
|
|
|
|
|
2010-04-15 02:53:55 +00:00
|
|
|
class Meta:
|
|
|
|
|
abstract = True
|
|
|
|
|
|
2015-01-27 23:43:29 +00:00
|
|
|
|
2010-04-15 18:00:44 +00:00
|
|
|
class StatusModel(models.Model):
|
2010-04-15 02:53:55 +00:00
|
|
|
"""
|
2010-04-16 03:47:28 +00:00
|
|
|
An abstract base class model with a ``status`` field that
|
|
|
|
|
automatically uses a ``STATUS`` class attribute of choices, a
|
|
|
|
|
``status_changed`` date-time field that records when ``status``
|
|
|
|
|
was last modified, and an automatically-added manager for each
|
|
|
|
|
status that returns objects with that status only.
|
2010-04-15 02:53:55 +00:00
|
|
|
|
|
|
|
|
"""
|
2010-04-15 22:01:38 +00:00
|
|
|
status = StatusField(_('status'))
|
2010-04-16 03:47:28 +00:00
|
|
|
status_changed = MonitorField(_('status changed'), monitor='status')
|
2010-04-15 02:53:55 +00:00
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
|
abstract = True
|
|
|
|
|
|
2015-01-27 23:43:29 +00:00
|
|
|
|
2010-04-26 20:30:01 +00:00
|
|
|
def add_status_query_managers(sender, **kwargs):
|
|
|
|
|
"""
|
|
|
|
|
Add a Querymanager for each status item dynamically.
|
2010-04-27 17:25:45 +00:00
|
|
|
|
2010-04-26 20:30:01 +00:00
|
|
|
"""
|
|
|
|
|
if not issubclass(sender, StatusModel):
|
|
|
|
|
return
|
2017-01-09 19:02:59 +00:00
|
|
|
|
|
|
|
|
if django.VERSION >= (1, 10):
|
|
|
|
|
# First, get current manager name...
|
|
|
|
|
default_manager = sender._meta.default_manager
|
|
|
|
|
|
2014-01-06 23:03:44 +00:00
|
|
|
for value, display in getattr(sender, 'STATUS', ()):
|
2015-01-27 23:43:29 +00:00
|
|
|
if _field_exists(sender, value):
|
|
|
|
|
raise ImproperlyConfigured(
|
|
|
|
|
"StatusModel: Model '%s' has a field named '%s' which "
|
|
|
|
|
"conflicts with a status of the same name."
|
|
|
|
|
% (sender.__name__, value)
|
|
|
|
|
)
|
2010-04-26 20:30:01 +00:00
|
|
|
sender.add_to_class(value, QueryManager(status=value))
|
|
|
|
|
|
2017-01-09 19:02:59 +00:00
|
|
|
if django.VERSION >= (1, 10):
|
|
|
|
|
# ...then, put it back, as add_to_class is modifying the default manager!
|
|
|
|
|
sender._meta.default_manager_name = default_manager.name
|
|
|
|
|
|
2015-01-27 23:43:29 +00:00
|
|
|
|
2010-04-26 20:30:01 +00:00
|
|
|
def add_timeframed_query_manager(sender, **kwargs):
|
|
|
|
|
"""
|
2010-04-27 17:25:45 +00:00
|
|
|
Add a QueryManager for a specific timeframe.
|
|
|
|
|
|
2010-04-26 20:30:01 +00:00
|
|
|
"""
|
|
|
|
|
if not issubclass(sender, TimeFramedModel):
|
|
|
|
|
return
|
2015-01-27 23:43:29 +00:00
|
|
|
if _field_exists(sender, 'timeframed'):
|
|
|
|
|
raise ImproperlyConfigured(
|
|
|
|
|
"Model '%s' has a field named 'timeframed' "
|
|
|
|
|
"which conflicts with the TimeFramedModel manager."
|
|
|
|
|
% sender.__name__
|
|
|
|
|
)
|
2010-04-26 20:30:01 +00:00
|
|
|
sender.add_to_class('timeframed', QueryManager(
|
2012-01-07 22:43:39 +00:00
|
|
|
(models.Q(start__lte=now) | models.Q(start__isnull=True)) &
|
|
|
|
|
(models.Q(end__gte=now) | models.Q(end__isnull=True))
|
2010-04-26 20:30:01 +00:00
|
|
|
))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
models.signals.class_prepared.connect(add_status_query_managers)
|
|
|
|
|
models.signals.class_prepared.connect(add_timeframed_query_manager)
|
2015-01-27 23:43:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def _field_exists(model_class, field_name):
|
2015-01-28 19:17:34 +00:00
|
|
|
return field_name in [f.attname for f in model_class._meta.local_fields]
|
2016-09-12 11:50:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class SoftDeletableModel(models.Model):
|
|
|
|
|
"""
|
|
|
|
|
An abstract base class model with a ``is_removed`` field that
|
|
|
|
|
marks entries that are not going to be used anymore, but are
|
|
|
|
|
kept in db for any reason.
|
|
|
|
|
Default manager returns only not-removed entries.
|
|
|
|
|
"""
|
|
|
|
|
is_removed = models.BooleanField(default=False)
|
|
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
|
abstract = True
|
|
|
|
|
|
|
|
|
|
objects = SoftDeletableManager()
|
|
|
|
|
|
2016-11-28 23:45:48 +00:00
|
|
|
def delete(self, using=None, soft=True, *args, **kwargs):
|
2016-09-12 11:50:03 +00:00
|
|
|
"""
|
2016-11-28 23:45:48 +00:00
|
|
|
Soft delete object (set its ``is_removed`` field to True).
|
|
|
|
|
Actually delete object if setting ``soft`` to False.
|
2016-09-12 11:50:03 +00:00
|
|
|
"""
|
2016-11-28 23:45:48 +00:00
|
|
|
if soft:
|
|
|
|
|
self.is_removed = True
|
|
|
|
|
self.save(using=using)
|
|
|
|
|
else:
|
|
|
|
|
return super(SoftDeletableModel, self).delete(using=using, *args, **kwargs)
|