2010-04-15 02:53:55 +00:00
|
|
|
from datetime import datetime
|
|
|
|
|
|
2009-07-02 18:03:02 +00:00
|
|
|
from django.db import models
|
2010-04-15 02:53:55 +00:00
|
|
|
from django.db.models.base import ModelBase
|
2009-07-02 18:03:02 +00:00
|
|
|
from django.contrib.contenttypes.models import ContentType
|
2010-04-15 02:49:51 +00:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2010-04-15 02:53:55 +00:00
|
|
|
from django.db.models.fields import FieldDoesNotExist
|
2009-07-02 18:03:02 +00:00
|
|
|
|
2010-04-15 02:53:55 +00:00
|
|
|
from model_utils.managers import QueryManager
|
|
|
|
|
from model_utils.fields import AutoCreatedField, AutoLastModifiedField, \
|
2010-04-15 21:59:31 +00:00
|
|
|
StatusField, StatusModifiedField
|
2009-07-02 20:14:29 +00:00
|
|
|
|
2009-07-02 18:03:02 +00:00
|
|
|
class InheritanceCastModel(models.Model):
|
|
|
|
|
"""
|
|
|
|
|
An abstract base class that provides a ``real_type`` FK to ContentType.
|
|
|
|
|
|
|
|
|
|
For use in trees of inherited models, to be able to downcast
|
|
|
|
|
parent instances to their child types.
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
real_type = models.ForeignKey(ContentType, editable=False, null=True)
|
2010-04-15 02:43:27 +00:00
|
|
|
|
2009-07-02 18:03:02 +00:00
|
|
|
def save(self, *args, **kwargs):
|
|
|
|
|
if not self.id:
|
|
|
|
|
self.real_type = self._get_real_type()
|
|
|
|
|
super(InheritanceCastModel, self).save(*args, **kwargs)
|
2010-04-15 02:43:27 +00:00
|
|
|
|
2009-07-02 18:03:02 +00:00
|
|
|
def _get_real_type(self):
|
|
|
|
|
return ContentType.objects.get_for_model(type(self))
|
2010-04-15 02:43:27 +00:00
|
|
|
|
2009-07-02 18:03:02 +00:00
|
|
|
def cast(self):
|
|
|
|
|
return self.real_type.get_object_for_this_type(pk=self.pk)
|
2010-04-15 02:43:27 +00:00
|
|
|
|
2009-07-02 18:03:02 +00:00
|
|
|
class Meta:
|
|
|
|
|
abstract = True
|
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
|
|
|
|
|
|
|
|
class TimeFramedBaseModel(ModelBase):
|
|
|
|
|
"""
|
|
|
|
|
A model base class for the ``TimeFramedModel`` that adds
|
|
|
|
|
a special model manager ``timeframed`` for time frames.
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
def _prepare(cls):
|
|
|
|
|
super(TimeFramedBaseModel, cls)._prepare()
|
|
|
|
|
try:
|
|
|
|
|
cls._meta.get_field('timeframed')
|
|
|
|
|
raise ValueError("Model %s has a field named 'timeframed' and "
|
|
|
|
|
"conflicts with a manager." % cls.__name__)
|
|
|
|
|
except FieldDoesNotExist:
|
|
|
|
|
pass
|
|
|
|
|
cls.add_to_class('timeframed', QueryManager(
|
2010-04-15 18:00:44 +00:00
|
|
|
(models.Q(start__lte=datetime.now()) | models.Q(start__isnull=True)) &
|
|
|
|
|
(models.Q(end__gte=datetime.now()) | models.Q(end__isnull=True))
|
2010-04-15 02:53:55 +00:00
|
|
|
))
|
|
|
|
|
|
|
|
|
|
class TimeFramedModel(models.Model):
|
|
|
|
|
"""
|
2010-04-15 18:00:44 +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
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
__metaclass__ = TimeFramedBaseModel
|
|
|
|
|
|
2010-04-15 18:00:44 +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
|
|
|
|
|
|
|
|
|
|
|
2010-04-15 18:00:44 +00:00
|
|
|
class StatusBaseModel(ModelBase):
|
2010-04-15 02:53:55 +00:00
|
|
|
"""
|
2010-04-15 18:00:44 +00:00
|
|
|
A model base class for the ``StatusModel`` to add
|
|
|
|
|
a series of model managers for each given status.
|
2010-04-15 02:53:55 +00:00
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
def _prepare(cls):
|
2010-04-15 18:00:44 +00:00
|
|
|
super(StatusBaseModel, cls)._prepare()
|
|
|
|
|
status = getattr(cls, 'STATUS', ())
|
|
|
|
|
if status is None:
|
2010-04-15 02:53:55 +00:00
|
|
|
return
|
2010-04-15 18:00:44 +00:00
|
|
|
for value, name in status:
|
2010-04-15 02:53:55 +00:00
|
|
|
try:
|
|
|
|
|
cls._meta.get_field(name)
|
|
|
|
|
raise ValueError("Model %s has a field named '%s' and "
|
2010-04-15 18:00:44 +00:00
|
|
|
"conflicts with a status."
|
2010-04-15 02:53:55 +00:00
|
|
|
% (cls.__name__, name))
|
|
|
|
|
except FieldDoesNotExist:
|
|
|
|
|
pass
|
2010-04-15 18:00:44 +00:00
|
|
|
cls.add_to_class(value, QueryManager(**{'status': value}))
|
2010-04-15 02:53:55 +00:00
|
|
|
|
2010-04-15 18:00:44 +00:00
|
|
|
class StatusModel(models.Model):
|
2010-04-15 02:53:55 +00:00
|
|
|
"""
|
|
|
|
|
An abstract base class model that provides self-updating
|
2010-04-15 18:00:44 +00:00
|
|
|
status fields like ``deleted`` and ``restored``.
|
2010-04-15 02:53:55 +00:00
|
|
|
|
|
|
|
|
"""
|
2010-04-15 18:00:44 +00:00
|
|
|
__metaclass__ = StatusBaseModel
|
2010-04-15 21:59:31 +00:00
|
|
|
status_date = StatusModifiedField(_('status date'))
|
2010-04-15 02:53:55 +00:00
|
|
|
|
2010-04-15 18:00:44 +00:00
|
|
|
status = StatusField(_('status'))
|
2010-04-15 02:53:55 +00:00
|
|
|
|
|
|
|
|
def __unicode__(self):
|
2010-04-15 18:00:44 +00:00
|
|
|
return self.get_status_display()
|
2010-04-15 02:53:55 +00:00
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
|
abstract = True
|
|
|
|
|
|