Source code for nlpmed_portal.users.signals
# 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/>.
from auditlog.models import LogEntry
from django.conf import settings
from django.db.models.signals import post_save
from django.dispatch import receiver
from hijack.signals import hijack_ended
from hijack.signals import hijack_started
[docs]
@receiver(hijack_started)
def log_hijack_start(sender, hijacker, hijacked, request, **kwargs):
LogEntry.objects.log_create(
hijacked,
force_log=True,
actor=hijacker,
action=LogEntry.Action.UPDATE,
changes={"hijack_start": [hijacker.pk, hijacked.pk]},
)
[docs]
@receiver(hijack_ended)
def log_hijack_end(sender, hijacker, hijacked, request, **kwargs):
LogEntry.objects.log_create(
hijacked,
force_log=True,
actor=hijacker,
action=LogEntry.Action.UPDATE,
changes={"hijack_end": [hijacker.pk, hijacked.pk]},
)
[docs]
@receiver(post_save, sender=settings.AUTH_USER_MODEL)
def activate_superuser_on_save(sender, instance, created, **kwargs):
if instance.is_superuser and not instance.is_active:
sender.objects.filter(pk=instance.pk, is_active=False).update(is_active=True)