2024-10-04 22:11:05 +02:00
|
|
|
from django.db import transaction
|
2022-04-14 23:40:10 +02:00
|
|
|
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
|
2024-10-04 22:11:05 +02:00
|
|
|
from zerver.models import Realm, RealmAuditLog, RealmExport, UserProfile
|
|
|
|
from zerver.models.realm_audit_logs import AuditLogEventType
|
django_api: Extract send_event_on_commit helper.
django-stubs 4.2.1 gives transaction.on_commit a more accurate type
annotation, but this exposed that mypy can’t handle the lambda default
parameters that we use to recapture loop variables such as
for stream_id in public_stream_ids:
peer_user_ids = …
event = …
transaction.on_commit(
lambda event=event, peer_user_ids=peer_user_ids: send_event(
realm, event, peer_user_ids
)
)
https://github.com/python/mypy/issues/15459
A workaround that mypy accepts is
transaction.on_commit(
(
lambda event, peer_user_ids: lambda: send_event(
realm, event, peer_user_ids
)
)(event, peer_user_ids)
)
But that’s kind of ugly and potentially error-prone, so let’s make a
helper function for this very common pattern.
send_event_on_commit(realm, event, peer_user_ids)
Signed-off-by: Anders Kaseorg <anders@zulip.com>
2023-06-17 20:53:07 +02:00
|
|
|
from zerver.tornado.django_api import send_event_on_commit
|
2022-04-14 23:40:10 +02:00
|
|
|
|
|
|
|
|
2023-06-28 18:08:17 +02:00
|
|
|
def notify_realm_export(realm: Realm) -> None:
|
|
|
|
event = dict(type="realm_export", exports=get_realm_exports_serialized(realm))
|
|
|
|
send_event_on_commit(realm, event, realm.get_human_admin_users().values_list("id", flat=True))
|
2022-04-14 23:40:10 +02:00
|
|
|
|
|
|
|
|
2024-10-04 22:11:05 +02:00
|
|
|
@transaction.atomic(durable=True)
|
|
|
|
def do_delete_realm_export(export_row: RealmExport, acting_user: UserProfile) -> None:
|
2024-09-26 12:18:55 +02:00
|
|
|
export_path = export_row.export_path
|
|
|
|
assert export_path is not None
|
2022-04-14 23:40:10 +02:00
|
|
|
|
2024-09-26 12:18:55 +02:00
|
|
|
delete_export_tarball(export_path)
|
2022-04-14 23:40:10 +02:00
|
|
|
|
2024-09-26 12:18:55 +02:00
|
|
|
export_row.status = RealmExport.DELETED
|
|
|
|
export_row.date_deleted = timezone_now()
|
|
|
|
export_row.save(update_fields=["status", "date_deleted"])
|
|
|
|
notify_realm_export(export_row.realm)
|
2024-10-04 22:11:05 +02:00
|
|
|
|
|
|
|
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},
|
|
|
|
)
|