realm: Migrate smaller files to typed_endpoint.

Migrate `realm_domains.py`, `realm_emoji.py`,
`realm_linkifiers.py`, `realm_logo.py`, `realm_playgrounds.py`
to typed_endpoint.
This commit is contained in:
Kenneth Rodrigues 2024-07-12 21:20:11 +05:30 committed by Tim Abbott
parent ba79d759f1
commit 246df3c884
5 changed files with 49 additions and 39 deletions

View File

@ -1,6 +1,7 @@
from django.core.exceptions import ValidationError from django.core.exceptions import ValidationError
from django.http import HttpRequest, HttpResponse from django.http import HttpRequest, HttpResponse
from django.utils.translation import gettext as _ from django.utils.translation import gettext as _
from pydantic import Json
from zerver.actions.realm_domains import ( from zerver.actions.realm_domains import (
do_add_realm_domain, do_add_realm_domain,
@ -10,9 +11,8 @@ from zerver.actions.realm_domains import (
from zerver.decorator import require_realm_owner from zerver.decorator import require_realm_owner
from zerver.lib.domains import validate_domain from zerver.lib.domains import validate_domain
from zerver.lib.exceptions import JsonableError 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.response import json_success
from zerver.lib.validator import check_bool from zerver.lib.typed_endpoint import PathOnly, typed_endpoint
from zerver.models import RealmDomain, UserProfile from zerver.models import RealmDomain, UserProfile
from zerver.models.realms import get_realm_domains from zerver.models.realms import get_realm_domains
@ -23,12 +23,13 @@ def list_realm_domains(request: HttpRequest, user_profile: UserProfile) -> HttpR
@require_realm_owner @require_realm_owner
@has_request_variables @typed_endpoint
def create_realm_domain( def create_realm_domain(
request: HttpRequest, request: HttpRequest,
user_profile: UserProfile, user_profile: UserProfile,
domain: str = REQ(), *,
allow_subdomains: bool = REQ(json_validator=check_bool), domain: str,
allow_subdomains: Json[bool],
) -> HttpResponse: ) -> HttpResponse:
domain = domain.strip().lower() domain = domain.strip().lower()
try: try:
@ -46,12 +47,13 @@ def create_realm_domain(
@require_realm_owner @require_realm_owner
@has_request_variables @typed_endpoint
def patch_realm_domain( def patch_realm_domain(
request: HttpRequest, request: HttpRequest,
user_profile: UserProfile, user_profile: UserProfile,
domain: str, *,
allow_subdomains: bool = REQ(json_validator=check_bool), domain: PathOnly[str],
allow_subdomains: Json[bool],
) -> HttpResponse: ) -> HttpResponse:
try: try:
realm_domain = RealmDomain.objects.get(realm=user_profile.realm, domain=domain) realm_domain = RealmDomain.objects.get(realm=user_profile.realm, domain=domain)
@ -62,9 +64,9 @@ def patch_realm_domain(
@require_realm_owner @require_realm_owner
@has_request_variables @typed_endpoint
def delete_realm_domain( def delete_realm_domain(
request: HttpRequest, user_profile: UserProfile, domain: str request: HttpRequest, user_profile: UserProfile, *, domain: PathOnly[str]
) -> HttpResponse: ) -> HttpResponse:
try: try:
realm_domain = RealmDomain.objects.get(realm=user_profile.realm, domain=domain) realm_domain = RealmDomain.objects.get(realm=user_profile.realm, domain=domain)

View File

@ -7,8 +7,8 @@ from zerver.actions.realm_emoji import check_add_realm_emoji, do_remove_realm_em
from zerver.decorator import require_member_or_admin from zerver.decorator import require_member_or_admin
from zerver.lib.emoji import check_remove_custom_emoji, check_valid_emoji_name, name_to_codepoint from zerver.lib.emoji import check_remove_custom_emoji, check_valid_emoji_name, name_to_codepoint
from zerver.lib.exceptions import JsonableError, ResourceNotFoundError from zerver.lib.exceptions import JsonableError, ResourceNotFoundError
from zerver.lib.request import REQ, has_request_variables
from zerver.lib.response import json_success from zerver.lib.response import json_success
from zerver.lib.typed_endpoint import PathOnly, typed_endpoint
from zerver.lib.upload import get_file_info from zerver.lib.upload import get_file_info
from zerver.models import RealmEmoji, UserProfile from zerver.models import RealmEmoji, UserProfile
from zerver.models.realm_emoji import get_all_custom_emoji_for_realm from zerver.models.realm_emoji import get_all_custom_emoji_for_realm
@ -23,9 +23,9 @@ def list_emoji(request: HttpRequest, user_profile: UserProfile) -> HttpResponse:
@require_member_or_admin @require_member_or_admin
@has_request_variables @typed_endpoint
def upload_emoji( def upload_emoji(
request: HttpRequest, user_profile: UserProfile, emoji_name: str = REQ(path_only=True) request: HttpRequest, user_profile: UserProfile, *, emoji_name: PathOnly[str]
) -> HttpResponse: ) -> HttpResponse:
emoji_name = emoji_name.strip().replace(" ", "_") emoji_name = emoji_name.strip().replace(" ", "_")
valid_built_in_emoji = name_to_codepoint.keys() valid_built_in_emoji = name_to_codepoint.keys()

View File

@ -1,6 +1,7 @@
from django.core.exceptions import ValidationError from django.core.exceptions import ValidationError
from django.http import HttpRequest, HttpResponse from django.http import HttpRequest, HttpResponse
from django.utils.translation import gettext as _ from django.utils.translation import gettext as _
from pydantic import Json
from zerver.actions.realm_linkifiers import ( from zerver.actions.realm_linkifiers import (
check_reorder_linkifiers, check_reorder_linkifiers,
@ -10,9 +11,8 @@ from zerver.actions.realm_linkifiers import (
) )
from zerver.decorator import require_realm_admin from zerver.decorator import require_realm_admin
from zerver.lib.exceptions import JsonableError, ValidationFailureError from zerver.lib.exceptions import JsonableError, ValidationFailureError
from zerver.lib.request import REQ, has_request_variables
from zerver.lib.response import json_success from zerver.lib.response import json_success
from zerver.lib.validator import check_int, check_list from zerver.lib.typed_endpoint import PathOnly, typed_endpoint
from zerver.models import RealmFilter, UserProfile from zerver.models import RealmFilter, UserProfile
from zerver.models.linkifiers import linkifiers_for_realm from zerver.models.linkifiers import linkifiers_for_realm
@ -24,12 +24,13 @@ def list_linkifiers(request: HttpRequest, user_profile: UserProfile) -> HttpResp
@require_realm_admin @require_realm_admin
@has_request_variables @typed_endpoint
def create_linkifier( def create_linkifier(
request: HttpRequest, request: HttpRequest,
user_profile: UserProfile, user_profile: UserProfile,
pattern: str = REQ(), *,
url_template: str = REQ(), pattern: str,
url_template: str,
) -> HttpResponse: ) -> HttpResponse:
try: try:
linkifier_id = do_add_linkifier( linkifier_id = do_add_linkifier(
@ -55,13 +56,14 @@ def delete_linkifier(
@require_realm_admin @require_realm_admin
@has_request_variables @typed_endpoint
def update_linkifier( def update_linkifier(
request: HttpRequest, request: HttpRequest,
user_profile: UserProfile, user_profile: UserProfile,
filter_id: int, *,
pattern: str = REQ(), filter_id: PathOnly[int],
url_template: str = REQ(), pattern: str,
url_template: str,
) -> HttpResponse: ) -> HttpResponse:
try: try:
do_update_linkifier( do_update_linkifier(
@ -79,11 +81,12 @@ def update_linkifier(
@require_realm_admin @require_realm_admin
@has_request_variables @typed_endpoint
def reorder_linkifiers( def reorder_linkifiers(
request: HttpRequest, request: HttpRequest,
user_profile: UserProfile, user_profile: UserProfile,
ordered_linkifier_ids: list[int] = REQ(json_validator=check_list(check_int)), *,
ordered_linkifier_ids: Json[list[int]],
) -> HttpResponse: ) -> HttpResponse:
check_reorder_linkifiers(user_profile.realm, ordered_linkifier_ids, acting_user=user_profile) check_reorder_linkifiers(user_profile.realm, ordered_linkifier_ids, acting_user=user_profile)
return json_success(request) return json_success(request)

View File

@ -3,23 +3,23 @@ from django.core.files.uploadedfile import UploadedFile
from django.http import HttpRequest, HttpResponse from django.http import HttpRequest, HttpResponse
from django.shortcuts import redirect from django.shortcuts import redirect
from django.utils.translation import gettext as _ from django.utils.translation import gettext as _
from pydantic import Json
from zerver.actions.realm_logo import do_change_logo_source from zerver.actions.realm_logo import do_change_logo_source
from zerver.decorator import require_realm_admin from zerver.decorator import require_realm_admin
from zerver.lib.exceptions import JsonableError from zerver.lib.exceptions import JsonableError
from zerver.lib.realm_logo import get_realm_logo_url from zerver.lib.realm_logo import get_realm_logo_url
from zerver.lib.request import REQ, has_request_variables
from zerver.lib.response import json_success from zerver.lib.response import json_success
from zerver.lib.typed_endpoint import typed_endpoint
from zerver.lib.upload import get_file_info, upload_logo_image from zerver.lib.upload import get_file_info, upload_logo_image
from zerver.lib.url_encoding import append_url_query_string from zerver.lib.url_encoding import append_url_query_string
from zerver.lib.validator import check_bool
from zerver.models import UserProfile from zerver.models import UserProfile
@require_realm_admin @require_realm_admin
@has_request_variables @typed_endpoint
def upload_logo( def upload_logo(
request: HttpRequest, user_profile: UserProfile, night: bool = REQ(json_validator=check_bool) request: HttpRequest, user_profile: UserProfile, *, night: Json[bool]
) -> HttpResponse: ) -> HttpResponse:
user_profile.realm.ensure_not_on_limited_plan() user_profile.realm.ensure_not_on_limited_plan()
@ -43,9 +43,9 @@ def upload_logo(
@require_realm_admin @require_realm_admin
@has_request_variables @typed_endpoint
def delete_logo_backend( def delete_logo_backend(
request: HttpRequest, user_profile: UserProfile, night: bool = REQ(json_validator=check_bool) request: HttpRequest, user_profile: UserProfile, *, night: Json[bool]
) -> HttpResponse: ) -> HttpResponse:
# We don't actually delete the logo because it might still # We don't actually delete the logo because it might still
# be needed if the URL was cached and it is rewritten # be needed if the URL was cached and it is rewritten
@ -56,9 +56,9 @@ def delete_logo_backend(
return json_success(request) return json_success(request)
@has_request_variables @typed_endpoint
def get_logo_backend( def get_logo_backend(
request: HttpRequest, user_profile: UserProfile, night: bool = REQ(json_validator=check_bool) request: HttpRequest, user_profile: UserProfile, *, night: Json[bool]
) -> HttpResponse: ) -> HttpResponse:
url = get_realm_logo_url(user_profile.realm, night) url = get_realm_logo_url(user_profile.realm, night)

View File

@ -1,13 +1,15 @@
import re import re
from typing import Annotated
from django.http import HttpRequest, HttpResponse from django.http import HttpRequest, HttpResponse
from django.utils.translation import gettext as _ 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_playgrounds import check_add_realm_playground, do_remove_realm_playground
from zerver.decorator import require_realm_admin from zerver.decorator import require_realm_admin
from zerver.lib.exceptions import JsonableError 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.response import json_success
from zerver.lib.typed_endpoint import PathOnly, typed_endpoint
from zerver.lib.validator import check_capped_string from zerver.lib.validator import check_capped_string
from zerver.models import Realm, RealmPlayground, UserProfile from zerver.models import Realm, RealmPlayground, UserProfile
@ -34,13 +36,16 @@ def access_playground_by_id(realm: Realm, playground_id: int) -> RealmPlayground
@require_realm_admin @require_realm_admin
@has_request_variables @typed_endpoint
def add_realm_playground( def add_realm_playground(
request: HttpRequest, request: HttpRequest,
user_profile: UserProfile, user_profile: UserProfile,
name: str = REQ(), *,
url_template: str = REQ(), name: str,
pygments_language: str = REQ(str_validator=check_pygments_language), url_template: str,
pygments_language: Annotated[
str, AfterValidator(lambda x: check_pygments_language("pygments_language", x))
],
) -> HttpResponse: ) -> HttpResponse:
playground_id = check_add_realm_playground( playground_id = check_add_realm_playground(
realm=user_profile.realm, realm=user_profile.realm,
@ -53,9 +58,9 @@ def add_realm_playground(
@require_realm_admin @require_realm_admin
@has_request_variables @typed_endpoint
def delete_realm_playground( def delete_realm_playground(
request: HttpRequest, user_profile: UserProfile, playground_id: int request: HttpRequest, user_profile: UserProfile, *, playground_id: PathOnly[int]
) -> HttpResponse: ) -> HttpResponse:
realm_playground = access_playground_by_id(user_profile.realm, playground_id) realm_playground = access_playground_by_id(user_profile.realm, playground_id)
do_remove_realm_playground(user_profile.realm, realm_playground, acting_user=user_profile) do_remove_realm_playground(user_profile.realm, realm_playground, acting_user=user_profile)