do_set_realm_property: Use transaction.atomic.

This commit is contained in:
Mateusz Mandera 2021-12-12 13:47:02 +01:00 committed by Tim Abbott
parent b3df4ef23f
commit 0dd97eeaab
2 changed files with 14 additions and 10 deletions

View File

@ -843,6 +843,7 @@ def active_humans_in_realm(realm: Realm) -> Sequence[UserProfile]:
return UserProfile.objects.filter(realm=realm, is_active=True, is_bot=False) return UserProfile.objects.filter(realm=realm, is_active=True, is_bot=False)
@transaction.atomic(savepoint=False)
def do_set_realm_property( def do_set_realm_property(
realm: Realm, name: str, value: Any, *, acting_user: Optional[UserProfile] realm: Realm, name: str, value: Any, *, acting_user: Optional[UserProfile]
) -> None: ) -> None:
@ -864,7 +865,7 @@ def do_set_realm_property(
property=name, property=name,
value=value, value=value,
) )
send_event(realm, event, active_user_ids(realm.id)) transaction.on_commit(lambda: send_event(realm, event, active_user_ids(realm.id)))
event_time = timezone_now() event_time = timezone_now()
RealmAuditLog.objects.create( RealmAuditLog.objects.create(
@ -892,12 +893,14 @@ def do_set_realm_property(
user_profiles = UserProfile.objects.filter(realm=realm, is_bot=False) user_profiles = UserProfile.objects.filter(realm=realm, is_bot=False)
for user_profile in user_profiles: for user_profile in user_profiles:
user_profile.email = get_display_email_address(user_profile) user_profile.email = get_display_email_address(user_profile)
# TODO: Design a bulk event for this or force-reload all clients
send_user_email_update_event(user_profile)
UserProfile.objects.bulk_update(user_profiles, ["email"]) UserProfile.objects.bulk_update(user_profiles, ["email"])
for user_profile in user_profiles: for user_profile in user_profiles:
flush_user_profile(sender=UserProfile, instance=user_profile) transaction.on_commit(
lambda: flush_user_profile(sender=UserProfile, instance=user_profile)
)
# TODO: Design a bulk event for this or force-reload all clients
send_user_email_update_event(user_profile)
def do_set_realm_authentication_methods( def do_set_realm_authentication_methods(

View File

@ -329,12 +329,13 @@ class PermissionTest(ZulipTestCase):
############################################################# #############################################################
# Now, switch email address visibility, check client_gravatar # Now, switch email address visibility, check client_gravatar
# is automatically disabled for the user. # is automatically disabled for the user.
do_set_realm_property( with self.captureOnCommitCallbacks(execute=True):
user.realm, do_set_realm_property(
"email_address_visibility", user.realm,
Realm.EMAIL_ADDRESS_VISIBILITY_ADMINS, "email_address_visibility",
acting_user=None, Realm.EMAIL_ADDRESS_VISIBILITY_ADMINS,
) acting_user=None,
)
result = self.client_get("/json/users", {"client_gravatar": "true"}) result = self.client_get("/json/users", {"client_gravatar": "true"})
self.assert_json_success(result) self.assert_json_success(result)
members = result.json()["members"] members = result.json()["members"]