Files
upstream-zulip/zerver/views/realm_playgrounds.py
upstream-mirror 64898b2c79
Some checks failed
Code scanning / CodeQL (push) Failing after 15s
Zulip production suite / Ubuntu 22.04 production build (push) Failing after 26s
Zulip production suite / Debian 12 production install with custom db name and user (push) Has been skipped
Zulip production suite / Ubuntu 22.04 production install and PostgreSQL upgrade with pgroonga (push) Has been skipped
Zulip production suite / Ubuntu 24.04 production install (push) Has been skipped
Zulip production suite / Ubuntu 26.04 production install (push) Has been skipped
Zulip production suite / Debian 13 production install (push) Has been skipped
Zulip production suite / 7.0 Version Upgrade (push) Has been skipped
Zulip production suite / 8.0 Version Upgrade (push) Has been skipped
Zulip production suite / 6.0 Version Upgrade (push) Has been skipped
Zulip production suite / 10.0 Version Upgrade (push) Has been skipped
Zulip production suite / 9.0 Version Upgrade (push) Has been skipped
Zulip production suite / 12.0 Version Upgrade (push) Has been skipped
Zulip production suite / 11.0 Version Upgrade (push) Has been skipped
GitHub Actions Security Analysis with zizmor / zizmor (push) Failing after 3s
Zulip CI / Debian 12 (Python 3.11, backend + documentation) (push) Failing after 26s
Zulip CI / Ubuntu 22.04 (Python 3.10, backend + frontend) (push) Failing after 3s
Zulip CI / Ubuntu 24.04 (Python 3.12, backend) (push) Failing after 25s
Zulip CI / Ubuntu 26.04 (Python 3.14, backend) (push) Failing after 25s
Zulip CI / Debian 13 (Python 3.13, backend) (push) Failing after 28s
Zulip production suite / Required jobs (push) Has been cancelled
Zulip CI / Required jobs (push) Has been cancelled
API Documentation Update Check / check-feature-level-updated (push) Has been cancelled
API Documentation Update Check / notify-if-api-docs-changed (push) Has been cancelled
snapshot of https://github.com/zulip/zulip.git @ 48c69911c7dd4e03b20552eb3bd77ad8151c1357 (2026-09-15)
2026-09-15 15:05:30 +05:00

78 lines
3.1 KiB
Python

import re
from typing import Annotated
from django.http import HttpRequest, HttpResponse
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
from zerver.lib.typed_endpoint import PathOnly, typed_endpoint
from zerver.lib.validator import check_capped_string
from zerver.models import Realm, RealmPlayground, UserProfile
from zerver.models.realm_playgrounds import PLAYGROUND_LANGUAGE_REGEX
def check_pygments_language(var_name: str, val: object) -> str:
s = check_capped_string(RealmPlayground.MAX_PYGMENTS_LANGUAGE_LENGTH)(var_name, val)
# We don't want to restrict the language here to be only from the list of valid
# Pygments languages. Keeping it open would allow us to hook up a "playground"
# for custom "languages" that aren't known to Pygments. We use a similar strategy
# even in our fenced_code Markdown processor.
if not re.match(rf"^{PLAYGROUND_LANGUAGE_REGEX}$", s):
for char in s:
if not re.match(rf"^{PLAYGROUND_LANGUAGE_REGEX}$", char):
raise JsonableError(
_("Invalid character in language: {character}").format(character=char)
)
if s in RealmPlayground.RESTRICTED_KEYWORDS:
raise JsonableError(_("Language '{language}' is not allowed.").format(language=s))
return s
def access_playground_by_id(realm: Realm, playground_id: int) -> RealmPlayground:
try:
realm_playground = RealmPlayground.objects.get(id=playground_id, realm=realm)
except RealmPlayground.DoesNotExist:
raise JsonableError(_("Invalid playground"))
return realm_playground
@require_realm_admin
@typed_endpoint
def add_realm_playground(
request: HttpRequest,
user_profile: UserProfile,
*,
name: str,
pygments_language: Annotated[
str, AfterValidator(lambda x: check_pygments_language("pygments_language", x))
],
url_template: str,
) -> HttpResponse:
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})
@require_realm_admin
@typed_endpoint
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)