def update(self, instance, validated_data): if instance.notification.mandatory: raise serializers.ValidationError( 'Attempting to set [%s] to %s. Mandatory notifications can\'t ' 'be modified' % (instance.notification.short, validated_data.get('enabled'))) enabled = validated_data['enabled'] request = self.context['request'] current_user = request.user remote_by_id = {l.id: l for l in notifications.REMOTE_NOTIFICATIONS} if instance.notification_id in remote_by_id: notification = remote_by_id[instance.notification_id] if not enabled: unsubscribe_newsletter(current_user, notification.basket_newsletter_id) elif enabled: subscribe_newsletter(current_user, notification.basket_newsletter_id, request=request) elif 'enabled' in validated_data: # Only save if non-mandatory and 'enabled' is set. # Ignore other fields. instance.enabled = validated_data['enabled'] # Not .update because some of the instances are new. instance.save() return instance
def update(self, instance, validated_data): if instance.notification.mandatory: raise serializers.ValidationError( 'Attempting to set [%s] to %s. Mandatory notifications can\'t ' 'be modified' % (instance.notification.short, validated_data.get('enabled'))) enabled = validated_data['enabled'] request = self.context['request'] current_user = request.user remote_by_id = { l.id: l for l in notifications.REMOTE_NOTIFICATIONS} use_basket = waffle.switch_is_active('activate-basket-sync') if use_basket and instance.notification_id in remote_by_id: notification = remote_by_id[instance.notification_id] if not enabled: unsubscribe_newsletter( current_user, notification.basket_newsletter_id) elif enabled: subscribe_newsletter( current_user, notification.basket_newsletter_id, request=request) elif 'enabled' in validated_data: # Only save if non-mandatory and 'enabled' is set. # Ignore other fields. instance.enabled = validated_data['enabled'] # Not .update because some of the instances are new. instance.save() return instance
def save(self, log_for_developer=True): user = super(UserEditForm, self).save(commit=False) data = self.cleaned_data photo = data['photo'] if photo: user.picture_type = 'image/png' tmp_destination = user.picture_path_original with storage.open(tmp_destination, 'wb') as fh: for chunk in photo.chunks(): fh.write(chunk) tasks.resize_photo.delay( tmp_destination, user.picture_path, set_modified_on=user.serializable_reference()) visible_notifications = (notifications.NOTIFICATIONS_BY_ID if self.instance.is_developer else notifications.NOTIFICATIONS_BY_ID_NOT_DEV) for (notification_id, notification) in visible_notifications.items(): enabled = (notification.mandatory or (str(notification_id) in data['notifications'])) UserNotification.objects.update_or_create( user=self.instance, notification_id=notification_id, defaults={'enabled': enabled}) if waffle.switch_is_active('activate-basket-sync'): by_basket_id = notifications.REMOTE_NOTIFICATIONS_BY_BASKET_ID for basket_id, notification in by_basket_id.items(): needs_subscribe = str(notification.id) in data['notifications'] needs_unsubscribe = (str(notification.id) not in data['notifications']) if needs_subscribe: subscribe_newsletter(self.instance, basket_id, request=self.request) elif needs_unsubscribe: unsubscribe_newsletter(self.instance, basket_id) log.debug(u'User (%s) updated their profile' % user) user.save() return user
def save(self, log_for_developer=True): user = super(UserEditForm, self).save(commit=False) data = self.cleaned_data photo = data['photo'] if photo: user.picture_type = 'image/png' tmp_destination = user.picture_path_original with storage.open(tmp_destination, 'wb') as fh: for chunk in photo.chunks(): fh.write(chunk) tasks.resize_photo.delay( tmp_destination, user.picture_path, set_modified_on=user.serializable_reference()) visible_notifications = ( notifications.NOTIFICATIONS_BY_ID if self.instance.is_developer else notifications.NOTIFICATIONS_BY_ID_NOT_DEV) for (notification_id, notification) in visible_notifications.items(): enabled = (notification.mandatory or (str(notification_id) in data['notifications'])) UserNotification.objects.update_or_create( user=self.instance, notification_id=notification_id, defaults={'enabled': enabled}) if waffle.switch_is_active('activate-basket-sync'): by_basket_id = notifications.REMOTE_NOTIFICATIONS_BY_BASKET_ID for basket_id, notification in by_basket_id.items(): needs_subscribe = str(notification.id) in data['notifications'] needs_unsubscribe = ( str(notification.id) not in data['notifications']) if needs_subscribe: subscribe_newsletter( self.instance, basket_id, request=self.request) elif needs_unsubscribe: unsubscribe_newsletter(self.instance, basket_id) log.debug(u'User (%s) updated their profile' % user) user.save() return user