actions: Split out zerver.actions.realm_playgrounds.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
Anders Kaseorg 2022-04-14 14:32:18 -07:00
parent 3a135b04d9
commit e887abcf41
7 changed files with 87 additions and 76 deletions

View File

@ -0,0 +1,82 @@
from typing import Any, List, Optional
import orjson
from django.db import transaction
from django.utils.timezone import now as timezone_now
from zerver.lib.types import RealmPlaygroundDict
from zerver.models import (
Realm,
RealmAuditLog,
RealmPlayground,
UserProfile,
active_user_ids,
get_realm_playgrounds,
)
from zerver.tornado.django_api import send_event
def notify_realm_playgrounds(realm: Realm, realm_playgrounds: List[RealmPlaygroundDict]) -> None:
event = dict(type="realm_playgrounds", realm_playgrounds=realm_playgrounds)
transaction.on_commit(lambda: send_event(realm, event, active_user_ids(realm.id)))
@transaction.atomic(durable=True)
def do_add_realm_playground(
realm: Realm, *, acting_user: Optional[UserProfile], **kwargs: Any
) -> int:
realm_playground = RealmPlayground(realm=realm, **kwargs)
# We expect full_clean to always pass since a thorough input validation
# is performed in the view (using check_url, check_pygments_language, etc)
# before calling this function.
realm_playground.full_clean()
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(),
extra_data=orjson.dumps(
{
"realm_playgrounds": realm_playgrounds,
"added_playground": RealmPlaygroundDict(
id=realm_playground.id,
name=realm_playground.name,
pygments_language=realm_playground.pygments_language,
url_prefix=realm_playground.url_prefix,
),
}
).decode(),
)
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,
"url_prefix": realm_playground.url_prefix,
}
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(),
extra_data=orjson.dumps(
{
"realm_playgrounds": realm_playgrounds,
"removed_playground": removed_playground,
}
).decode(),
)
notify_realm_playgrounds(realm, realm_playgrounds)

View File

@ -185,7 +185,6 @@ from zerver.lib.types import (
ProfileFieldData,
RawStreamDict,
RawSubscriptionDict,
RealmPlaygroundDict,
SubscriptionInfo,
SubscriptionStreamDict,
UnspecifiedValue,
@ -241,7 +240,6 @@ from zerver.models import (
RealmDomain,
RealmEmoji,
RealmFilter,
RealmPlayground,
RealmUserDefault,
Recipient,
ScheduledEmail,
@ -275,7 +273,6 @@ from zerver.models import (
get_old_unclaimed_attachments,
get_realm,
get_realm_domains,
get_realm_playgrounds,
get_stream,
get_stream_by_id_in_realm,
get_system_bot,
@ -7878,72 +7875,6 @@ def do_remove_realm_domain(
transaction.on_commit(lambda: send_event(realm, event, active_user_ids(realm.id)))
def notify_realm_playgrounds(realm: Realm, realm_playgrounds: List[RealmPlaygroundDict]) -> None:
event = dict(type="realm_playgrounds", realm_playgrounds=realm_playgrounds)
transaction.on_commit(lambda: send_event(realm, event, active_user_ids(realm.id)))
@transaction.atomic(durable=True)
def do_add_realm_playground(
realm: Realm, *, acting_user: Optional[UserProfile], **kwargs: Any
) -> int:
realm_playground = RealmPlayground(realm=realm, **kwargs)
# We expect full_clean to always pass since a thorough input validation
# is performed in the view (using check_url, check_pygments_language, etc)
# before calling this function.
realm_playground.full_clean()
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(),
extra_data=orjson.dumps(
{
"realm_playgrounds": realm_playgrounds,
"added_playground": RealmPlaygroundDict(
id=realm_playground.id,
name=realm_playground.name,
pygments_language=realm_playground.pygments_language,
url_prefix=realm_playground.url_prefix,
),
}
).decode(),
)
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,
"url_prefix": realm_playground.url_prefix,
}
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(),
extra_data=orjson.dumps(
{
"realm_playgrounds": realm_playgrounds,
"removed_playground": removed_playground,
}
).decode(),
)
notify_realm_playgrounds(realm, realm_playgrounds)
def get_occupied_streams(realm: Realm) -> QuerySet:
# TODO: Make a generic stub for QuerySet
"""Get streams with subscribers"""

View File

@ -10,10 +10,10 @@ from typing import Any, Callable, Dict, List, Optional, Set, Tuple
from django.utils.timezone import now as timezone_now
from zerver.actions.realm_playgrounds import do_add_realm_playground
from zerver.lib.actions import (
do_add_linkifier,
do_add_reaction,
do_add_realm_playground,
do_create_user,
update_user_presence,
)

View File

@ -6,12 +6,12 @@ from django.contrib.auth.password_validation import validate_password
from django.utils.timezone import now as timezone_now
from analytics.models import StreamCount
from zerver.actions.realm_playgrounds import do_add_realm_playground, do_remove_realm_playground
from zerver.lib.actions import (
bulk_add_subscriptions,
bulk_remove_subscriptions,
do_activate_mirror_dummy_user,
do_add_realm_domain,
do_add_realm_playground,
do_change_avatar_fields,
do_change_bot_owner,
do_change_default_all_public_streams,
@ -33,7 +33,6 @@ from zerver.lib.actions import (
do_reactivate_user,
do_regenerate_api_key,
do_remove_realm_domain,
do_remove_realm_playground,
do_rename_stream,
do_set_realm_authentication_methods,
do_set_realm_message_editing,

View File

@ -13,6 +13,7 @@ from unittest import mock
import orjson
from django.utils.timezone import now as timezone_now
from zerver.actions.realm_playgrounds import do_add_realm_playground, do_remove_realm_playground
from zerver.actions.submessage import do_add_submessage
from zerver.actions.typing import check_send_typing_notification, do_send_stream_typing_notification
from zerver.actions.user_groups import (
@ -33,7 +34,6 @@ from zerver.lib.actions import (
do_add_linkifier,
do_add_reaction,
do_add_realm_domain,
do_add_realm_playground,
do_add_streams_to_default_stream_group,
do_change_avatar_fields,
do_change_bot_owner,
@ -77,7 +77,6 @@ from zerver.lib.actions import (
do_remove_realm_custom_profile_field,
do_remove_realm_domain,
do_remove_realm_emoji,
do_remove_realm_playground,
do_remove_streams_from_default_stream_group,
do_rename_stream,
do_revoke_multi_use_invite,

View File

@ -1,4 +1,4 @@
from zerver.lib.actions import do_add_realm_playground
from zerver.actions.realm_playgrounds import do_add_realm_playground
from zerver.lib.test_classes import ZulipTestCase
from zerver.models import RealmPlayground, get_realm

View File

@ -4,8 +4,8 @@ from django.core.exceptions import ValidationError
from django.http import HttpRequest, HttpResponse
from django.utils.translation import gettext as _
from zerver.actions.realm_playgrounds import do_add_realm_playground, do_remove_realm_playground
from zerver.decorator import require_realm_admin
from zerver.lib.actions import do_add_realm_playground, do_remove_realm_playground
from zerver.lib.exceptions import JsonableError, ValidationFailureError
from zerver.lib.request import REQ, has_request_variables
from zerver.lib.response import json_success