2022-04-14 23:37:16 +02:00
|
|
|
from django.db import transaction
|
|
|
|
from django.utils.timezone import now as timezone_now
|
|
|
|
|
|
|
|
from zerver.lib.realm_logo import get_realm_logo_data
|
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:37:16 +02:00
|
|
|
|
|
|
|
|
|
|
|
@transaction.atomic(durable=True)
|
|
|
|
def do_change_logo_source(
|
2024-07-12 02:30:23 +02:00
|
|
|
realm: Realm, logo_source: str, night: bool, *, acting_user: UserProfile | None
|
2022-04-14 23:37:16 +02:00
|
|
|
) -> None:
|
|
|
|
if not night:
|
|
|
|
realm.logo_source = logo_source
|
|
|
|
realm.logo_version += 1
|
|
|
|
realm.save(update_fields=["logo_source", "logo_version"])
|
|
|
|
|
|
|
|
else:
|
|
|
|
realm.night_logo_source = logo_source
|
|
|
|
realm.night_logo_version += 1
|
|
|
|
realm.save(update_fields=["night_logo_source", "night_logo_version"])
|
|
|
|
|
|
|
|
RealmAuditLog.objects.create(
|
|
|
|
event_type=RealmAuditLog.REALM_LOGO_CHANGED,
|
|
|
|
realm=realm,
|
|
|
|
event_time=timezone_now(),
|
|
|
|
acting_user=acting_user,
|
|
|
|
)
|
|
|
|
|
|
|
|
event = dict(
|
|
|
|
type="realm",
|
|
|
|
op="update_dict",
|
|
|
|
property="night_logo" if night else "logo",
|
|
|
|
data=get_realm_logo_data(realm, night),
|
|
|
|
)
|
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))
|