do_update_user_custom_profile_data: Don't notify if value not changed.

This commit is contained in:
Mateusz Mandera
2019-10-01 04:13:32 +02:00
committed by Tim Abbott
parent 7d46da0fce
commit d66cbd2832
2 changed files with 27 additions and 0 deletions

View File

@@ -5465,6 +5465,14 @@ def do_update_user_custom_profile_data(user_profile: UserProfile,
field_value, created = CustomProfileFieldValue.objects.get_or_create(
user_profile=user_profile,
field_id=field['id'])
if not created and field_value.value == str(field['value']):
# If the field value isn't actually being changed to a different one,
# and always_notify is disabled, we have nothing to do here for this field.
# Note: field_value.value is a TextField() so we need to cast field['value']
# to a string for the comparison in this if.
continue
field_value.value = field['value']
if field_value.field.is_renderable():
field_value.rendered_value = render_stream_description(str(field['value']))

View File

@@ -12,6 +12,8 @@ from zerver.models import CustomProfileField, \
from zerver.lib.external_accounts import DEFAULT_EXTERNAL_ACCOUNTS
import ujson
import mock
class CustomProfileFieldTestCase(ZulipTestCase):
def setUp(self) -> None:
self.realm = get_realm("zulip")
@@ -595,6 +597,23 @@ class UpdateCustomProfileFieldTest(CustomProfileFieldTestCase):
self.assertIsNotNone(rendered_value)
self.assertEqual("<p><strong><em>beware</em></strong> of jealousy...</p>", rendered_value)
def test_do_update_value_not_changed(self) -> None:
iago = self.example_user("iago")
self.login(iago.email)
realm = get_realm("zulip")
# Set field value:
field = CustomProfileField.objects.get(name="Mentor", realm=realm)
data = [{'id': field.id,
'value': [self.example_user("aaron").id]}] # type: List[Dict[str, Union[int, str, List[int]]]]
do_update_user_custom_profile_data(iago, data)
with mock.patch("zerver.lib.actions.notify_user_update_custom_profile_data") as mock_notify:
# Attempting to "update" the field value, when it wouldn't actually change,
# if always_notify is disabled, shouldn't trigger notify.
do_update_user_custom_profile_data(iago, data)
mock_notify.assert_not_called()
class ListCustomProfileFieldTest(CustomProfileFieldTestCase):
def test_list(self) -> None:
self.login(self.example_email("iago"))