audit_log: Record RealmAuditLog in do_change_notification_settings.

Removed logging with log_event and used RealmAuditLog instead.
Added tests in test_audit_log for the same.
This commit is contained in:
arpit551
2020-07-13 03:11:53 +05:30
committed by Tim Abbott
parent 54df9290b9
commit 0d79b55b2e
7 changed files with 49 additions and 16 deletions

View File

@@ -3755,11 +3755,13 @@ def do_create_realm(string_id: str, name: str,
return realm
def do_change_notification_settings(user_profile: UserProfile, name: str,
value: Union[bool, int, str], log: bool=True) -> None:
value: Union[bool, int, str],
acting_user: Optional[UserProfile]=None) -> None:
"""Takes in a UserProfile object, the name of a global notification
preference to update, and the value to update to
"""
old_value = getattr(user_profile, name)
notification_setting_type = UserProfile.notification_setting_types[name]
assert isinstance(value, notification_setting_type), (
f'Cannot update {name}: {value} is not an instance of {notification_setting_type}')
@@ -3775,8 +3777,14 @@ def do_change_notification_settings(user_profile: UserProfile, name: str,
'user': user_profile.email,
'notification_name': name,
'setting': value}
if log:
log_event(event)
event_time = timezone_now()
RealmAuditLog.objects.create(
realm=user_profile.realm, event_type=RealmAuditLog.USER_NOTIFICATION_SETTINGS_CHANGED, event_time=event_time,
acting_user=acting_user, modified_user=user_profile, extra_data=ujson.dumps({
RealmAuditLog.OLD_VALUE: {'property': name, 'value': old_value},
RealmAuditLog.NEW_VALUE: {'property': name, 'value': value}
}))
send_event(user_profile.realm, event, [user_profile.id])
def do_change_enter_sends(user_profile: UserProfile, enter_sends: bool) -> None: