# Imports
from django.contrib import admin
from django.utils.translation import ugettext_lazy as _
from .filters import ArchivedByListFilter
# Exports
__all__ = (
"BaseArchivedAdmin",
)
# Models
[docs]class BaseArchivedAdmin(admin.ModelAdmin):
"""Base admin class for models implementing the ``ArchivedModel``."""
[docs] def get_actions(self, request):
return [
"mark_archived",
"mark_unarchived",
]
[docs] def get_fields(self, request, obj=None):
return [
"is_archived",
]
[docs] def get_list_display(self, request):
return [
"archived_by",
"archived_dt",
"is_archived",
]
[docs] def get_list_filter(self, request):
return [
ArchivedByListFilter,
]
[docs] def mark_archived(self, request, queryset):
"""Mark a set of records as archived."""
count = 0
for row in queryset:
row.mark_archived(request.user)
count += 1
self.message_user(request, _("Archived %s records." % count))
mark_archived.short_description = _("Archive Selected Records")
[docs] def mark_unarchived(self, request, queryset):
"""Mark a set of records as unarchived."""
count = 0
for row in queryset:
row.mark_unarchived()
count += 1
self.message_user(request, _("Restored %s records from archive." % count))
mark_unarchived.short_description = _("Restore Selected Records From Archive")
[docs] def save_model(self, request, obj, form, change):
# It is safe to call mark_archived() even when the record has already been marked as archived.
if obj.is_archived:
obj.mark_archived(request.user, commit=False)
super().save_model(request, obj, form, change)