2020-10-27 02:14:56 +01:00
|
|
|
import re
|
2024-07-12 17:50:11 +02:00
|
|
|
from typing import Annotated
|
2020-10-27 02:14:56 +01:00
|
|
|
|
|
|
|
from django.http import HttpRequest, HttpResponse
|
2021-04-16 00:57:30 +02:00
|
|
|
from django.utils.translation import gettext as _
|
2024-07-12 17:50:11 +02:00
|
|
|
from pydantic import AfterValidator
|
2020-10-27 02:14:56 +01:00
|
|
|
|
2023-07-24 21:14:42 +02:00
|
|
|
from zerver.actions.realm_playgrounds import check_add_realm_playground, do_remove_realm_playground
|
2020-10-27 02:14:56 +01:00
|
|
|
from zerver.decorator import require_realm_admin
|
2023-07-24 21:14:42 +02:00
|
|
|
from zerver.lib.exceptions import JsonableError
|
2021-07-04 10:00:10 +02:00
|
|
|
from zerver.lib.response import json_success
|
2024-07-12 17:50:11 +02:00
|
|
|
from zerver.lib.typed_endpoint import PathOnly, typed_endpoint
|
2023-05-27 05:04:50 +02:00
|
|
|
from zerver.lib.validator import check_capped_string
|
2020-10-27 02:21:22 +01:00
|
|
|
from zerver.models import Realm, RealmPlayground, UserProfile
|
2020-10-27 02:14:56 +01:00
|
|
|
|
|
|
|
|
|
|
|
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
|
2021-04-25 23:11:21 +02:00
|
|
|
# even in our fenced_code Markdown processor.
|
2020-10-27 02:14:56 +01:00
|
|
|
valid_pygments_language = re.compile(r"^[ a-zA-Z0-9_+-./#]*$")
|
|
|
|
matched_results = valid_pygments_language.match(s)
|
|
|
|
if not matched_results:
|
|
|
|
raise JsonableError(_("Invalid characters in pygments language"))
|
|
|
|
return s
|
|
|
|
|
|
|
|
|
2020-10-27 02:21:22 +01:00
|
|
|
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
|
|
|
|
|
|
|
|
|
2020-10-27 02:14:56 +01:00
|
|
|
@require_realm_admin
|
2024-07-12 17:50:11 +02:00
|
|
|
@typed_endpoint
|
2020-10-27 02:14:56 +01:00
|
|
|
def add_realm_playground(
|
|
|
|
request: HttpRequest,
|
|
|
|
user_profile: UserProfile,
|
2024-07-12 17:50:11 +02:00
|
|
|
*,
|
|
|
|
name: str,
|
|
|
|
url_template: str,
|
|
|
|
pygments_language: Annotated[
|
|
|
|
str, AfterValidator(lambda x: check_pygments_language("pygments_language", x))
|
|
|
|
],
|
2020-10-27 02:14:56 +01:00
|
|
|
) -> HttpResponse:
|
2023-07-24 21:14:42 +02:00
|
|
|
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(),
|
|
|
|
)
|
2022-01-31 13:44:02 +01:00
|
|
|
return json_success(request, data={"id": playground_id})
|
2020-10-27 02:21:22 +01:00
|
|
|
|
|
|
|
|
|
|
|
@require_realm_admin
|
2024-07-12 17:50:11 +02:00
|
|
|
@typed_endpoint
|
2020-10-27 02:21:22 +01:00
|
|
|
def delete_realm_playground(
|
2024-07-12 17:50:11 +02:00
|
|
|
request: HttpRequest, user_profile: UserProfile, *, playground_id: PathOnly[int]
|
2020-10-27 02:21:22 +01:00
|
|
|
) -> HttpResponse:
|
|
|
|
realm_playground = access_playground_by_id(user_profile.realm, playground_id)
|
2022-03-11 15:40:42 +01:00
|
|
|
do_remove_realm_playground(user_profile.realm, realm_playground, acting_user=user_profile)
|
2022-01-31 13:44:02 +01:00
|
|
|
return json_success(request)
|