From c942d87256f286fbed7ee85dd32377a725fc47c2 Mon Sep 17 00:00:00 2001 From: Gabriel Detraz Date: Sat, 30 Jun 2018 02:53:19 +0200 Subject: [PATCH] =?UTF-8?q?Sync=20du=20pseudo=20et=20mailalias=20=C3=A0=20?= =?UTF-8?q?la=20creation=20de=20l'user?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- users/models.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/users/models.py b/users/models.py index f9cf68ca..d51aba95 100644 --- a/users/models.py +++ b/users/models.py @@ -929,6 +929,12 @@ class User(RevMixin, FieldPermissionModelMixin, AbstractBaseUser, 'internal_address' : self.can_change_internal_address, } + def clean(self, *args, **kwargs): + """Check if this pseudo is already used by any mailalias. + Better than raising an error in post-save and catching it""" + if MailAlias.objects.filter(valeur=self.pseudo).exclude(user=self): + raise ValidationError("Ce pseudo est déjà utilisé") + def __str__(self): return self.pseudo @@ -1051,9 +1057,11 @@ class Club(User): @receiver(post_save, sender=User) def user_post_save(**kwargs): """ Synchronisation post_save : envoie le mail de bienvenue si creation + Synchronise le pseudo, en créant un alias mail correspondant Synchronise le ldap""" is_created = kwargs['created'] user = kwargs['instance'] + mail_alias, created = MailAlias.objects.get_or_create(valeur=user.pseudo, user=user) if is_created: user.notif_inscription() user.ldap_sync(