# Imports
from django.contrib import admin
from django.utils.translation import ugettext_lazy as _
from .filters import OwnedByListFilter
# Exports
__all__ = (
"BaseOwnedByAdmin",
)
# Models
[docs]class BaseOwnedByAdmin(admin.ModelAdmin):
"""Base admin class for models implementing the ``OwnedByModel``."""
[docs] def get_actions(self, request):
return [
"set_current_owner",
]
[docs] def get_fields(self, request, obj=None):
return [
"owned_by",
]
[docs] def get_list_display(self, request):
return [
"owned_by",
]
[docs] def get_list_filter(self, request):
return [
OwnedByListFilter,
]
[docs] def set_current_owner(self, request, queryset):
"""Set record ownership to the current user."""
count = 0
for row in queryset:
row.owned_by = request.user
row.save()
count += 1
self.message_user(request, _("Changed ownership of %s records." % count))
set_current_owner.short_description = _("Set Yourself as the Owner of the Select Records")