diff --git a/apps/member/apps.py b/apps/member/apps.py index 2d7f4ab7..20be3e3c 100644 --- a/apps/member/apps.py +++ b/apps/member/apps.py @@ -2,9 +2,22 @@ # SPDX-License-Identifier: GPL-3.0-or-later from django.apps import AppConfig +from django.conf import settings +from django.db.models.signals import pre_save from django.utils.translation import gettext_lazy as _ +from .signals import save_user_note + class MemberConfig(AppConfig): name = 'member' verbose_name = _('member') + + def ready(self): + """ + Define app internal signals to interact with other apps + """ + pre_save.connect( + save_user_note, + sender=settings.AUTH_USER_MODEL, + ) diff --git a/apps/member/signals.py b/apps/member/signals.py index 4e945ad5..5debce4b 100644 --- a/apps/member/signals.py +++ b/apps/member/signals.py @@ -1,2 +1,15 @@ # Copyright (C) 2018-2020 by BDE ENS Paris-Saclay # SPDX-License-Identifier: GPL-3.0-or-later + +def save_user_note(instance, created, raw, **_kwargs): + """ + Hook to create and save a profile when an user is updated if it is not registered with the signup form + """ + if raw: + # When provisionning data, do not try to autocreate + return + + if created: + from .models import Profile + Profile.objects.get_or_create(user=instance) + instance.profile.save()