From 4a3f9b89fe1d6d57c3858c854f10716a6ce45789 Mon Sep 17 00:00:00 2001 From: Saleem Latif Date: Fri, 13 Nov 2020 13:13:14 +0500 Subject: [PATCH] Do not send email if profile not created, this code path is run as a result UserPreference save signal before profile is created. --- lms/djangoapps/email_marketing/signals.py | 4 ++++ lms/djangoapps/email_marketing/tests/test_signals.py | 6 +++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/lms/djangoapps/email_marketing/signals.py b/lms/djangoapps/email_marketing/signals.py index 0625919a13..6def2d32bf 100644 --- a/lms/djangoapps/email_marketing/signals.py +++ b/lms/djangoapps/email_marketing/signals.py @@ -173,6 +173,10 @@ def email_marketing_user_field_changed(sender, user=None, table=None, setting=No if user.is_anonymous: return + # Ignore users that do not yet have a profile + if not hasattr(user, 'profile'): + return + # ignore anything but User, Profile or UserPreference tables if table not in {'auth_user', 'auth_userprofile', 'user_api_userpreference'}: return diff --git a/lms/djangoapps/email_marketing/tests/test_signals.py b/lms/djangoapps/email_marketing/tests/test_signals.py index 4e604d3c46..acb833d6b7 100644 --- a/lms/djangoapps/email_marketing/tests/test_signals.py +++ b/lms/djangoapps/email_marketing/tests/test_signals.py @@ -30,7 +30,7 @@ from lms.djangoapps.email_marketing.tasks import ( update_user_email ) from openedx.core.djangoapps.lang_pref import LANGUAGE_KEY -from common.djangoapps.student.models import Registration +from common.djangoapps.student.models import Registration, User from common.djangoapps.student.tests.factories import CourseEnrollmentFactory, UserFactory, UserProfileFactory from common.djangoapps.util.json_request import JsonResponse @@ -336,6 +336,10 @@ class EmailMarketingTests(TestCase): email_marketing_user_field_changed(None, user=anon) self.assertFalse(mock_log_error.called) + user = User(username='test', email='test@example.com') + email_marketing_user_field_changed(None, user=user) + self.assertFalse(mock_log_error.called) + @patch('lms.djangoapps.email_marketing.tasks.SailthruClient.api_post') def test_change_email(self, mock_sailthru): """