Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1# Imports
3from django.contrib import admin
4from django.utils.translation import ugettext_lazy as _
5from .filters import ArchivedByListFilter
7# Exports
9__all__ = (
10 "BaseArchivedAdmin",
11)
13# Models
16class BaseArchivedAdmin(admin.ModelAdmin):
17 """Base admin class for models implementing the ``ArchivedModel``."""
19 def get_actions(self, request):
20 return [
21 "mark_archived",
22 "mark_unarchived",
23 ]
25 def get_fields(self, request, obj=None):
26 return [
27 "is_archived",
28 ]
30 def get_list_display(self, request):
31 return [
32 "archived_by",
33 "archived_dt",
34 "is_archived",
35 ]
37 def get_list_filter(self, request):
38 return [
39 ArchivedByListFilter,
40 ]
42 def mark_archived(self, request, queryset):
43 """Mark a set of records as archived."""
44 count = 0
45 for row in queryset:
46 row.mark_archived(request.user)
47 count += 1
49 self.message_user(request, _("Archived %s records." % count))
51 mark_archived.short_description = _("Archive Selected Records")
53 def mark_unarchived(self, request, queryset):
54 """Mark a set of records as unarchived."""
55 count = 0
56 for row in queryset:
57 row.mark_unarchived()
58 count += 1
60 self.message_user(request, _("Restored %s records from archive." % count))
62 mark_unarchived.short_description = _("Restore Selected Records From Archive")
64 def save_model(self, request, obj, form, change):
65 # It is safe to call mark_archived() even when the record has already been marked as archived.
66 if obj.is_archived:
67 obj.mark_archived(request.user, commit=False)
69 super().save_model(request, obj, form, change)