Source code for nlpmed_portal.management.admin

# SPDX-FileCopyrightText: Copyright (C) 2026 Omid Jafari <omidjafari.com>
# SPDX-License-Identifier: AGPL-3.0-or-later
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

import contextlib

from auditlog.admin import LogEntryAdmin as BaseLogEntryAdmin
from auditlog.models import LogEntry
from django.contrib import admin
from django.contrib.admin.exceptions import NotRegistered
from django.core.cache import cache
from simple_history.admin import SimpleHistoryAdmin

from nlpmed_portal.context_processors import _SYSTEM_MESSAGE_CACHE_KEY
from nlpmed_portal.management.models import Feedback
from nlpmed_portal.management.models import SystemMessage

admin.site.register(Feedback, SimpleHistoryAdmin)


[docs] @admin.register(SystemMessage) class SystemMessageAdmin(SimpleHistoryAdmin):
[docs] def save_model(self, request, obj, form, change): super().save_model(request, obj, form, change) cache.delete(_SYSTEM_MESSAGE_CACHE_KEY)
[docs] def delete_model(self, request, obj): super().delete_model(request, obj) cache.delete(_SYSTEM_MESSAGE_CACHE_KEY)
with contextlib.suppress(NotRegistered): admin.site.unregister(LogEntry)
[docs] @admin.register(LogEntry) class LogEntryAdmin(BaseLogEntryAdmin): list_display = BaseLogEntryAdmin.list_display
[docs] def get_fieldsets(self, request, obj=...): fieldsets = list(super().get_fieldsets(request, obj)) already_exists = any( "remote_addr" in (fields if isinstance(fields, (list, tuple)) else (fields,)) for _, opts in fieldsets for fields in (opts.get("fields") or []) ) if not already_exists: fieldsets.append(("Request", {"fields": ("remote_addr",)})) return fieldsets