From 6f93f07844f332c86883b33a8cb8a0c5e7da38eb Mon Sep 17 00:00:00 2001 From: Sahil Batra Date: Tue, 15 Mar 2022 15:40:29 +0530 Subject: [PATCH] types: Create a new TypedDict RealmPlaygroundDict for playground objects. This commit creates a new TypedDict RealmPlaygroundDict for realm playground objects. Now the list of playgrounds in the events sent to clients and the "added_playground" field of RealmAuditLog entry use RealmPlaygroundDict instead of Dict. --- zerver/lib/actions.py | 17 ++++++++--------- zerver/lib/types.py | 7 +++++++ zerver/models.py | 7 ++++--- zerver/tests/test_audit_log.py | 13 +++++++------ 4 files changed, 26 insertions(+), 18 deletions(-) diff --git a/zerver/lib/actions.py b/zerver/lib/actions.py index 93108d7599..2e8ea230a3 100644 --- a/zerver/lib/actions.py +++ b/zerver/lib/actions.py @@ -182,6 +182,7 @@ from zerver.lib.types import ( ProfileFieldData, RawStreamDict, RawSubscriptionDict, + RealmPlaygroundDict, SubscriptionInfo, SubscriptionStreamDict, UnspecifiedValue, @@ -8097,9 +8098,7 @@ 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[Dict[str, Union[int, str]]] -) -> None: +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))) @@ -8123,12 +8122,12 @@ def do_add_realm_playground( extra_data=orjson.dumps( { "realm_playgrounds": realm_playgrounds, - "added_playground": { - "id": realm_playground.id, - "name": realm_playground.name, - "pygments_language": realm_playground.pygments_language, - "url_prefix": realm_playground.url_prefix, - }, + "added_playground": RealmPlaygroundDict( + id=realm_playground.id, + name=realm_playground.name, + pygments_language=realm_playground.pygments_language, + url_prefix=realm_playground.url_prefix, + ), } ).decode(), ) diff --git a/zerver/lib/types.py b/zerver/lib/types.py index 43db3cb4a6..66544dcdce 100644 --- a/zerver/lib/types.py +++ b/zerver/lib/types.py @@ -227,3 +227,10 @@ class SubscriptionInfo: subscriptions: List[SubscriptionStreamDict] unsubscribed: List[SubscriptionStreamDict] never_subscribed: List[NeverSubscribedStreamDict] + + +class RealmPlaygroundDict(TypedDict): + id: int + name: str + pygments_language: str + url_prefix: str diff --git a/zerver/models.py b/zerver/models.py index 1d1527da5c..2201048e9d 100644 --- a/zerver/models.py +++ b/zerver/models.py @@ -86,6 +86,7 @@ from zerver.lib.types import ( ProfileData, ProfileDataElementBase, ProfileDataElementValue, + RealmPlaygroundDict, RealmUserValidator, UnspecifiedValue, UserFieldElement, @@ -1381,11 +1382,11 @@ class RealmPlayground(models.Model): return f"" -def get_realm_playgrounds(realm: Realm) -> List[Dict[str, Union[int, str]]]: - playgrounds: List[Dict[str, Union[int, str]]] = [] +def get_realm_playgrounds(realm: Realm) -> List[RealmPlaygroundDict]: + playgrounds: List[RealmPlaygroundDict] = [] for playground in RealmPlayground.objects.filter(realm=realm).all(): playgrounds.append( - dict( + RealmPlaygroundDict( id=playground.id, name=playground.name, pygments_language=playground.pygments_language, diff --git a/zerver/tests/test_audit_log.py b/zerver/tests/test_audit_log.py index d9b98b9084..85404db0eb 100644 --- a/zerver/tests/test_audit_log.py +++ b/zerver/tests/test_audit_log.py @@ -44,6 +44,7 @@ from zerver.lib.message import get_last_message_id from zerver.lib.stream_traffic import get_streams_traffic from zerver.lib.streams import create_stream_if_needed from zerver.lib.test_classes import ZulipTestCase +from zerver.lib.types import RealmPlaygroundDict from zerver.models import ( Message, Realm, @@ -745,12 +746,12 @@ class TestRealmAuditLog(ZulipTestCase): pygments_language="Python", url_prefix="https://python.example.com", ) - added_playground: Dict[str, Union[int, str]] = { - "id": playground_id, - "name": "Python playground", - "pygments_language": "Python", - "url_prefix": "https://python.example.com", - } + added_playground = RealmPlaygroundDict( + id=playground_id, + name="Python playground", + pygments_language="Python", + url_prefix="https://python.example.com", + ) expected_extra_data = { "realm_playgrounds": intial_playgrounds + [added_playground], "added_playground": added_playground,