django-model-utils/model_utils/models.py

95 lines
3.1 KiB
Python
Raw Normal View History

from __future__ import unicode_literals
from django.db import models
2010-04-15 02:49:51 +00:00
from django.utils.translation import ugettext_lazy as _
from django.db.models.fields import FieldDoesNotExist
2013-02-19 00:21:14 +00:00
from django.core.exceptions import ImproperlyConfigured
2013-04-12 23:12:06 +00:00
from django.utils.timezone import now
2013-01-27 21:30:31 +00:00
from model_utils.managers import QueryManager
from model_utils.fields import AutoCreatedField, AutoLastModifiedField, \
StatusField, MonitorField
2010-04-15 02:43:27 +00:00
class TimeStampedModel(models.Model):
"""
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'))
class Meta:
abstract = True
class TimeFramedModel(models.Model):
"""
An abstract base class model that provides ``start``
and ``end`` fields to record a timeframe.
"""
start = models.DateTimeField(_('start'), null=True, blank=True)
end = models.DateTimeField(_('end'), null=True, blank=True)
class Meta:
abstract = True
class StatusModel(models.Model):
"""
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.
"""
status = StatusField(_('status'))
status_changed = MonitorField(_('status changed'), monitor='status')
class Meta:
abstract = True
def add_status_query_managers(sender, **kwargs):
"""
Add a Querymanager for each status item dynamically.
2010-04-27 17:25:45 +00:00
"""
if not issubclass(sender, StatusModel):
return
for value, name in getattr(sender, 'STATUS', ()):
try:
sender._meta.get_field(name)
raise ImproperlyConfigured("StatusModel: Model '%s' has a field "
"named '%s' which conflicts with a "
"status of the same name."
% (sender.__name__, name))
except FieldDoesNotExist:
pass
sender.add_to_class(value, QueryManager(status=value))
def add_timeframed_query_manager(sender, **kwargs):
"""
2010-04-27 17:25:45 +00:00
Add a QueryManager for a specific timeframe.
"""
if not issubclass(sender, TimeFramedModel):
return
try:
sender._meta.get_field('timeframed')
2010-04-27 17:25:45 +00:00
raise ImproperlyConfigured("Model '%s' has a field named "
"'timeframed' which conflicts with "
2012-01-07 22:43:39 +00:00
"the TimeFramedModel manager."
2010-04-27 17:25:45 +00:00
% sender.__name__)
except FieldDoesNotExist:
pass
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))
))
models.signals.class_prepared.connect(add_status_query_managers)
models.signals.class_prepared.connect(add_timeframed_query_manager)