2019-07-02 09:51:50 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
2022-12-20 12:23:42 +00:00
|
|
|
|
2019-07-02 09:51:50 +00:00
|
|
|
from django.db import models
|
|
|
|
from django.utils import timezone
|
2023-07-24 03:52:25 +00:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2019-07-02 09:51:50 +00:00
|
|
|
|
2019-10-18 07:05:45 +00:00
|
|
|
__all__ = [
|
|
|
|
"NoDeleteManager", "NoDeleteModelMixin", "NoDeleteQuerySet",
|
|
|
|
]
|
2019-07-02 09:51:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
class NoDeleteQuerySet(models.query.QuerySet):
|
|
|
|
|
|
|
|
def delete(self):
|
|
|
|
return self.update(is_discard=True, discard_time=timezone.now())
|
|
|
|
|
|
|
|
|
|
|
|
class NoDeleteManager(models.Manager):
|
|
|
|
|
|
|
|
def get_all(self):
|
|
|
|
return NoDeleteQuerySet(self.model, using=self._db)
|
|
|
|
|
|
|
|
def get_queryset(self):
|
|
|
|
return NoDeleteQuerySet(self.model, using=self._db).filter(is_discard=False)
|
|
|
|
|
|
|
|
def get_deleted(self):
|
|
|
|
return NoDeleteQuerySet(self.model, using=self._db).filter(is_discard=True)
|
|
|
|
|
|
|
|
|
|
|
|
class NoDeleteModelMixin(models.Model):
|
|
|
|
is_discard = models.BooleanField(verbose_name=_("is discard"), default=False)
|
|
|
|
discard_time = models.DateTimeField(verbose_name=_("discard time"), null=True, blank=True)
|
|
|
|
|
|
|
|
objects = NoDeleteManager()
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
abstract = True
|
|
|
|
|
|
|
|
def delete(self):
|
|
|
|
self.is_discard = True
|
|
|
|
self.discard_time = timezone.now()
|
|
|
|
return self.save()
|