diff --git a/apps/users/models/user.py b/apps/users/models/user.py index 21cc9bf03..b696795a8 100644 --- a/apps/users/models/user.py +++ b/apps/users/models/user.py @@ -596,6 +596,11 @@ class User(AuthMixin, TokenMixin, RoleMixin, MFAMixin, AbstractUser): user.save() user.groups.add(UserGroup.initial()) + def can_send_created_mail(self): + if self.email and self.source == self.SOURCE_LOCAL: + return True + return False + @classmethod def generate_fake(cls, count=100): from random import seed, choice diff --git a/apps/users/signals_handler.py b/apps/users/signals_handler.py index 45c03f3cf..aa1458c7d 100644 --- a/apps/users/signals_handler.py +++ b/apps/users/signals_handler.py @@ -17,7 +17,7 @@ def on_user_create(sender, user=None, **kwargs): logger.debug("Receive user `{}` create signal".format(user.name)) from .utils import send_user_created_mail logger.info(" - Sending welcome mail ...".format(user.name)) - if user.email: + if user.can_send_created_mail(): send_user_created_mail(user) diff --git a/apps/users/tasks.py b/apps/users/tasks.py index 45e1c40cd..5c7bc0e47 100644 --- a/apps/users/tasks.py +++ b/apps/users/tasks.py @@ -22,15 +22,15 @@ logger = get_logger(__file__) @shared_task def check_password_expired(): - users = User.objects.exclude(role=User.ROLE_APP) + users = User.objects.filter(source=User.SOURCE_LOCAL).exclude(role=User.ROLE_APP) for user in users: if not user.is_valid: continue if not user.password_will_expired: continue - send_password_expiration_reminder_mail(user) msg = "The user {} password expires in {} days" logger.info(msg.format(user, user.password_expired_remain_days)) + send_password_expiration_reminder_mail(user) @shared_task @@ -55,6 +55,8 @@ def check_user_expired(): continue if not user.will_expired: continue + msg = "The user {} will expires in {} days" + logger.info(msg.format(user, user.expired_remain_days)) send_user_expiration_reminder_mail(user)