From c7cb983ebd41e78897b77d9b27fd84d430be8fa6 Mon Sep 17 00:00:00 2001 From: Sahil Batra Date: Thu, 9 Sep 2021 23:49:08 +0530 Subject: [PATCH] settings: Move check_settings_values to user_settings.py. This commit moves check_settings_values to user_settings.py from validator.py such that we can import the functions at the top without any issue of cyclic imports. --- zerver/lib/validator.py | 33 ----------------------------- zerver/views/realm.py | 2 +- zerver/views/user_settings.py | 40 +++++++++++++++++++++++++++++------ 3 files changed, 34 insertions(+), 41 deletions(-) diff --git a/zerver/lib/validator.py b/zerver/lib/validator.py index af55602e63..2a8a758221 100644 --- a/zerver/lib/validator.py +++ b/zerver/lib/validator.py @@ -582,36 +582,3 @@ def check_string_or_int(var_name: str, val: object) -> Union[str, int]: return val raise ValidationError(_("{var_name} is not a string or integer").format(var_name=var_name)) - - -def check_settings_values( - notification_sound: Optional[str], - email_notifications_batching_period_seconds: Optional[int], - default_language: Optional[str] = None, -) -> None: - from zerver.lib.actions import get_available_notification_sounds - from zerver.lib.i18n import get_available_language_codes - - # We can't use REQ for this widget because - # get_available_language_codes requires provisioning to be - # complete. - if default_language is not None and default_language not in get_available_language_codes(): - raise JsonableError(_("Invalid default_language")) - - if ( - notification_sound is not None - and notification_sound not in get_available_notification_sounds() - and notification_sound != "none" - ): - raise JsonableError(_("Invalid notification sound '{}'").format(notification_sound)) - - if email_notifications_batching_period_seconds is not None and ( - email_notifications_batching_period_seconds <= 0 - or email_notifications_batching_period_seconds > 7 * 24 * 60 * 60 - ): - # We set a limit of one week for the batching period - raise JsonableError( - _("Invalid email batching period: {} seconds").format( - email_notifications_batching_period_seconds - ) - ) diff --git a/zerver/views/realm.py b/zerver/views/realm.py index fdc66fc347..85103f13dc 100644 --- a/zerver/views/realm.py +++ b/zerver/views/realm.py @@ -31,12 +31,12 @@ from zerver.lib.validator import ( check_dict, check_int, check_int_in, - check_settings_values, check_string_in, check_string_or_int, to_non_negative_int, ) from zerver.models import Realm, RealmUserDefault, UserProfile +from zerver.views.user_settings import check_settings_values @require_realm_admin diff --git a/zerver/views/user_settings.py b/zerver/views/user_settings.py index fa0ffad780..099ef6a5c2 100644 --- a/zerver/views/user_settings.py +++ b/zerver/views/user_settings.py @@ -26,6 +26,7 @@ from zerver.lib.actions import ( do_change_user_setting, do_regenerate_api_key, do_start_email_change_process, + get_available_notification_sounds, ) from zerver.lib.avatar import avatar_url from zerver.lib.email_validation import ( @@ -34,17 +35,12 @@ from zerver.lib.email_validation import ( validate_email_not_already_in_realm, ) from zerver.lib.exceptions import JsonableError, RateLimited +from zerver.lib.i18n import get_available_language_codes from zerver.lib.request import REQ, has_request_variables from zerver.lib.response import json_success from zerver.lib.send_email import FromAddress, send_email from zerver.lib.upload import upload_avatar_image -from zerver.lib.validator import ( - check_bool, - check_int, - check_int_in, - check_settings_values, - check_string_in, -) +from zerver.lib.validator import check_bool, check_int, check_int_in, check_string_in from zerver.models import UserProfile, avatar_changes_disabled, name_changes_disabled from zproject.backends import check_password_strength, email_belongs_to_ldap @@ -93,6 +89,36 @@ emojiset_choices = {emojiset["key"] for emojiset in UserProfile.emojiset_choices default_view_options = ["recent_topics", "all_messages"] +def check_settings_values( + notification_sound: Optional[str], + email_notifications_batching_period_seconds: Optional[int], + default_language: Optional[str] = None, +) -> None: + # We can't use REQ for this widget because + # get_available_language_codes requires provisioning to be + # complete. + if default_language is not None and default_language not in get_available_language_codes(): + raise JsonableError(_("Invalid default_language")) + + if ( + notification_sound is not None + and notification_sound not in get_available_notification_sounds() + and notification_sound != "none" + ): + raise JsonableError(_("Invalid notification sound '{}'").format(notification_sound)) + + if email_notifications_batching_period_seconds is not None and ( + email_notifications_batching_period_seconds <= 0 + or email_notifications_batching_period_seconds > 7 * 24 * 60 * 60 + ): + # We set a limit of one week for the batching period + raise JsonableError( + _("Invalid email batching period: {} seconds").format( + email_notifications_batching_period_seconds + ) + ) + + @human_users_only @has_request_variables def json_change_settings(