django-model-utils/model_utils/models.py

174 lines
5.1 KiB
Python
Raw Normal View History

from django.core.exceptions import ImproperlyConfigured
2023-10-26 15:53:16 +00:00
from django.db import models
2020-11-29 20:58:00 +00:00
from django.db.models.functions import Now
from django.utils.translation import gettext_lazy as _
2019-02-26 16:55:02 +00:00
from model_utils.fields import (
AutoCreatedField,
AutoLastModifiedField,
MonitorField,
2020-11-29 20:58:00 +00:00
StatusField,
2019-02-26 16:55:02 +00:00
UUIDField,
)
2020-11-29 20:58:00 +00:00
from model_utils.managers import QueryManager, SoftDeletableManager
2019-02-26 16:55:02 +00:00
2019-08-21 09:19:13 +00:00
now = Now()
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'))
def save(self, *args, **kwargs):
"""
Overriding the save method in order to make sure that
modified field is updated even if it is not given as
a parameter to the update field argument.
"""
update_fields = kwargs.get('update_fields', None)
if update_fields:
kwargs['update_fields'] = set(update_fields).union({'modified'})
super().save(*args, **kwargs)
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')
def save(self, *args, **kwargs):
"""
Overriding the save method in order to make sure that
status_changed field is updated even if it is not given as
a parameter to the update field argument.
"""
update_fields = kwargs.get('update_fields', None)
if update_fields and 'status' in update_fields:
kwargs['update_fields'] = set(update_fields).union({'status_changed'})
super().save(*args, **kwargs)
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
default_manager = sender._meta.default_manager
for value, display in getattr(sender, 'STATUS', ()):
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)
)
sender.add_to_class(value, QueryManager(status=value))
sender._meta.default_manager_name = default_manager.name
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
if _field_exists(sender, 'timeframed'):
raise ImproperlyConfigured(
"Model '%s' has a field named 'timeframed' "
"which conflicts with the TimeFramedModel manager."
% sender.__name__
)
sender.add_to_class('timeframed', QueryManager(
(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)
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(_emit_deprecation_warnings=True)
available_objects = SoftDeletableManager()
all_objects = models.Manager()
2016-09-12 11:50:03 +00:00
def delete(self, using=None, soft=True, *args, **kwargs):
2016-09-12 11:50:03 +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
"""
if soft:
self.is_removed = True
self.save(using=using)
else:
return super().delete(using=using, *args, **kwargs)
2019-02-26 16:32:56 +00:00
class UUIDModel(models.Model):
"""
This abstract base class provides id field on any model that inherits from it
which will be the primary key.
"""
id = UUIDField(
primary_key=True,
version=4,
editable=False,
)
class Meta:
abstract = True