2022-04-14 23:39:22 +02:00
|
|
|
from django.db import transaction
|
|
|
|
from django.utils.timezone import now as timezone_now
|
|
|
|
|
|
|
|
from zerver.lib.realm_icon import realm_icon_url
|
2023-12-15 01:16:00 +01:00
|
|
|
from zerver.models import Realm, RealmAuditLog, UserProfile
|
|
|
|
from zerver.models.users import active_user_ids
|
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:39:22 +02:00
|
|
|
|
|
|
|
|
|
|
|
@transaction.atomic(durable=True)
|
|
|
|
def do_change_icon_source(
|
2024-07-12 02:30:23 +02:00
|
|
|
realm: Realm, icon_source: str, *, acting_user: UserProfile | None
|
2022-04-14 23:39:22 +02:00
|
|
|
) -> None:
|
|
|
|
realm.icon_source = icon_source
|
|
|
|
realm.icon_version += 1
|
|
|
|
realm.save(update_fields=["icon_source", "icon_version"])
|
|
|
|
|
|
|
|
event_time = timezone_now()
|
|
|
|
RealmAuditLog.objects.create(
|
|
|
|
realm=realm,
|
|
|
|
event_type=RealmAuditLog.REALM_ICON_SOURCE_CHANGED,
|
2023-07-13 19:46:06 +02:00
|
|
|
extra_data={"icon_source": icon_source, "icon_version": realm.icon_version},
|
2022-04-14 23:39:22 +02:00
|
|
|
event_time=event_time,
|
|
|
|
acting_user=acting_user,
|
|
|
|
)
|
|
|
|
|
|
|
|
event = dict(
|
|
|
|
type="realm",
|
|
|
|
op="update_dict",
|
|
|
|
property="icon",
|
|
|
|
data=dict(icon_source=realm.icon_source, icon_url=realm_icon_url(realm)),
|
|
|
|
)
|
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(
|
|
|
|
realm,
|
|
|
|
event,
|
|
|
|
active_user_ids(realm.id),
|
2022-04-14 23:39:22 +02:00
|
|
|
)
|