diff --git a/tom_common/apps.py b/tom_common/apps.py index fae63b5f..b242634f 100644 --- a/tom_common/apps.py +++ b/tom_common/apps.py @@ -7,6 +7,10 @@ class TomCommonConfig(AppConfig): name = 'tom_common' def ready(self): + # Import signals for automatically saving profiles when updating User objects + # https://docs.djangoproject.com/en/5.1/topics/signals/#connecting-receiver-functions + import tom_common.signals # noqa + # Set default plotly theme on startup valid_themes = ['plotly', 'plotly_white', 'plotly_dark', 'ggplot2', 'seaborn', 'simple_white', 'none'] diff --git a/tom_common/signals.py b/tom_common/signals.py index e2e76191..4091c21c 100644 --- a/tom_common/signals.py +++ b/tom_common/signals.py @@ -1,14 +1,12 @@ +import logging + from django.db.models.signals import post_save from django.contrib.auth.models import User from django.dispatch import receiver -from tom_common.models import Profile +from tom_common.models import Profile -@receiver(post_save, sender=User) -def create_profile(sender, instance, created, **kwargs): - """When a new user is created, create a profile for them.""" - if created: - Profile.objects.create(user=instance) +logger = logging.getLogger(__name__) @receiver(post_save, sender=User) @@ -19,4 +17,5 @@ def save_profile(sender, instance, **kwargs): try: instance.profile.save() except User.profile.RelatedObjectDoesNotExist: + logger.info(f'No Profile found for {instance}. Creating Profile.') Profile.objects.create(user=instance)