Skip to content

Commit ae06852

Browse files
committed
fix mailchimp async return value problem
1 parent 9f366cd commit ae06852

File tree

3 files changed

+25
-8
lines changed

3 files changed

+25
-8
lines changed

api/users/serializers.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@
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.profile.views import update_osf_help_mails_subscription, update_mailchimp_subscription
41+
from website import settings
42+
from website.profile.views import update_mailchimp_subscription
4243
from website.settings import MAILCHIMP_GENERAL_LIST, OSF_HELP_LIST, CONFIRM_REGISTRATIONS_BY_EMAIL
4344
from website.util import api_v2_url
4445

@@ -543,10 +544,14 @@ class UserSettingsUpdateSerializer(UserSettingsSerializer):
543544

544545
def update_email_preferences(self, instance, attr, value):
545546
if self.MAP_MAIL[attr] == OSF_HELP_LIST:
546-
update_osf_help_mails_subscription(user=instance, subscribe=value)
547-
else:
547+
instance.osf_mailing_lists[settings.OSF_HELP_LIST] = value
548+
elif self.MAP_MAIL[attr] == MAILCHIMP_GENERAL_LIST:
548549
update_mailchimp_subscription(instance, self.MAP_MAIL[attr], value)
550+
else:
551+
raise exceptions.ValidationError(detail='Invalid email preference.')
552+
549553
instance.save()
554+
return instance
550555

551556
def update_two_factor(self, instance, value, two_factor_addon):
552557
if value:
@@ -581,6 +586,15 @@ def to_representation(self, instance):
581586
Overriding to_representation allows using different serializers for the request and response.
582587
"""
583588
context = self.context
589+
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+
584598
return UserSettingsSerializer(instance=instance, context=context).data
585599

586600
def update(self, instance, validated_data):
@@ -597,6 +611,11 @@ def update(self, instance, validated_data):
597611
elif attr in self.MAP_MAIL.keys():
598612
self.update_email_preferences(instance, attr, value)
599613

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+
600619
return instance
601620

602621

api_tests/users/views/test_user_settings_detail.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,8 +206,10 @@ def test_authorized_patch_200(self, mailchimp_mock, app, user_one, payload, url)
206206
assert res.status_code == 200
207207

208208
user_one.refresh_from_db()
209+
assert res.json['data']['attributes']['subscribe_osf_help_email'] is False
209210
assert user_one.osf_mailing_lists[OSF_HELP_LIST] is False
210211
mailchimp_mock.assert_called_with(user_one, MAILCHIMP_GENERAL_LIST, True)
212+
assert res.json['data']['attributes']['subscribe_osf_general_email'] is True
211213

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

website/profile/views.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,7 @@ def user_choose_mailing_lists(auth, **kwargs):
496496
for list_name, subscribe in json_data.items():
497497
# TO DO: change this to take in any potential non-mailchimp, something like try: update_subscription(), except IndexNotFound: update_mailchimp_subscription()
498498
if list_name == settings.OSF_HELP_LIST:
499-
update_osf_help_mails_subscription(user=user, subscribe=subscribe)
499+
user.osf_mailing_lists[settings.OSF_HELP_LIST] = subscribe
500500
else:
501501
update_mailchimp_subscription(user, list_name, subscribe)
502502
else:
@@ -602,10 +602,6 @@ def impute_names(**kwargs):
602602
return auth_utils.impute_names(name)
603603

604604

605-
def update_osf_help_mails_subscription(user, subscribe):
606-
user.osf_mailing_lists[settings.OSF_HELP_LIST] = subscribe
607-
user.save()
608-
609605
@must_be_logged_in
610606
def serialize_names(**kwargs):
611607
user = kwargs['auth'].user

0 commit comments

Comments
 (0)