diff --git a/zerver/actions/realm_export.py b/zerver/actions/realm_export.py index 1ae86b50f3..154d6cdd4a 100644 --- a/zerver/actions/realm_export.py +++ b/zerver/actions/realm_export.py @@ -1,8 +1,10 @@ +from django.db import transaction from django.utils.timezone import now as timezone_now from zerver.lib.export import get_realm_exports_serialized from zerver.lib.upload import delete_export_tarball -from zerver.models import Realm, RealmExport +from zerver.models import Realm, RealmAuditLog, RealmExport, UserProfile +from zerver.models.realm_audit_logs import AuditLogEventType from zerver.tornado.django_api import send_event_on_commit @@ -11,7 +13,8 @@ def notify_realm_export(realm: Realm) -> None: send_event_on_commit(realm, event, realm.get_human_admin_users().values_list("id", flat=True)) -def do_delete_realm_export(export_row: RealmExport) -> None: +@transaction.atomic(durable=True) +def do_delete_realm_export(export_row: RealmExport, acting_user: UserProfile) -> None: export_path = export_row.export_path assert export_path is not None @@ -21,3 +24,11 @@ def do_delete_realm_export(export_row: RealmExport) -> None: export_row.date_deleted = timezone_now() export_row.save(update_fields=["status", "date_deleted"]) notify_realm_export(export_row.realm) + + RealmAuditLog.objects.create( + acting_user=acting_user, + realm=export_row.realm, + event_type=AuditLogEventType.REALM_EXPORT_DELETED, + event_time=export_row.date_deleted, + extra_data={"realm_export_id": export_row.id}, + ) diff --git a/zerver/models/realm_audit_logs.py b/zerver/models/realm_audit_logs.py index f311178f4c..a9487ea197 100644 --- a/zerver/models/realm_audit_logs.py +++ b/zerver/models/realm_audit_logs.py @@ -66,6 +66,7 @@ class AuditLogEventType(IntEnum): REALM_EMOJI_REMOVED = 227 REALM_LINKIFIERS_REORDERED = 228 REALM_IMPORTED = 229 + REALM_EXPORT_DELETED = 230 SUBSCRIPTION_CREATED = 301 SUBSCRIPTION_ACTIVATED = 302 diff --git a/zerver/tests/test_events.py b/zerver/tests/test_events.py index f0cb6ac4d9..fa4358dbb7 100644 --- a/zerver/tests/test_events.py +++ b/zerver/tests/test_events.py @@ -3422,6 +3422,12 @@ class NormalActionsTest(BaseAction): has_failed_timestamp=False, ) + audit_log = RealmAuditLog.objects.last() + assert audit_log is not None + self.assertEqual(audit_log.event_type, AuditLogEventType.REALM_EXPORT_DELETED) + self.assertEqual(audit_log.acting_user, self.user_profile) + self.assertEqual(audit_log.extra_data["realm_export_id"], export_row_id) + def test_notify_realm_export_on_failure(self) -> None: do_change_user_role( self.user_profile, UserProfile.ROLE_REALM_ADMINISTRATOR, acting_user=None diff --git a/zerver/views/realm_export.py b/zerver/views/realm_export.py index 98a517f351..ed5aff1851 100644 --- a/zerver/views/realm_export.py +++ b/zerver/views/realm_export.py @@ -106,7 +106,7 @@ def delete_realm_export(request: HttpRequest, user: UserProfile, export_id: int) raise JsonableError(_("Export failed, nothing to delete")) if export_row.status in [RealmExport.REQUESTED, RealmExport.STARTED]: raise JsonableError(_("Export still in progress")) - do_delete_realm_export(export_row) + do_delete_realm_export(export_row, user) return json_success(request)