Source code for superdjango.db.trashed.models

# Imports

from django.conf import settings
from django.db import models
from django.utils.translation import ugettext_lazy as _
from django.utils.timezone import now

# Exports

__all__ = (
    "TrashedModel",
)

# Constants

AUTH_USER_MODEL = settings.AUTH_USER_MODEL

# Models


[docs]class TrashedModel(models.Model): """Allow a record to be marked as "trashed" without deleting it.""" is_trashed = models.NullBooleanField( _("in trash"), help_text=_("Indicates this record is in the trash."), ) trashed_by = models.ForeignKey( AUTH_USER_MODEL, blank=True, help_text=_("User that put the record in the trash."), null=True, on_delete=models.SET_NULL, related_name="%(app_label)s_%(class)s_trashed_records", verbose_name=_("trashed by") ) trashed_dt = models.DateTimeField( _("trashed date/time"), blank=True, help_text=_("Date and time the record was trashed."), null=True ) class Meta: abstract = True
[docs] def restore_record(self, commit=True): """Remove the record in the trash. :param commit: Whether to save after updating the fields. :type commit: bool """ self.is_trashed = False self.trashed_by_user = None self.trash_dt = None if commit: self.save(update_fields=["is_trashed", "trashed_by", "trashed_dt"])
[docs] def trash_record(self, user, commit=True): """Put the record in the trash. :param user: The user instance. :type user: AUTH_USER_MODEL :param commit: Whether to save after updating the fields. :type commit: bool """ self.is_trashed = True self.trashed_by_user = user self.trash_dt = now() if commit: self.save(update_fields=["is_trashed", "trashed_by", "trashed_dt"])