Skip to content

Commit c43b4ee

Browse files
committed
simpify mail pref changing by make synchronous function instead of async task
1 parent fbf5f85 commit c43b4ee

File tree

5 files changed

+39
-50
lines changed

5 files changed

+39
-50
lines changed

api/users/serializers.py

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,7 @@
3838
from osf.models.user_message import MessageTypes
3939
from osf.models.provider import AbstractProviderGroupObjectPermission
4040
from osf.utils.requests import string_type_request_headers
41-
from website import settings
42-
from website.profile.views import update_mailchimp_subscription
41+
from website import settings, mailchimp_utils
4342
from website.settings import MAILCHIMP_GENERAL_LIST, OSF_HELP_LIST, CONFIRM_REGISTRATIONS_BY_EMAIL
4443
from website.util import api_v2_url
4544

@@ -546,7 +545,17 @@ def update_email_preferences(self, instance, attr, value):
546545
if self.MAP_MAIL[attr] == OSF_HELP_LIST:
547546
instance.osf_mailing_lists[settings.OSF_HELP_LIST] = value
548547
elif self.MAP_MAIL[attr] == MAILCHIMP_GENERAL_LIST:
549-
update_mailchimp_subscription(instance, self.MAP_MAIL[attr], value)
548+
if value:
549+
mailchimp_utils.subscribe_mailchimp(
550+
self.MAP_MAIL[attr],
551+
instance._id,
552+
)
553+
else:
554+
mailchimp_utils.unsubscribe_mailchimp(
555+
self.MAP_MAIL[attr],
556+
instance._id,
557+
username=instance.username,
558+
)
550559
else:
551560
raise exceptions.ValidationError(detail='Invalid email preference.')
552561

