Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
actions: Split out zerver.actions.realm_playgrounds.
Anders Kaseorge887abcf41b9
diff --git a/zerver/actions/realm_playgrounds.py b/zerver/actions/realm_playgrounds.pynew file mode 100644index 00000000000..f021b061198--- /dev/null+++ b/zerver/actions/realm_playgrounds.py@@ -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)))++[email protected](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++[email protected](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)diff --git a/zerver/lib/actions.py b/zerver/lib/actions.pyindex d627343b286..94d5a16d7b1 100644--- a/zerver/lib/actions.py+++ b/zerver/lib/actions.py@@ -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)))--[email protected](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--[email protected](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"""diff --git a/zerver/openapi/curl_param_value_generators.py b/zerver/openapi/curl_param_value_generators.pyindex ad05c934c3c..c02ef8cec60 100644--- a/zerver/openapi/curl_param_value_generators.py+++ b/zerver/openapi/curl_param_value_generators.py@@ -10,10 +10,10 @@ from typing import Any, Callable, Dict, List, Optional, Set, Tuplefrom django.utils.timezone import now as timezone_now+from zerver.actions.realm_playgrounds import do_add_realm_playgroundfrom zerver.lib.actions import (do_add_linkifier,do_add_reaction,- do_add_realm_playground,do_create_user,update_user_presence,)diff --git a/zerver/tests/test_audit_log.py b/zerver/tests/test_audit_log.pyindex 85404db0ebb..7cf2079c516 100644--- a/zerver/tests/test_audit_log.py+++ b/zerver/tests/test_audit_log.py@@ -6,12 +6,12 @@ from django.contrib.auth.password_validation import validate_passwordfrom django.utils.timezone import now as timezone_nowfrom analytics.models import StreamCount+from zerver.actions.realm_playgrounds import do_add_realm_playground, do_remove_realm_playgroundfrom 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,diff --git a/zerver/tests/test_events.py b/zerver/tests/test_events.pyindex ba48932a57b..f14ca1fa33d 100644--- a/zerver/tests/test_events.py+++ b/zerver/tests/test_events.py@@ -13,6 +13,7 @@ from unittest import mockimport orjsonfrom django.utils.timezone import now as timezone_now+from zerver.actions.realm_playgrounds import do_add_realm_playground, do_remove_realm_playgroundfrom zerver.actions.submessage import do_add_submessagefrom zerver.actions.typing import check_send_typing_notification, do_send_stream_typing_notificationfrom 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,diff --git a/zerver/tests/test_realm_playgrounds.py b/zerver/tests/test_realm_playgrounds.pyindex 6fcab68471b..7f804072b9d 100644--- a/zerver/tests/test_realm_playgrounds.py+++ b/zerver/tests/test_realm_playgrounds.py@@ -1,4 +1,4 @@-from zerver.lib.actions import do_add_realm_playground+from zerver.actions.realm_playgrounds import do_add_realm_playgroundfrom zerver.lib.test_classes import ZulipTestCasefrom zerver.models import RealmPlayground, get_realmdiff --git a/zerver/views/realm_playgrounds.py b/zerver/views/realm_playgrounds.pyindex 21b83cef409..1a8ce078b2c 100644--- a/zerver/views/realm_playgrounds.py+++ b/zerver/views/realm_playgrounds.py@@ -4,8 +4,8 @@ from django.core.exceptions import ValidationErrorfrom django.http import HttpRequest, HttpResponsefrom django.utils.translation import gettext as _+from zerver.actions.realm_playgrounds import do_add_realm_playground, do_remove_realm_playgroundfrom zerver.decorator import require_realm_admin-from zerver.lib.actions import do_add_realm_playground, do_remove_realm_playgroundfrom zerver.lib.exceptions import JsonableError, ValidationFailureErrorfrom zerver.lib.request import REQ, has_request_variablesfrom zerver.lib.response import json_success