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.

code_playground: Add custom playground options in the default_options list.

Maneesh Shukladc95699862ec
diff --git a/web/src/settings_org.ts b/web/src/settings_org.ts
index 5d116ee1b92..92b75b0169f 100644
--- a/web/src/settings_org.ts
+++ b/web/src/settings_org.ts
@@ -1139,30 +1139,37 @@ export let init_dropdown_widgets = (): void => {
"channel",
);
- const default_code_language_options = (): dropdown_widget.Option[] => {
- const options = Object.keys(pygments_data.langs).map((x) => ({
- name: x,
- unique_id: x,
- }));
-
- const disabled_option = {
- is_setting_disabled: true,
- unique_id: "",
- name: $t({defaultMessage: "No language set"}),
- };
-
- options.unshift(disabled_option);
- return options;
- };
set_up_dropdown_widget(
"realm_default_code_block_language",
- default_code_language_options,
+ combined_code_language_options,
"language",
);
set_up_dropdown_widget_for_realm_group_settings();
};
+export const combined_code_language_options = (): dropdown_widget.Option[] => {
+ // Default language options from pygments_data
+ const default_options = Object.keys(pygments_data.langs).map((x) => ({
+ name: x,
+ unique_id: x,
+ }));
+
+ // Custom playground language options from realm_playgrounds.
+ const playground_options = (realm.realm_playgrounds ?? []).map((playground) => ({
+ name: playground.pygments_language,
+ unique_id: playground.pygments_language,
+ }));
+
+ const disabled_option = {
+ is_setting_disabled: true,
+ unique_id: "",
+ name: $t({defaultMessage: "No language set"}),
+ };
+
+ return [disabled_option, ...playground_options, ...default_options];
+};
+
export function rewire_init_dropdown_widgets(value: typeof init_dropdown_widgets): void {
init_dropdown_widgets = value;
}
diff --git a/web/tests/settings_org.test.cjs b/web/tests/settings_org.test.cjs
index 58c6a95d971..cc173e46412 100644
--- a/web/tests/settings_org.test.cjs
+++ b/web/tests/settings_org.test.cjs
@@ -23,6 +23,7 @@ const settings_account = zrequire("settings_account");
const settings_components = zrequire("settings_components");
const settings_org = zrequire("settings_org");
const {set_current_user, set_realm} = zrequire("state_data");
+const pygments_data = zrequire("pygments_data");
const {initialize_user_settings} = zrequire("user_settings");
const current_user = {};
@@ -688,6 +689,36 @@ test("test get_sorted_options_list", () => {
);
});
+test("test combined_code_language_options", ({override}) => {
+ const default_options = Object.keys(pygments_data.langs).map((x) => ({
+ name: x,
+ unique_id: x,
+ }));
+
+ const expected_options_without_realm_playgrounds = [
+ {is_setting_disabled: true, unique_id: "", name: $t({defaultMessage: "No language set"})},
+ ...default_options,
+ ];
+
+ const options_without_realm_playgrounds = settings_org.combined_code_language_options();
+ assert.deepEqual(options_without_realm_playgrounds, expected_options_without_realm_playgrounds);
+
+ override(realm, "realm_playgrounds", [
+ {pygments_language: "custom_lang_1"},
+ {pygments_language: "custom_lang_2"},
+ ]);
+
+ const expected_options_with_realm_playgrounds = [
+ {is_setting_disabled: true, unique_id: "", name: $t({defaultMessage: "No language set"})},
+ {unique_id: "custom_lang_1", name: "custom_lang_1"},
+ {unique_id: "custom_lang_2", name: "custom_lang_2"},
+ ...default_options,
+ ];
+
+ const options_with_realm_playgrounds = settings_org.combined_code_language_options();
+ assert.deepEqual(options_with_realm_playgrounds, expected_options_with_realm_playgrounds);
+});
+
test("misc", ({override}) => {
override(current_user, "is_admin", false);
$("#user-avatar-upload-widget").length = 1;
diff --git a/zerver/tests/test_realm_playgrounds.py b/zerver/tests/test_realm_playgrounds.py
index 45e23b8de11..7a4f240a0f6 100644
--- a/zerver/tests/test_realm_playgrounds.py
+++ b/zerver/tests/test_realm_playgrounds.py
@@ -1,4 +1,5 @@
from zerver.actions.realm_playgrounds import check_add_realm_playground
+from zerver.actions.realm_settings import do_set_realm_property
from zerver.lib.test_classes import ZulipTestCase
from zerver.models import RealmPlayground
from zerver.models.realms import get_realm
@@ -131,3 +132,25 @@ class RealmPlaygroundTests(ZulipTestCase):
result = self.api_delete(iago, f"/api/v1/realm/playgrounds/{playground_id}")
self.assert_json_success(result)
self.assertFalse(RealmPlayground.objects.filter(name="Python").exists())
+
+ def test_delete_default_code_block_language_playground(self) -> None:
+ iago = self.example_user("iago")
+ realm = get_realm("zulip")
+
+ playground_id = check_add_realm_playground(
+ realm,
+ acting_user=iago,
+ name="Python playground",
+ pygments_language="Python",
+ url_template="https://python.example.com{code}",
+ )
+ self.assertTrue(RealmPlayground.objects.filter(name="Python playground").exists())
+
+ # Set the default code block language to the playground's language
+ do_set_realm_property(realm, "default_code_block_language", "Python", acting_user=iago)
+ self.assertEqual(realm.default_code_block_language, "Python")
+ result = self.api_delete(iago, f"/api/v1/realm/playgrounds/{playground_id}")
+ self.assert_json_success(result)
+ realm.refresh_from_db()
+ self.assertFalse(RealmPlayground.objects.filter(name="Python").exists())
+ self.assertEqual(realm.default_code_block_language, "")
diff --git a/zerver/views/realm_playgrounds.py b/zerver/views/realm_playgrounds.py
index 0ebecbabd3a..a9ae3bafb1e 100644
--- a/zerver/views/realm_playgrounds.py
+++ b/zerver/views/realm_playgrounds.py
@@ -6,6 +6,7 @@ from django.utils.translation import gettext as _
from pydantic import AfterValidator
from zerver.actions.realm_playgrounds import check_add_realm_playground, do_remove_realm_playground
+from zerver.actions.realm_settings import do_set_realm_property
from zerver.decorator import require_realm_admin
from zerver.lib.exceptions import JsonableError
from zerver.lib.response import json_success
@@ -63,5 +64,9 @@ def delete_realm_playground(
request: HttpRequest, user_profile: UserProfile, *, playground_id: PathOnly[int]
) -> HttpResponse:
realm_playground = access_playground_by_id(user_profile.realm, playground_id)
+ if user_profile.realm.default_code_block_language == realm_playground.pygments_language:
+ do_set_realm_property(
+ user_profile.realm, "default_code_block_language", "", acting_user=user_profile
+ )
do_remove_realm_playground(user_profile.realm, realm_playground, acting_user=user_profile)
return json_success(request)