def update_user(self, existing_user=None, **kwargs): is_update_successful = False if not existing_user and 'email' in self.cleaned_data: from django.contrib.auth.models import User django_user = User() django_user.username = self.cleaned_data['email'] django_user.save() existing_user = CouchUser.from_django_user(django_user) existing_user.save() is_update_successful = True if 'email' in self.cleaned_data: old_email = existing_user.email new_email = self.cleaned_data['email'] if old_email != new_email: if existing_user.subscribed_to_commcare_users: handle_changed_mailchimp_email( existing_user, old_email, new_email, settings.MAILCHIMP_COMMCARE_USERS_ID) if not existing_user.email_opt_out: handle_changed_mailchimp_email( existing_user, old_email, new_email, settings.MAILCHIMP_MASS_EMAIL_ID) for prop in self.direct_properties: setattr(existing_user, prop, self.cleaned_data[prop]) is_update_successful = True if is_update_successful: existing_user.save() return is_update_successful
def update_user(self, existing_user=None, save=True, **kwargs): is_update_successful = False if not existing_user and 'email' in self.cleaned_data: from django.contrib.auth.models import User django_user = User() django_user.username = self.cleaned_data['email'] django_user.save() existing_user = CouchUser.from_django_user(django_user) existing_user.save() is_update_successful = True if 'email' in self.cleaned_data: old_email = existing_user.email new_email = self.cleaned_data['email'] if old_email != new_email: handle_changed_mailchimp_email(existing_user, old_email, new_email) for prop in self.direct_properties: setattr(existing_user, prop, self.cleaned_data[prop]) is_update_successful = True if is_update_successful and save: existing_user.save() return is_update_successful
def test_handle_changed_mailchimp_email_unsubscribe(self, unsubscribe): """ Only one user with this email, unsubscribe it from both """ new_email = "*****@*****.**" handle_changed_mailchimp_email(self.couch_user, self.email, new_email) expected_call_args = [mock.call(self.couch_user, settings.MAILCHIMP_COMMCARE_USERS_ID, email=self.email), mock.call(self.couch_user, settings.MAILCHIMP_MASS_EMAIL_ID, email=self.email)] self.assertEqual(unsubscribe.call_count, 2) self.assertItemsEqual(unsubscribe.call_args_list, expected_call_args)
def test_handle_changed_mailchimp_email_dont_unsubscribe(self, unsubscribe): """ Two users with the same email, dont unsubscribe """ other_username = "******" other_couch_user = CommCareUser.create(self.domain, other_username, "passw3rd", email=self.email) other_couch_user.subscribed_to_commcare_users = True other_couch_user.email_opt_out = False other_couch_user.save() new_email = "*****@*****.**" handle_changed_mailchimp_email(self.couch_user, self.email, new_email) self.assertEqual(unsubscribe.call_count, 0)
def test_handle_changed_mailchimp_email_other_user_not_subscribed(self, unsubscribe): """ Two users with the same email, other user is not subscribed, so unsubscribe """ other_username = "******" other_couch_user = CommCareUser.create(self.domain, other_username, "passw3rd", email=self.email) other_couch_user.subscribed_to_commcare_users = False other_couch_user.email_opt_out = True other_couch_user.save() new_email = "*****@*****.**" handle_changed_mailchimp_email(self.couch_user, self.email, new_email) expected_call_args = [mock.call(self.couch_user, settings.MAILCHIMP_COMMCARE_USERS_ID, email=self.email), mock.call(self.couch_user, settings.MAILCHIMP_MASS_EMAIL_ID, email=self.email)] self.assertEqual(unsubscribe.call_count, 2) self.assertItemsEqual(unsubscribe.call_args_list, expected_call_args)
def test_handle_changed_mailchimp_email_same_email_alternate_subscriptions(self, unsubscribe): """ Two users with the same email, other user is subscribed to opposite lists """ self.couch_user.email_opt_out = True self.couch_user.save() # User subscribed to mass email list other_username = "******" other_couch_user = CommCareUser.create(self.domain, other_username, "passw3rd", email=self.email) other_couch_user.subscribed_to_commcare_users = False other_couch_user.email_opt_out = False other_couch_user.save() new_email = "*****@*****.**" handle_changed_mailchimp_email(self.couch_user, self.email, new_email) expected_call_args = [mock.call(self.couch_user, settings.MAILCHIMP_MASS_EMAIL_ID, email=self.email)] self.assertEqual(unsubscribe.call_count, 1) self.assertItemsEqual(unsubscribe.call_args_list, expected_call_args)
def update_user(self, existing_user=None, save=True, **kwargs): is_update_successful = False if not existing_user and 'email' in self.cleaned_data: from django.contrib.auth.models import User django_user = User() django_user.username = self.cleaned_data['email'] django_user.save() existing_user = CouchUser.from_django_user(django_user) existing_user.save() is_update_successful = True if 'email' in self.cleaned_data: old_email = existing_user.email new_email = self.cleaned_data['email'] if old_email != new_email: if existing_user.subscribed_to_commcare_users: handle_changed_mailchimp_email( existing_user, old_email, new_email, settings.MAILCHIMP_COMMCARE_USERS_ID ) if not existing_user.email_opt_out: handle_changed_mailchimp_email( existing_user, old_email, new_email, settings.MAILCHIMP_MASS_EMAIL_ID ) for prop in self.direct_properties: setattr(existing_user, prop, self.cleaned_data[prop]) is_update_successful = True if is_update_successful and save: existing_user.save() return is_update_successful