Source code for superdjango.db.trashed.managers

# Imports

from django.db import models

# Exports

__all__ = (
    "TrashedManager",
    "TrashedQuerySet",
)

# QuerySets


[docs]class TrashedQuerySet(models.QuerySet): """Provide filtering for records implementing ``TrashModel``."""
[docs] def trashed(self): """Return only trashed records.""" return self.filter(is_trashed=True)
[docs] def available(self): """Return records that aren't in the trash.""" return self.filter(is_trashed=False)
# Managers
[docs]class TrashedManager(models.Manager): """Work with records implementing ``TrashModel``. Example: .. code-block:: py class MyModel(TrashModel): objects = models.Manager() trashcan = TrashManager() .. note:: If you want to always filter out trashed records, this manager must be the default. Emptying the trash can. """
[docs] def empty(self, confirmed=False, criteria=None): """Empty the trash can by permanently removing all records marked as trash. :param confirmed: Indicates the removal is confirmed. This is an extra precaution to prevent unintended deletion. :type confirmed: bool :param criteria: Additional criteria. :type criteria: dict Example of using additional criteria: .. code-block:: py # models.py from superdjango.db.audit.models import AddedByModel from superdjango.db.trash.managers import TrashManager from superdjango.db.trash.models import TrashModel class Task(AddedByModel, TrashModel): trashcan = TrashManager() # ... # views.py class EmptyTrash(FormView): def form_valid(self, form): criteria = { 'added_by': self.request.user, } Task.trashcan.empty(confirmed=True, criteria) # ... """ if not confirmed: return 0 if type(criteria) == dict: return self.trashed().filter(**criteria).delete() else: return self.trashed().delete()
# noinspection PyMethodMayBeStatic
[docs] def get_queryset(self): """By default, only available records are returned.""" return TrashedQuerySet().available()
[docs] def trashed(self): """Return only trashed records.""" return self.get_queryset().trashed()