Source code for superdjango.db.expiration.models

# Imports

from django.db import models
from django.utils.translation import gettext_lazy as _
from django.utils.timezone import now

# Exports

__all__ = (
    "ExpirationModel",
)

# Models


[docs]class ExpirationModel(models.Model): """Allows a model to (semi) automatically expire. .. note:: There are several options for managers. It is left to the developer to decide which ones are needed. """ expiration_dt = models.DateTimeField( _("expiration date/time"), blank=True, help_text=_("Date and time the record should expire."), null=True ) has_expired = models.BooleanField( _("expired"), help_text=_("Indicates the record has expired."), null=True ) class Meta: abstract = True
[docs] def is_past_expiration(self): """Determine whether the record has expired. :rtype: bool :returns: ``True`` if the record is past the expiration date. """ if self.has_expired is not None: return self.has_expired if not self.expiration_dt: return False return self.expiration_dt > now()