2024-07-12 02:30:17 +02:00
|
|
|
from typing import Optional
|
2022-04-14 23:32:18 +02:00
|
|
|
|
2023-07-24 21:14:42 +02:00
|
|
|
from django.core.exceptions import ValidationError
|
2022-04-14 23:32:18 +02:00
|
|
|
from django.db import transaction
|
|
|
|
from django.utils.timezone import now as timezone_now
|
|
|
|
|
2023-07-24 21:14:42 +02:00
|
|
|
from zerver.lib.exceptions import ValidationFailureError
|
2022-04-14 23:32:18 +02:00
|
|
|
from zerver.lib.types import RealmPlaygroundDict
|
2023-12-15 02:58:46 +01:00
|
|
|
from zerver.models import Realm, RealmAuditLog, RealmPlayground, UserProfile
|
|
|
|
from zerver.models.realm_playgrounds import get_realm_playgrounds
|
2023-12-15 01:16:00 +01:00
|
|
|
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:32:18 +02:00
|
|
|
|
|
|
|
|
2024-07-12 02:30:17 +02:00
|
|
|
def notify_realm_playgrounds(realm: Realm, realm_playgrounds: list[RealmPlaygroundDict]) -> None:
|
2022-04-14 23:32:18 +02:00
|
|
|
event = dict(type="realm_playgrounds", realm_playgrounds=realm_playgrounds)
|
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:32:18 +02:00
|
|
|
|
|
|
|
|
|
|
|
@transaction.atomic(durable=True)
|
2023-07-24 21:14:42 +02:00
|
|
|
def check_add_realm_playground(
|
2023-05-27 01:33:01 +02:00
|
|
|
realm: Realm,
|
|
|
|
*,
|
|
|
|
acting_user: Optional[UserProfile],
|
|
|
|
name: str,
|
|
|
|
pygments_language: str,
|
2023-05-27 05:04:50 +02:00
|
|
|
url_template: str,
|
2022-04-14 23:32:18 +02:00
|
|
|
) -> int:
|
2023-05-27 01:33:01 +02:00
|
|
|
realm_playground = RealmPlayground(
|
2023-05-27 03:29:40 +02:00
|
|
|
realm=realm,
|
|
|
|
name=name,
|
|
|
|
pygments_language=pygments_language,
|
2023-05-27 05:04:50 +02:00
|
|
|
url_template=url_template,
|
2023-05-27 01:33:01 +02:00
|
|
|
)
|
2023-07-24 21:14:42 +02:00
|
|
|
# The additional validations using url_template_validaton
|
|
|
|
# check_pygments_language, etc are included in full_clean.
|
|
|
|
# Because we want to avoid raising ValidationError from this check_*
|
|
|
|
# function, we do error handling here to turn it into a JsonableError.
|
|
|
|
try:
|
|
|
|
realm_playground.full_clean()
|
|
|
|
except ValidationError as e:
|
|
|
|
raise ValidationFailureError(e)
|
2022-04-14 23:32:18 +02:00
|
|
|
realm_playground.save()
|
|
|
|
realm_playgrounds = get_realm_playgrounds(realm)
|
|
|
|
RealmAuditLog.objects.create(
|
|
|
|
realm=realm,
|
|
|
|
acting_user=acting_user,
|
|
|
|
event_type=RealmAuditLog.REALM_PLAYGROUND_ADDED,
|
|
|
|
event_time=timezone_now(),
|
2023-07-13 19:46:06 +02:00
|
|
|
extra_data={
|
|
|
|
"realm_playgrounds": realm_playgrounds,
|
|
|
|
"added_playground": RealmPlaygroundDict(
|
|
|
|
id=realm_playground.id,
|
|
|
|
name=realm_playground.name,
|
|
|
|
pygments_language=realm_playground.pygments_language,
|
|
|
|
url_template=realm_playground.url_template,
|
|
|
|
),
|
|
|
|
},
|
2022-04-14 23:32:18 +02:00
|
|
|
)
|
|
|
|
notify_realm_playgrounds(realm, realm_playgrounds)
|
|
|
|
return realm_playground.id
|
|
|
|
|
|
|
|
|
|
|
|
@transaction.atomic(durable=True)
|
|
|
|
def do_remove_realm_playground(
|
|
|
|
realm: Realm, realm_playground: RealmPlayground, *, acting_user: Optional[UserProfile]
|
|
|
|
) -> None:
|
|
|
|
removed_playground = {
|
|
|
|
"name": realm_playground.name,
|
|
|
|
"pygments_language": realm_playground.pygments_language,
|
2023-05-27 03:29:40 +02:00
|
|
|
"url_template": realm_playground.url_template,
|
2022-04-14 23:32:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
realm_playground.delete()
|
|
|
|
realm_playgrounds = get_realm_playgrounds(realm)
|
|
|
|
|
|
|
|
RealmAuditLog.objects.create(
|
|
|
|
realm=realm,
|
|
|
|
acting_user=acting_user,
|
|
|
|
event_type=RealmAuditLog.REALM_PLAYGROUND_REMOVED,
|
|
|
|
event_time=timezone_now(),
|
2023-07-13 19:46:06 +02:00
|
|
|
extra_data={
|
|
|
|
"realm_playgrounds": realm_playgrounds,
|
|
|
|
"removed_playground": removed_playground,
|
|
|
|
},
|
2022-04-14 23:32:18 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
notify_realm_playgrounds(realm, realm_playgrounds)
|