mirror of
https://github.com/zulip/zulip.git
synced 2025-10-23 04:52:12 +00:00
zerver: Extract function to parse Enum from string value.
This commit extracts a function from `zerver/views/realm.py` used for `message_edit_history_visibility_policy` so it can be reused for other settings.
This commit is contained in:
committed by
Tim Abbott
parent
6b26b828b3
commit
934042d47e
@@ -1,6 +1,8 @@
|
||||
import re
|
||||
import zoneinfo
|
||||
from collections.abc import Collection
|
||||
from enum import Enum
|
||||
from typing import TypeVar
|
||||
|
||||
from django.core.exceptions import ValidationError
|
||||
from django.core.validators import URLValidator
|
||||
@@ -8,6 +10,7 @@ from django.utils.translation import gettext as _
|
||||
from pydantic import AfterValidator, BeforeValidator, NonNegativeInt
|
||||
from pydantic_core import PydanticCustomError
|
||||
|
||||
from zerver.lib.exceptions import JsonableError
|
||||
from zerver.lib.timezone import canonicalize_timezone
|
||||
|
||||
# The Pydantic.StringConstraints does not have validation for the string to be
|
||||
@@ -105,3 +108,17 @@ def check_color(var_name: str, val: object) -> str:
|
||||
if not matched_results:
|
||||
raise ValueError(_("{var_name} is not a valid hex color code").format(var_name=var_name))
|
||||
return s
|
||||
|
||||
|
||||
EnumT = TypeVar("EnumT", bound=Enum)
|
||||
|
||||
|
||||
def parse_enum_from_string_value(
|
||||
val: str,
|
||||
setting_name: str,
|
||||
enum: type[EnumT],
|
||||
) -> EnumT:
|
||||
try:
|
||||
return enum[val]
|
||||
except KeyError:
|
||||
raise JsonableError(_("Invalid {setting_name}").format(setting_name=setting_name))
|
||||
|
@@ -36,7 +36,11 @@ from zerver.lib.response import json_success
|
||||
from zerver.lib.retention import parse_message_retention_days
|
||||
from zerver.lib.streams import access_stream_by_id
|
||||
from zerver.lib.typed_endpoint import ApiParamConfig, typed_endpoint
|
||||
from zerver.lib.typed_endpoint_validators import check_int_in_validator, check_string_in_validator
|
||||
from zerver.lib.typed_endpoint_validators import (
|
||||
check_int_in_validator,
|
||||
check_string_in_validator,
|
||||
parse_enum_from_string_value,
|
||||
)
|
||||
from zerver.lib.user_groups import (
|
||||
GroupSettingChangeRequest,
|
||||
access_user_group_for_setting,
|
||||
@@ -80,17 +84,6 @@ def check_jitsi_url(value: str) -> str:
|
||||
raise JsonableError(_("{var_name} is not an allowed_type").format(var_name=var_name))
|
||||
|
||||
|
||||
def parse_message_edit_history_visibility_policy(
|
||||
policy_name: str,
|
||||
) -> MessageEditHistoryVisibilityPolicyEnum:
|
||||
try:
|
||||
return MessageEditHistoryVisibilityPolicyEnum[policy_name]
|
||||
except KeyError:
|
||||
raise JsonableError(
|
||||
_("Invalid {var_name}").format(var_name="message_edit_history_visibility_policy")
|
||||
)
|
||||
|
||||
|
||||
@require_realm_admin
|
||||
@typed_endpoint
|
||||
def update_realm(
|
||||
@@ -125,7 +118,14 @@ def update_realm(
|
||||
Json[int | str] | None, ApiParamConfig("message_content_edit_limit_seconds")
|
||||
] = None,
|
||||
message_edit_history_visibility_policy: Annotated[
|
||||
str | None, AfterValidator(lambda val: parse_message_edit_history_visibility_policy(val))
|
||||
str | None,
|
||||
AfterValidator(
|
||||
lambda val: parse_enum_from_string_value(
|
||||
val,
|
||||
"message_edit_history_visibility_policy",
|
||||
MessageEditHistoryVisibilityPolicyEnum,
|
||||
)
|
||||
),
|
||||
] = None,
|
||||
default_language: str | None = None,
|
||||
waiting_period_threshold: Json[NonNegativeInt] | None = None,
|
||||
|
Reference in New Issue
Block a user