Skip to content

Commit

Permalink
ldapsync: use logging framework
Browse files Browse the repository at this point in the history
  • Loading branch information
timhawes committed Jul 20, 2024
1 parent ae5fbc8 commit 081a2fe
Showing 1 changed file with 25 additions and 20 deletions.
45 changes: 25 additions & 20 deletions ldapsync/receivers.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# SPDX-FileCopyrightText: 2022 Tim Hawes <[email protected]>
# SPDX-FileCopyrightText: 2022-2024 Tim Hawes <[email protected]>
#
# SPDX-License-Identifier: MIT

import logging

from django.conf import settings
from django.contrib.auth import get_user_model
from django.contrib.auth.models import Group
Expand All @@ -13,69 +15,72 @@
from .utils import sync_group, sync_user


logger = logging.getLogger(__name__)


@receiver(post_save, sender=get_user_model())
def sync_ldap_user(sender, instance, update_fields=[], **kwargs):
if update_fields and list(update_fields) == ["last_login"]:
# skip update if last_login was the only change
return
try:
print(f"ldapsync user {instance}")
logger.info(f"ldapsync user {instance}")
sync_user(instance)
except Exception as e:
print(f"Exception in ldapsync of {instance}: {e}")
except Exception:
logger.exception(f"Exception in ldapsync of {instance}")


@receiver(post_save, sender=PosixUser)
def sync_ldap_posix_user(sender, instance, **kwargs):
try:
print(f"ldapsync user {instance.user} (for PosixUser)")
logger.info(f"ldapsync user {instance.user} (for PosixUser)")
sync_user(instance.user)
except Exception as e:
print(f"Exception in ldapsync of {instance}: {e}")
except Exception:
logger.exception(f"Exception in ldapsync of {instance}")


@receiver(post_save, sender=SSHKey)
def sync_ldap_posix_sshkey(sender, instance, **kwargs):
try:
print(f"ldapsync user {instance.user} (for SSHKey)")
logger.info(f"ldapsync user {instance.user} (for SSHKey)")
sync_user(instance.user)
except Exception as e:
print(f"Exception in ldapsync of {instance}: {e}")
except Exception:
logger.exception(f"Exception in ldapsync of {instance}")


@receiver(post_save, sender=Group)
def sync_ldap_group(sender, instance, **kwargs):
try:
print(f"ldapsync group {instance}")
logger.info(f"ldapsync group {instance}")
sync_group(instance)
except Exception as e:
print(f"Exception in ldapsync of {instance}: {e}")
except Exception:
logger.exception(f"Exception in ldapsync of {instance}")


@receiver(post_save, sender=PosixGroup)
def sync_ldap_posix_group(sender, instance, **kwargs):
try:
print(f"ldapsync user {instance.group} (for PosixGroup)")
logger.info(f"ldapsync user {instance.group} (for PosixGroup)")
sync_group(instance.group)
except Exception as e:
print(f"Exception in ldapsync of {instance}: {e}")
except Exception:
logger.exception(f"Exception in ldapsync of {instance}")


@receiver(m2m_changed, sender=Group.user_set.through)
def sync_ldap_group_m2m(sender, instance, *, action=None, **kwargs):
if action in ["post_add", "post_remove"]:
try:
print(f"ldapsync group {instance} (for membership)")
logger.info(f"ldapsync group {instance} (for membership)")
sync_group(instance)
except Exception as e:
print(f"Exception in ldapsync of {instance}: {e}")
logger.exception(f"Exception in ldapsync of {instance}")


# @receiver(post_save)
# def test_post_save(sender, instance, **kwargs):
# print(f"POST_SAVE sender={sender} instance={instance} kwargs={kwargs}")
# logger.debug(f"POST_SAVE sender={sender} instance={instance} kwargs={kwargs}")


# @receiver(m2m_changed)
# def test_m2m_changed(sender, instance, **kwargs):
# print(f"M2M_CHANGED sender={sender} instance={instance} kwargs={kwargs}")
# logger.debug(f"M2M_CHANGED sender={sender} instance={instance} kwargs={kwargs}")

0 comments on commit 081a2fe

Please sign in to comment.