do_set_realm_property: Noop if value isn't really changing.

It makes no sense to do operations if the value isn't changing. In
particular, this creates RealmAuditLog entries and sends useless events.
This commit is contained in:
Mateusz Mandera 2023-12-11 20:32:24 +01:00 committed by Tim Abbott
parent cf68f8ae24
commit b09f3a2da1
2 changed files with 16 additions and 9 deletions

View File

@ -56,6 +56,9 @@ def do_set_realm_property(
), f"Cannot update {name}: {value} is not an instance of {property_type}"
old_value = getattr(realm, name)
if old_value == value:
return
setattr(realm, name, value)
realm.save(update_fields=[name])

View File

@ -3532,7 +3532,11 @@ class RealmPropertyActionTest(BaseAction):
if vals is None:
raise AssertionError(f"No test created for {name}")
now = timezone_now()
original_val = getattr(self.user_profile.realm, name)
do_set_realm_property(self.user_profile.realm, name, vals[0], acting_user=self.user_profile)
if vals[0] != original_val:
self.assertEqual(
RealmAuditLog.objects.filter(
realm=self.user_profile.realm,