2022-04-14 23:40:10 +02:00
|
|
|
import orjson
|
|
|
|
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 RealmAuditLog, UserProfile
|
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
|
|
|
|
|
|
|
|
|
|
|
def notify_realm_export(user_profile: UserProfile) -> None:
|
|
|
|
# In the future, we may want to send this event to all realm admins.
|
|
|
|
event = dict(type="realm_export", exports=get_realm_exports_serialized(user_profile))
|
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
|
|
|
send_event_on_commit(user_profile.realm, event, [user_profile.id])
|
2022-04-14 23:40:10 +02:00
|
|
|
|
|
|
|
|
|
|
|
def do_delete_realm_export(user_profile: UserProfile, export: RealmAuditLog) -> None:
|
|
|
|
# Give mypy a hint so it knows `orjson.loads`
|
|
|
|
# isn't being passed an `Optional[str]`.
|
|
|
|
export_extra_data = export.extra_data
|
|
|
|
assert export_extra_data is not None
|
|
|
|
export_data = orjson.loads(export_extra_data)
|
|
|
|
export_path = export_data.get("export_path")
|
|
|
|
|
|
|
|
if export_path:
|
|
|
|
# Allow removal even if the export failed.
|
|
|
|
delete_export_tarball(export_path)
|
|
|
|
|
|
|
|
export_data.update(deleted_timestamp=timezone_now().timestamp())
|
|
|
|
export.extra_data = orjson.dumps(export_data).decode()
|
|
|
|
export.save(update_fields=["extra_data"])
|
|
|
|
notify_realm_export(user_profile)
|