Files
zulip/zerver/actions/realm_domains.py
Anders Kaseorg a03ea9dc08 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>
(cherry picked from commit 7657cb4a0f)
2023-08-10 17:01:52 -05:00

121 lines
3.8 KiB
Python

from typing import Optional
import orjson
from django.db import transaction
from django.utils.timezone import now as timezone_now
from zerver.actions.realm_settings import do_set_realm_property
from zerver.models import (
Realm,
RealmAuditLog,
RealmDomain,
RealmDomainDict,
UserProfile,
active_user_ids,
get_realm_domains,
)
from zerver.tornado.django_api import send_event_on_commit
@transaction.atomic(durable=True)
def do_add_realm_domain(
realm: Realm, domain: str, allow_subdomains: bool, *, acting_user: Optional[UserProfile]
) -> RealmDomain:
realm_domain = RealmDomain.objects.create(
realm=realm, domain=domain, allow_subdomains=allow_subdomains
)
added_domain = RealmDomainDict(domain=domain, allow_subdomains=allow_subdomains)
RealmAuditLog.objects.create(
realm=realm,
acting_user=acting_user,
event_type=RealmAuditLog.REALM_DOMAIN_ADDED,
event_time=timezone_now(),
extra_data=orjson.dumps(
{
"realm_domains": get_realm_domains(realm),
"added_domain": added_domain,
}
).decode(),
)
event = dict(
type="realm_domains",
op="add",
realm_domain=RealmDomainDict(
domain=realm_domain.domain, allow_subdomains=realm_domain.allow_subdomains
),
)
send_event_on_commit(realm, event, active_user_ids(realm.id))
return realm_domain
@transaction.atomic(durable=True)
def do_change_realm_domain(
realm_domain: RealmDomain, allow_subdomains: bool, *, acting_user: Optional[UserProfile]
) -> None:
realm_domain.allow_subdomains = allow_subdomains
realm_domain.save(update_fields=["allow_subdomains"])
changed_domain = RealmDomainDict(
domain=realm_domain.domain,
allow_subdomains=realm_domain.allow_subdomains,
)
RealmAuditLog.objects.create(
realm=realm_domain.realm,
acting_user=acting_user,
event_type=RealmAuditLog.REALM_DOMAIN_CHANGED,
event_time=timezone_now(),
extra_data=orjson.dumps(
{
"realm_domains": get_realm_domains(realm_domain.realm),
"changed_domain": changed_domain,
}
).decode(),
)
event = dict(
type="realm_domains",
op="change",
realm_domain=dict(
domain=realm_domain.domain, allow_subdomains=realm_domain.allow_subdomains
),
)
send_event_on_commit(realm_domain.realm, event, active_user_ids(realm_domain.realm_id))
@transaction.atomic(durable=True)
def do_remove_realm_domain(
realm_domain: RealmDomain, *, acting_user: Optional[UserProfile]
) -> None:
realm = realm_domain.realm
domain = realm_domain.domain
realm_domain.delete()
removed_domain = RealmDomainDict(
domain=realm_domain.domain,
allow_subdomains=realm_domain.allow_subdomains,
)
RealmAuditLog.objects.create(
realm=realm,
acting_user=acting_user,
event_type=RealmAuditLog.REALM_DOMAIN_REMOVED,
event_time=timezone_now(),
extra_data=orjson.dumps(
{
"realm_domains": get_realm_domains(realm),
"removed_domain": removed_domain,
}
).decode(),
)
if RealmDomain.objects.filter(realm=realm).count() == 0 and realm.emails_restricted_to_domains:
# If this was the last realm domain, we mark the realm as no
# longer restricted to domain, because the feature doesn't do
# anything if there are no domains, and this is probably less
# confusing than the alternative.
do_set_realm_property(realm, "emails_restricted_to_domains", False, acting_user=acting_user)
event = dict(type="realm_domains", op="remove", domain=domain)
send_event_on_commit(realm, event, active_user_ids(realm.id))