From 246df3c88458f4208dae2054dd3791ee8d381aa6 Mon Sep 17 00:00:00 2001 From: Kenneth Rodrigues Date: Fri, 12 Jul 2024 21:20:11 +0530 Subject: [PATCH] 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. --- zerver/views/realm_domains.py | 22 ++++++++++++---------- zerver/views/realm_emoji.py | 6 +++--- zerver/views/realm_linkifiers.py | 25 ++++++++++++++----------- zerver/views/realm_logo.py | 16 ++++++++-------- zerver/views/realm_playgrounds.py | 19 ++++++++++++------- 5 files changed, 49 insertions(+), 39 deletions(-) diff --git a/zerver/views/realm_domains.py b/zerver/views/realm_domains.py index 65ac5a0fe8..b7b0c9750d 100644 --- a/zerver/views/realm_domains.py +++ b/zerver/views/realm_domains.py @@ -1,6 +1,7 @@ from django.core.exceptions import ValidationError from django.http import HttpRequest, HttpResponse from django.utils.translation import gettext as _ +from pydantic import Json from zerver.actions.realm_domains import ( do_add_realm_domain, @@ -10,9 +11,8 @@ from zerver.actions.realm_domains import ( from zerver.decorator import require_realm_owner from zerver.lib.domains import validate_domain 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_bool +from zerver.lib.typed_endpoint import PathOnly, typed_endpoint from zerver.models import RealmDomain, UserProfile 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 -@has_request_variables +@typed_endpoint def create_realm_domain( request: HttpRequest, user_profile: UserProfile, - domain: str = REQ(), - allow_subdomains: bool = REQ(json_validator=check_bool), + *, + domain: str, + allow_subdomains: Json[bool], ) -> HttpResponse: domain = domain.strip().lower() try: @@ -46,12 +47,13 @@ def create_realm_domain( @require_realm_owner -@has_request_variables +@typed_endpoint def patch_realm_domain( request: HttpRequest, user_profile: UserProfile, - domain: str, - allow_subdomains: bool = REQ(json_validator=check_bool), + *, + domain: PathOnly[str], + allow_subdomains: Json[bool], ) -> HttpResponse: try: realm_domain = RealmDomain.objects.get(realm=user_profile.realm, domain=domain) @@ -62,9 +64,9 @@ def patch_realm_domain( @require_realm_owner -@has_request_variables +@typed_endpoint def delete_realm_domain( - request: HttpRequest, user_profile: UserProfile, domain: str + request: HttpRequest, user_profile: UserProfile, *, domain: PathOnly[str] ) -> HttpResponse: try: realm_domain = RealmDomain.objects.get(realm=user_profile.realm, domain=domain) diff --git a/zerver/views/realm_emoji.py b/zerver/views/realm_emoji.py index be99e9c066..e089ceb58b 100644 --- a/zerver/views/realm_emoji.py +++ b/zerver/views/realm_emoji.py @@ -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.lib.emoji import check_remove_custom_emoji, check_valid_emoji_name, name_to_codepoint 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.typed_endpoint import PathOnly, typed_endpoint from zerver.lib.upload import get_file_info from zerver.models import RealmEmoji, UserProfile 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 -@has_request_variables +@typed_endpoint 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: emoji_name = emoji_name.strip().replace(" ", "_") valid_built_in_emoji = name_to_codepoint.keys() diff --git a/zerver/views/realm_linkifiers.py b/zerver/views/realm_linkifiers.py index ae9bd4b4a7..8e163c0e46 100644 --- a/zerver/views/realm_linkifiers.py +++ b/zerver/views/realm_linkifiers.py @@ -1,6 +1,7 @@ from django.core.exceptions import ValidationError from django.http import HttpRequest, HttpResponse from django.utils.translation import gettext as _ +from pydantic import Json from zerver.actions.realm_linkifiers import ( check_reorder_linkifiers, @@ -10,9 +11,8 @@ from zerver.actions.realm_linkifiers import ( ) from zerver.decorator import require_realm_admin 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.validator import check_int, check_list +from zerver.lib.typed_endpoint import PathOnly, typed_endpoint from zerver.models import RealmFilter, UserProfile from zerver.models.linkifiers import linkifiers_for_realm @@ -24,12 +24,13 @@ def list_linkifiers(request: HttpRequest, user_profile: UserProfile) -> HttpResp @require_realm_admin -@has_request_variables +@typed_endpoint def create_linkifier( request: HttpRequest, user_profile: UserProfile, - pattern: str = REQ(), - url_template: str = REQ(), + *, + pattern: str, + url_template: str, ) -> HttpResponse: try: linkifier_id = do_add_linkifier( @@ -55,13 +56,14 @@ def delete_linkifier( @require_realm_admin -@has_request_variables +@typed_endpoint def update_linkifier( request: HttpRequest, user_profile: UserProfile, - filter_id: int, - pattern: str = REQ(), - url_template: str = REQ(), + *, + filter_id: PathOnly[int], + pattern: str, + url_template: str, ) -> HttpResponse: try: do_update_linkifier( @@ -79,11 +81,12 @@ def update_linkifier( @require_realm_admin -@has_request_variables +@typed_endpoint def reorder_linkifiers( request: HttpRequest, user_profile: UserProfile, - ordered_linkifier_ids: list[int] = REQ(json_validator=check_list(check_int)), + *, + ordered_linkifier_ids: Json[list[int]], ) -> HttpResponse: check_reorder_linkifiers(user_profile.realm, ordered_linkifier_ids, acting_user=user_profile) return json_success(request) diff --git a/zerver/views/realm_logo.py b/zerver/views/realm_logo.py index 99dd5aa94a..0a56e9b311 100644 --- a/zerver/views/realm_logo.py +++ b/zerver/views/realm_logo.py @@ -3,23 +3,23 @@ from django.core.files.uploadedfile import UploadedFile from django.http import HttpRequest, HttpResponse from django.shortcuts import redirect from django.utils.translation import gettext as _ +from pydantic import Json from zerver.actions.realm_logo import do_change_logo_source from zerver.decorator import require_realm_admin from zerver.lib.exceptions import JsonableError 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.typed_endpoint import typed_endpoint from zerver.lib.upload import get_file_info, upload_logo_image from zerver.lib.url_encoding import append_url_query_string -from zerver.lib.validator import check_bool from zerver.models import UserProfile @require_realm_admin -@has_request_variables +@typed_endpoint def upload_logo( - request: HttpRequest, user_profile: UserProfile, night: bool = REQ(json_validator=check_bool) + request: HttpRequest, user_profile: UserProfile, *, night: Json[bool] ) -> HttpResponse: user_profile.realm.ensure_not_on_limited_plan() @@ -43,9 +43,9 @@ def upload_logo( @require_realm_admin -@has_request_variables +@typed_endpoint 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: # We don't actually delete the logo because it might still # be needed if the URL was cached and it is rewritten @@ -56,9 +56,9 @@ def delete_logo_backend( return json_success(request) -@has_request_variables +@typed_endpoint 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: url = get_realm_logo_url(user_profile.realm, night) diff --git a/zerver/views/realm_playgrounds.py b/zerver/views/realm_playgrounds.py index 849338fbbc..0ebecbabd3 100644 --- a/zerver/views/realm_playgrounds.py +++ b/zerver/views/realm_playgrounds.py @@ -1,13 +1,15 @@ 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.decorator import require_realm_admin 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.typed_endpoint import PathOnly, typed_endpoint from zerver.lib.validator import check_capped_string 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 -@has_request_variables +@typed_endpoint def add_realm_playground( request: HttpRequest, user_profile: UserProfile, - name: str = REQ(), - url_template: str = REQ(), - pygments_language: str = REQ(str_validator=check_pygments_language), + *, + name: str, + url_template: str, + pygments_language: Annotated[ + str, AfterValidator(lambda x: check_pygments_language("pygments_language", x)) + ], ) -> HttpResponse: playground_id = check_add_realm_playground( realm=user_profile.realm, @@ -53,9 +58,9 @@ def add_realm_playground( @require_realm_admin -@has_request_variables +@typed_endpoint def delete_realm_playground( - request: HttpRequest, user_profile: UserProfile, playground_id: int + request: HttpRequest, user_profile: UserProfile, *, playground_id: PathOnly[int] ) -> HttpResponse: realm_playground = access_playground_by_id(user_profile.realm, playground_id) do_remove_realm_playground(user_profile.realm, realm_playground, acting_user=user_profile)