@@ -587,14 +596,6 @@ def to_representation(self, instance):
587596
"""
588597
context = self.context
589598

590-
# special case to show that we sent a request to subscribe, but don't
591-
# change actual value until the task executes in case it doesn't and they need to try again
592-
if getattr(instance, 'assume_mailchimp_completed_request', False):
593-
instance.mailchimp_mailing_lists[MAILCHIMP_GENERAL_LIST] = True
594-
instance.save = lambda _: NotImplementedError(
595-
'Cannot save read comments in UserSettingsUpdateSerializer.to_representation',
596-
)
597-
598599
return UserSettingsSerializer(instance=instance, context=context).data
599600

600601
def update(self, instance, validated_data):
@@ -611,11 +612,6 @@ def update(self, instance, validated_data):
611612
elif attr in self.MAP_MAIL.keys():
612613
self.update_email_preferences(instance, attr, value)
613614

614-
# special case to show that we sent a request to subscribe, but don't
615-
# change actual value until the task executes in case it doesn't and they need to try again
616-
if value and self.MAP_MAIL[attr] == MAILCHIMP_GENERAL_LIST:
617-
instance.assume_mailchimp_completed_request = True
618-
619615
return instance
620616

621617

api_tests/users/views/test_user_settings_detail.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from osf_tests.factories import (
55
AuthUserFactory,
66
)
7-
from website.settings import MAILCHIMP_GENERAL_LIST, OSF_HELP_LIST
7+
from website.settings import OSF_HELP_LIST
88

99

1010
@pytest.fixture()
@@ -200,16 +200,15 @@ def bad_payload(self, user_one):
200200
}
201201
}
202202

203-
@mock.patch('api.users.serializers.update_mailchimp_subscription')
204-
def test_authorized_patch_200(self, mailchimp_mock, app, user_one, payload, url):
203+
@mock.patch('website.mailchimp_utils.get_mailchimp_api')
204+
def test_authorized_patch_200(self, mock_mailchimp_client, app, user_one, payload, url):
205205
res = app.patch_json_api(url, payload, auth=user_one.auth)
206206
assert res.status_code == 200
207207

208208
user_one.refresh_from_db()
209209
assert res.json['data']['attributes']['subscribe_osf_help_email'] is False
210210
assert user_one.osf_mailing_lists[OSF_HELP_LIST] is False
211-
mailchimp_mock.assert_called_with(user_one, MAILCHIMP_GENERAL_LIST, True)
212-
assert res.json['data']['attributes']['subscribe_osf_general_email'] is True
211+
mock_mailchimp_client.assert_called_with()
213212

214213
def test_bad_payload_patch_400(self, app, user_one, bad_payload, url):
215214
res = app.patch_json_api(url, bad_payload, auth=user_one.auth, expect_errors=True)

tests/test_configure_mailing_list_view.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ def test_get_notifications(self):
4545
res = self.app.get(url, auth=user.auth)
4646
assert mailing_lists == res.json['mailing_lists']
4747

48-
@unittest.skipIf(settings.USE_CELERY, 'Subscription must happen synchronously for this test')
4948
@mock.patch('website.mailchimp_utils.get_mailchimp_api')
5049
def test_user_choose_mailing_lists_updates_user_dict(self, mock_get_mailchimp_api):
5150
user = AuthUserFactory()

website/mailchimp_utils.py

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -39,37 +39,35 @@ def get_list_name_from_id(list_id):
3939
@queued_task
4040
@app.task
4141
@transaction.atomic
42+
def subscribe_mailchimp_async(list_name, user_id):
43+
return subscribe_mailchimp(list_name=list_name, user_id=user_id)
44+
4245
def subscribe_mailchimp(list_name, user_id):
4346
user = OSFUser.load(user_id)
44-
user_hash = hashlib.md5(user.username.lower().encode()).hexdigest()
45-
m = get_mailchimp_api()
46-
list_id = get_list_id_from_name(list_name=list_name)
47+
if not user:
48+
raise OSFError('User not found.')
4749

4850
if user.mailchimp_mailing_lists is None:
4951
user.mailchimp_mailing_lists = {}
5052

51-
try:
52-
m.lists.members.create_or_update(
53-
list_id=list_id,
54-
subscriber_hash=user_hash,
55-
data={
56-
'status': 'subscribed',
57-
'status_if_new': 'subscribed',
58-
'email_address': user.username,
59-
'merge_fields': {
60-
'FNAME': user.given_name,
61-
'LNAME': user.family_name
62-
}
53+
get_mailchimp_api().lists.members.create_or_update(
54+
list_id=get_list_id_from_name(list_name=list_name),
55+
subscriber_hash=hashlib.md5(
56+
user.username.lower().encode()
57+
).hexdigest(),
58+
data={
59+
'status': 'subscribed',
60+
'status_if_new': 'subscribed',
61+
'email_address': user.username,
62+
'merge_fields': {
63+
'FNAME': user.given_name,
64+
'LNAME': user.family_name
6365
}
64-
)
65-
except MailChimpError as error:
66-
sentry.log_exception(error)
67-
sentry.log_message(error)
68-
user.mailchimp_mailing_lists[list_name] = False
69-
else:
70-
user.mailchimp_mailing_lists[list_name] = True
71-
finally:
72-
user.save()
66+
}
67+
)
68+
69+
user.mailchimp_mailing_lists[list_name] = True
70+
user.save()
7371

7472

7573
def unsubscribe_mailchimp(list_name, user_id, username=None):

website/profile/views.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -519,10 +519,7 @@ def update_mailchimp_subscription(user, list_name, subscription):
519519
:param boolean subscription: true if user is subscribed
520520
"""
521521
if subscription:
522-
try:
523-
mailchimp_utils.subscribe_mailchimp(list_name, user._id)
524-
except (MailChimpError, OSFError):
525-
pass
522+
mailchimp_utils.subscribe_mailchimp(list_name, user._id)
526523
else:
527524
try:
528525
mailchimp_utils.unsubscribe_mailchimp_async(list_name, user._id, username=user.username)

0 commit comments

Comments
 (0)