38
38
from osf .models .user_message import MessageTypes
39
39
from osf .models .provider import AbstractProviderGroupObjectPermission
40
40
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
42
43
from website .settings import MAILCHIMP_GENERAL_LIST , OSF_HELP_LIST , CONFIRM_REGISTRATIONS_BY_EMAIL
43
44
from website .util import api_v2_url
44
45
@@ -543,10 +544,14 @@ class UserSettingsUpdateSerializer(UserSettingsSerializer):
543
544
544
545
def update_email_preferences (self , instance , attr , value ):
545
546
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 :
548
549
update_mailchimp_subscription (instance , self .MAP_MAIL [attr ], value )
550
+ else :
551
+ raise exceptions .ValidationError (detail = 'Invalid email preference.' )
552
+
549
553
instance .save ()
554
+ return instance
550
555
551
556
def update_two_factor (self , instance , value , two_factor_addon ):
552
557
if value :
@@ -581,6 +586,15 @@ def to_representation(self, instance):
581
586
Overriding to_representation allows using different serializers for the request and response.
582
587
"""
583
588
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
+
584
598
return UserSettingsSerializer (instance = instance , context = context ).data
585
599
586
600
def update (self , instance , validated_data ):
@@ -597,6 +611,11 @@ def update(self, instance, validated_data):
597
611
elif attr in self .MAP_MAIL .keys ():
598
612
self .update_email_preferences (instance , attr , value )
599
613
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
+
600
619
return instance
601
620
602
621
0 commit comments