groups: Migrate group creation and edit endpoints to typed_endpoint.

This commit is contained in:
Sahil Batra 2024-05-06 21:05:51 +05:30 committed by Tim Abbott
parent 43810f76ea
commit 0cc2244aac
1 changed files with 20 additions and 37 deletions

View File

@ -1,10 +1,11 @@
from typing import Dict, List, Optional, Sequence, Union from typing import List, Optional, Sequence, Union
from django.conf import settings from django.conf import settings
from django.db import transaction from django.db import transaction
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 django.utils.translation import override as override_language from django.utils.translation import override as override_language
from pydantic import Json
from zerver.actions.message_send import do_send_messages, internal_prep_private_message from zerver.actions.message_send import do_send_messages, internal_prep_private_message
from zerver.actions.user_groups import ( from zerver.actions.user_groups import (
@ -23,7 +24,7 @@ from zerver.lib.exceptions import JsonableError
from zerver.lib.mention import MentionBackend, silent_mention_syntax_for_user from zerver.lib.mention import MentionBackend, silent_mention_syntax_for_user
from zerver.lib.request import REQ, has_request_variables 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.types import Validator from zerver.lib.typed_endpoint import PathOnly, typed_endpoint
from zerver.lib.user_groups import ( from zerver.lib.user_groups import (
AnonymousSettingGroupDict, AnonymousSettingGroupDict,
access_user_group_by_id, access_user_group_by_id,
@ -39,51 +40,34 @@ from zerver.lib.user_groups import (
user_groups_in_realm_serialized, user_groups_in_realm_serialized,
) )
from zerver.lib.users import access_user_by_id, user_ids_to_users from zerver.lib.users import access_user_by_id, user_ids_to_users
from zerver.lib.validator import check_bool, check_dict_only, check_int, check_list, check_union from zerver.lib.validator import check_bool, check_int, check_list
from zerver.models import NamedUserGroup, UserGroup, UserProfile from zerver.models import NamedUserGroup, UserGroup, UserProfile
from zerver.models.users import get_system_bot from zerver.models.users import get_system_bot
from zerver.views.streams import compose_views from zerver.views.streams import compose_views
def parse_group_setting_value( def parse_group_setting_value(
setting_value: Union[int, Dict[str, List[int]]], setting_value: Union[int, AnonymousSettingGroupDict],
) -> Union[int, AnonymousSettingGroupDict]: ) -> Union[int, AnonymousSettingGroupDict]:
if isinstance(setting_value, int): if isinstance(setting_value, int):
return setting_value return setting_value
if len(setting_value["direct_members"]) == 0 and len(setting_value["direct_subgroups"]) == 1: if len(setting_value.direct_members) == 0 and len(setting_value.direct_subgroups) == 1:
return setting_value["direct_subgroups"][0] return setting_value.direct_subgroups[0]
return AnonymousSettingGroupDict( return setting_value
direct_members=setting_value["direct_members"],
direct_subgroups=setting_value["direct_subgroups"],
)
check_group_setting: Validator[Union[int, Dict[str, List[int]]]] = check_union(
[
check_int,
check_dict_only(
[
("direct_members", check_list(check_int)),
("direct_subgroups", check_list(check_int)),
]
),
]
)
@require_user_group_edit_permission @require_user_group_edit_permission
@has_request_variables @typed_endpoint
def add_user_group( def add_user_group(
request: HttpRequest, request: HttpRequest,
user_profile: UserProfile, user_profile: UserProfile,
name: str = REQ(), *,
members: Sequence[int] = REQ(json_validator=check_list(check_int), default=[]), name: str,
description: str = REQ(), members: Json[Sequence[int]],
can_mention_group: Optional[Union[Dict[str, List[int]], int]] = REQ( description: str,
json_validator=check_group_setting, default=None can_mention_group: Optional[Json[Union[AnonymousSettingGroupDict, int]]] = None,
),
) -> HttpResponse: ) -> HttpResponse:
user_profiles = user_ids_to_users(members, user_profile.realm) user_profiles = user_ids_to_users(members, user_profile.realm)
name = check_user_group_name(name) name = check_user_group_name(name)
@ -152,16 +136,15 @@ def check_setting_value_changed(
@transaction.atomic @transaction.atomic
@require_user_group_edit_permission @require_user_group_edit_permission
@has_request_variables @typed_endpoint
def edit_user_group( def edit_user_group(
request: HttpRequest, request: HttpRequest,
user_profile: UserProfile, user_profile: UserProfile,
user_group_id: int = REQ(json_validator=check_int, path_only=True), *,
name: Optional[str] = REQ(default=None), user_group_id: PathOnly[int],
description: Optional[str] = REQ(default=None), name: Optional[str] = None,
can_mention_group: Optional[Union[Dict[str, List[int]], int]] = REQ( description: Optional[str] = None,
json_validator=check_group_setting, default=None can_mention_group: Optional[Json[Union[int, AnonymousSettingGroupDict]]] = None,
),
) -> HttpResponse: ) -> HttpResponse:
if name is None and description is None and can_mention_group is None: if name is None and description is None and can_mention_group is None:
raise JsonableError(_("No new data supplied")) raise JsonableError(_("No new data supplied"))