Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

realm_playgrounds: Refactor error handling for validation on creation.

Zixuan James Life1a2f6f02e3
diff --git a/web/src/realm_playground.ts b/web/src/realm_playground.ts
index 39cddbbcd1c..1ac3c5931dc 100644
--- a/web/src/realm_playground.ts
+++ b/web/src/realm_playground.ts
@@ -7,7 +7,7 @@ type RealmPlayground = {
id: number;
name: string;
pygments_language: string;
- url_prefix: string;
+ url_template: string;
};
const map_language_to_playground_info = new Map<
diff --git a/zerver/actions/realm_playgrounds.py b/zerver/actions/realm_playgrounds.py
index e246256f267..0f2da6265f7 100644
--- a/zerver/actions/realm_playgrounds.py
+++ b/zerver/actions/realm_playgrounds.py
@@ -1,9 +1,11 @@
from typing import List, Optional
import orjson
+from django.core.exceptions import ValidationError
from django.db import transaction
from django.utils.timezone import now as timezone_now
+from zerver.lib.exceptions import ValidationFailureError
from zerver.lib.types import RealmPlaygroundDict
from zerver.models import (
Realm,
@@ -22,7 +24,7 @@ def notify_realm_playgrounds(realm: Realm, realm_playgrounds: List[RealmPlaygrou
@transaction.atomic(durable=True)
-def do_add_realm_playground(
+def check_add_realm_playground(
realm: Realm,
*,
acting_user: Optional[UserProfile],
@@ -36,7 +38,14 @@ def do_add_realm_playground(
pygments_language=pygments_language,
url_template=url_template,
)
- realm_playground.full_clean()
+ # 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)
realm_playground.save()
realm_playgrounds = get_realm_playgrounds(realm)
RealmAuditLog.objects.create(
diff --git a/zerver/openapi/curl_param_value_generators.py b/zerver/openapi/curl_param_value_generators.py
index 37b3d0960d9..af4c9883ecd 100644
--- a/zerver/openapi/curl_param_value_generators.py
+++ b/zerver/openapi/curl_param_value_generators.py
@@ -14,7 +14,7 @@ from zerver.actions.create_user import do_create_user
from zerver.actions.presence import update_user_presence
from zerver.actions.reactions import do_add_reaction
from zerver.actions.realm_linkifiers import do_add_linkifier
-from zerver.actions.realm_playgrounds import do_add_realm_playground
+from zerver.actions.realm_playgrounds import check_add_realm_playground
from zerver.lib.events import do_events_register
from zerver.lib.initial_password import initial_password
from zerver.lib.test_classes import ZulipTestCase
@@ -302,7 +302,7 @@ def add_realm_playground() -> Dict[str, object]:
@openapi_param_value_generator(["/realm/playgrounds/{playground_id}:delete"])
def remove_realm_playground() -> Dict[str, object]:
- playground_id = do_add_realm_playground(
+ playground_id = check_add_realm_playground(
get_realm("zulip"),
acting_user=None,
name="Python playground",
diff --git a/zerver/tests/test_audit_log.py b/zerver/tests/test_audit_log.py
index 104a56d54c4..e369b3bf3e3 100644
--- a/zerver/tests/test_audit_log.py
+++ b/zerver/tests/test_audit_log.py
@@ -29,7 +29,7 @@ from zerver.actions.realm_linkifiers import (
do_remove_linkifier,
do_update_linkifier,
)
-from zerver.actions.realm_playgrounds import do_add_realm_playground, do_remove_realm_playground
+from zerver.actions.realm_playgrounds import check_add_realm_playground, do_remove_realm_playground
from zerver.actions.realm_settings import (
do_deactivate_realm,
do_reactivate_realm,
@@ -861,7 +861,7 @@ class TestRealmAuditLog(ZulipTestCase):
user = self.example_user("iago")
initial_playgrounds = get_realm_playgrounds(user.realm)
now = timezone_now()
- playground_id = do_add_realm_playground(
+ playground_id = check_add_realm_playground(
user.realm,
acting_user=user,
name="Python playground",
diff --git a/zerver/tests/test_events.py b/zerver/tests/test_events.py
index 8645d19b154..95815cbd119 100644
--- a/zerver/tests/test_events.py
+++ b/zerver/tests/test_events.py
@@ -66,7 +66,7 @@ from zerver.actions.realm_linkifiers import (
do_update_linkifier,
)
from zerver.actions.realm_logo import do_change_logo_source
-from zerver.actions.realm_playgrounds import do_add_realm_playground, do_remove_realm_playground
+from zerver.actions.realm_playgrounds import check_add_realm_playground, do_remove_realm_playground
from zerver.actions.realm_settings import (
do_change_realm_org_type,
do_change_realm_plan_type,
@@ -2223,7 +2223,7 @@ class NormalActionsTest(BaseAction):
def test_realm_playground_events(self) -> None:
events = self.verify_action(
- lambda: do_add_realm_playground(
+ lambda: check_add_realm_playground(
self.user_profile.realm,
acting_user=None,
name="Python playground",
diff --git a/zerver/tests/test_realm_playgrounds.py b/zerver/tests/test_realm_playgrounds.py
index 0fc0bc7f45d..97ac4fe43e8 100644
--- a/zerver/tests/test_realm_playgrounds.py
+++ b/zerver/tests/test_realm_playgrounds.py
@@ -1,4 +1,4 @@
-from zerver.actions.realm_playgrounds import do_add_realm_playground
+from zerver.actions.realm_playgrounds import check_add_realm_playground
from zerver.lib.test_classes import ZulipTestCase
from zerver.models import RealmPlayground, get_realm
@@ -115,7 +115,7 @@ class RealmPlaygroundTests(ZulipTestCase):
iago = self.example_user("iago")
realm = get_realm("zulip")
- playground_id = do_add_realm_playground(
+ playground_id = check_add_realm_playground(
realm,
acting_user=iago,
name="Python playground",
diff --git a/zerver/views/realm_playgrounds.py b/zerver/views/realm_playgrounds.py
index 53d860e2670..849338fbbc6 100644
--- a/zerver/views/realm_playgrounds.py
+++ b/zerver/views/realm_playgrounds.py
@@ -1,12 +1,11 @@
import re
-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.actions.realm_playgrounds import check_add_realm_playground, do_remove_realm_playground
from zerver.decorator import require_realm_admin
-from zerver.lib.exceptions import JsonableError, ValidationFailureError
+from zerver.lib.exceptions import JsonableError
from zerver.lib.request import REQ, has_request_variables
from zerver.lib.response import json_success
from zerver.lib.validator import check_capped_string
@@ -43,16 +42,13 @@ def add_realm_playground(
url_template: str = REQ(),
pygments_language: str = REQ(str_validator=check_pygments_language),
) -> HttpResponse:
- try:
- playground_id = do_add_realm_playground(
- realm=user_profile.realm,
- acting_user=user_profile,
- name=name.strip(),
- pygments_language=pygments_language.strip(),
- url_template=url_template.strip(),
- )
- except ValidationError as e:
- raise ValidationFailureError(e)
+ playground_id = check_add_realm_playground(
+ realm=user_profile.realm,
+ acting_user=user_profile,
+ name=name.strip(),
+ pygments_language=pygments_language.strip(),
+ url_template=url_template.strip(),
+ )
return json_success(request, data={"id": playground_id})