Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

custom_profile_fields: Rename internal "select" identifiers.

Sairam Bisoyiad572bf698e9
diff --git a/web/src/custom_profile_fields_ui.ts b/web/src/custom_profile_fields_ui.ts
index 1ec130098bc..ed7f2bd6318 100644
--- a/web/src/custom_profile_fields_ui.ts
+++ b/web/src/custom_profile_fields_ui.ts
@@ -48,11 +48,11 @@ export function append_custom_profile_fields(element_id: string, user_id: number
rendered_value: "",
};
const editable_by_user = current_user.is_admin || field.editable_by_user;
- const is_select_field = field.type === all_field_types.DROPDOWN.id;
+ const is_dropdown_field = field.type === all_field_types.DROPDOWN.id;
const field_choices = [];
- if (is_select_field) {
- const field_choice_dict = settings_components.select_field_data_schema.parse(
+ if (is_dropdown_field) {
+ const field_choice_dict = settings_components.custom_profile_field_choices_schema.parse(
JSON.parse(field.field_data),
);
for (const [value, {order, text}] of Object.entries(field_choice_dict)) {
@@ -73,7 +73,7 @@ export function append_custom_profile_fields(element_id: string, user_id: number
is_date_field: field.type === all_field_types.DATE.id,
is_url_field: field.type === all_field_types.URL.id,
is_pronouns_field: field.type === all_field_types.PRONOUNS.id,
- is_select_field,
+ is_dropdown_field,
field_choices,
for_manage_user_modal: element_id === "#edit-user-form .custom-profile-field-form",
is_empty_required_field: field.required && !field_value.value,
diff --git a/web/src/settings_components.ts b/web/src/settings_components.ts
index bfff8138a85..c32143feb23 100644
--- a/web/src/settings_components.ts
+++ b/web/src/settings_components.ts
@@ -397,13 +397,13 @@ function get_message_retention_setting_value(
return util.check_time_input(custom_input_val);
}
-export const select_field_data_schema = z.record(
+export const custom_profile_field_choices_schema = z.record(
z.string(),
z.object({text: z.string(), order: z.string()}),
);
-export type SelectFieldData = z.output<typeof select_field_data_schema>;
+export type SelectFieldData = z.output<typeof custom_profile_field_choices_schema>;
-function read_select_field_data_from_form(
+function read_custom_profile_field_choices_from_form(
$profile_field_form: JQuery,
old_field_data: unknown,
): SelectFieldData {
@@ -413,7 +413,7 @@ function read_select_field_data_from_form(
const old_option_value_map = new Map<string, string>();
if (old_field_data !== undefined) {
for (const [value, choice] of Object.entries(
- select_field_data_schema.parse(old_field_data),
+ custom_profile_field_choices_schema.parse(old_field_data),
)) {
assert(typeof choice !== "string");
old_option_value_map.set(choice.text, value);
@@ -486,7 +486,7 @@ export function read_field_data_from_form(
// Only the following field types support associated field data.
if (field_type_id === field_types.DROPDOWN.id) {
- return read_select_field_data_from_form($profile_field_form, old_field_data);
+ return read_custom_profile_field_choices_from_form($profile_field_form, old_field_data);
} else if (field_type_id === field_types.EXTERNAL_ACCOUNT.id) {
const parsed_old_field_data = old_field_data
? external_account_field_schema.parse(old_field_data)
diff --git a/web/src/settings_profile_fields.ts b/web/src/settings_profile_fields.ts
index 1319d16eb97..db2ebc43525 100644
--- a/web/src/settings_profile_fields.ts
+++ b/web/src/settings_profile_fields.ts
@@ -379,7 +379,7 @@ function open_custom_profile_field_creation_form_modal(): void {
}
function initialize_custom_profile_field_form(): void {
- set_up_select_field();
+ set_up_custom_profile_field_choices();
setup_external_accounts_dropdown_widget();
$("#profile_field_type").on("change", () => {
@@ -562,7 +562,7 @@ function alphabetize_profile_field_choices($sortable_element: JQuery): void {
sortable_instance.sort(choices_array.map((v) => v[1]));
}
-function set_up_select_field_edit_form(
+function set_up_custom_profile_field_choices_edit_form(
$profile_field_form: JQuery,
field: CustomProfileField,
): void {
@@ -571,7 +571,7 @@ function set_up_select_field_edit_form(
$choice_list.off();
$choice_list.empty();
const choices_data = parse_field_choices_from_field_data(
- settings_components.select_field_data_schema.parse(JSON.parse(field.field_data)),
+ settings_components.custom_profile_field_choices_schema.parse(JSON.parse(field.field_data)),
);
for (const choice of choices_data) {
@@ -613,8 +613,9 @@ function open_custom_profile_field_edit_form_modal(this: HTMLElement): void {
}
let choices: FieldChoice[] = [];
if (field.type === field_types.DROPDOWN.id) {
- const select_field_data = settings_components.select_field_data_schema.parse(field_data);
- choices = parse_field_choices_from_field_data(select_field_data);
+ const custom_profile_field_choices =
+ settings_components.custom_profile_field_choices_schema.parse(field_data);
+ choices = parse_field_choices_from_field_data(custom_profile_field_choices);
}
const modal_content_html = render_edit_custom_profile_field_form({
@@ -627,7 +628,7 @@ function open_custom_profile_field_edit_form_modal(this: HTMLElement): void {
required: field.required,
editable_by_user: field.editable_by_user,
use_for_user_matching: field.use_for_user_matching,
- is_select_field: field.type === field_types.DROPDOWN.id,
+ is_dropdown_field: field.type === field_types.DROPDOWN.id,
is_external_account_field: field.type === field_types.EXTERNAL_ACCOUNT.id,
valid_to_display_in_summary: is_valid_to_display_in_summary(field.type),
valid_to_use_for_user_matching: is_valid_to_use_for_user_matching(field.type),
@@ -650,7 +651,7 @@ function open_custom_profile_field_edit_form_modal(this: HTMLElement): void {
}
if (field.type === field_types.DROPDOWN.id) {
- set_up_select_field_edit_form($profile_field_form, field);
+ set_up_custom_profile_field_choices_edit_form($profile_field_form, field);
}
if (field.type === field_types.EXTERNAL_ACCOUNT.id) {
@@ -732,25 +733,25 @@ function open_custom_profile_field_edit_form_modal(this: HTMLElement): void {
if (field.type === field_types.DROPDOWN.id && data["field_data"] !== undefined) {
const new_values = new Set(
Object.keys(
- settings_components.select_field_data_schema.parse(
+ settings_components.custom_profile_field_choices_schema.parse(
JSON.parse(data["field_data"].toString()),
),
),
);
const deleted_values: Record<string, string> = {};
- const select_field_data =
- settings_components.select_field_data_schema.parse(field_data);
- for (const [value, option] of Object.entries(select_field_data)) {
+ const custom_profile_field_choices =
+ settings_components.custom_profile_field_choices_schema.parse(field_data);
+ for (const [value, option] of Object.entries(custom_profile_field_choices)) {
if (!new_values.has(value)) {
deleted_values[value] = option.text;
}
}
if (Object.keys(deleted_values).length > 0) {
- const edit_select_field_modal_callback = (): void => {
+ const edit_custom_profile_field_modal_callback = (): void => {
show_modal_for_deleting_options(field, deleted_values, update_profile_field);
};
- dialog_widget.close(edit_select_field_modal_callback);
+ dialog_widget.close(edit_custom_profile_field_modal_callback);
return;
}
}
@@ -908,7 +909,7 @@ export function do_populate_profile_fields(profile_fields_data: CustomProfileFie
loading.destroy_indicator($("#admin_page_profile_fields_loading_indicator"));
}
-function set_up_select_field(): void {
+function set_up_custom_profile_field_choices(): void {
const $profile_field_choices = $("#profile_field_choices");
create_choice_row($profile_field_choices);
diff --git a/web/src/user_profile.ts b/web/src/user_profile.ts
index 396126b75d3..deb94327b64 100644
--- a/web/src/user_profile.ts
+++ b/web/src/user_profile.ts
@@ -496,7 +496,7 @@ export function get_custom_profile_field_data(
profile_field.value = field_value.value;
break;
case field_types.DROPDOWN.id: {
- const field_choice_dict = settings_components.select_field_data_schema.parse(
+ const field_choice_dict = settings_components.custom_profile_field_choices_schema.parse(
JSON.parse(field.field_data),
);
profile_field.value = field_choice_dict[field_value.value]!.text;
diff --git a/web/templates/settings/custom_user_profile_field.hbs b/web/templates/settings/custom_user_profile_field.hbs
index 2a96eb924c0..8bbee349d17 100644
--- a/web/templates/settings/custom_user_profile_field.hbs
+++ b/web/templates/settings/custom_user_profile_field.hbs
@@ -8,7 +8,7 @@
<div class="settings-profile-user-field {{#if is_empty_required_field}}empty-required-field{{/if}} {{#unless editable_by_user}}not-editable-by-user-input-wrapper{{/unless}}{{#if (and is_date_field editable_by_user)}}editable-date-field{{/if}}">
{{#if is_long_text_field}}
<textarea id="id_custom_profile_field_input_{{ field.id }}" maxlength="500" class="custom_user_field_value {{#if for_manage_user_modal}}modal-textarea{{else}}settings-textarea{{/if}}" name="{{ field.id }}" {{#unless editable_by_user}}disabled{{/unless}}>{{ field_value.value }}</textarea>
- {{else if is_select_field}}
+ {{else if is_dropdown_field}}
<select id="id_custom_profile_field_input_{{ field.id }}" class="custom_user_field_value {{#if for_manage_user_modal}}modal_select{{else}}settings_select{{/if}} bootstrap-focus-style" name="{{ field.id }}" {{#unless editable_by_user}}disabled{{/unless}}>
<option value=""></option>
{{#each field_choices}}
diff --git a/web/templates/settings/edit_custom_profile_field_form.hbs b/web/templates/settings/edit_custom_profile_field_form.hbs
index b9cb75340b6..c4978ea781c 100644
--- a/web/templates/settings/edit_custom_profile_field_form.hbs
+++ b/web/templates/settings/edit_custom_profile_field_form.hbs
@@ -8,7 +8,7 @@
<label for="id-custom-profile-field-hint" class="modal-field-label">{{t "Hint" }}</label>
<input type="text" name="hint" id="id-custom-profile-field-hint" class="modal_text_input prop-element" value="{{ hint }}" maxlength="80" data-setting-widget-type="string" />
</div>
- {{#if is_select_field }}
+ {{#if is_dropdown_field }}
<div class="input-group prop-element profile-field-choices-wrapper" id="id-custom-profile-field-field-data" data-setting-widget-type="field-data-setting">
<label for="profile_field_choices_edit" class="modal-field-label">{{t "Field choices" }}</label>
<div class="profile-field-choices" name="profile_field_choices_edit">
diff --git a/zerver/lib/users.py b/zerver/lib/users.py
index 1187827eede..260e7fcb021 100644
--- a/zerver/lib/users.py
+++ b/zerver/lib/users.py
@@ -528,7 +528,7 @@ def validate_user_custom_profile_field(
validator = validators[field_type]
return validator(var_name, value)
elif field_type == CustomProfileField.DROPDOWN:
- choice_field_validator = CustomProfileField.SELECT_FIELD_VALIDATORS[field_type]
+ choice_field_validator = CustomProfileField.DROPDOWN_FIELD_VALIDATORS[field_type]
field_data = field.field_data
# Put an assertion so that mypy doesn't complain.
assert field_data is not None
diff --git a/zerver/lib/validator.py b/zerver/lib/validator.py
index 71f67aa52b6..f4c060236a2 100644
--- a/zerver/lib/validator.py
+++ b/zerver/lib/validator.py
@@ -383,7 +383,9 @@ def check_external_account_url_pattern(var_name: str, val: object) -> str:
return s
-def validate_select_field_data(field_data: ProfileFieldData) -> dict[str, dict[str, str]]:
+def validate_custom_profile_field_choices(
+ field_data: ProfileFieldData,
+) -> dict[str, dict[str, str]]:
"""
This function is used to validate the data sent to the server while
creating/editing choices of the choice field in Organization settings.
@@ -414,7 +416,7 @@ def validate_select_field_data(field_data: ProfileFieldData) -> dict[str, dict[s
return cast(dict[str, dict[str, str]], field_data)
-def validate_select_field(var_name: str, field_data: str, value: object) -> str:
+def validate_dropdown_field(var_name: str, field_data: str, value: object) -> str:
"""
This function is used to validate the value selected by the user against a
choice field. This is not used to validate admin data.
diff --git a/zerver/models/custom_profile_fields.py b/zerver/models/custom_profile_fields.py
index ab07ee1a1e0..b887491f9a8 100644
--- a/zerver/models/custom_profile_fields.py
+++ b/zerver/models/custom_profile_fields.py
@@ -27,7 +27,7 @@ from zerver.lib.validator import (
check_long_string,
check_short_string,
check_url,
- validate_select_field,
+ validate_dropdown_field,
)
from zerver.models.realms import Realm
from zerver.models.users import UserProfile
@@ -101,15 +101,15 @@ class CustomProfileField(models.Model):
# These are the fields whose validators require more than var_name
# and value argument. i.e. DROPDOWN require field_data, USER require
# realm as argument.
- SELECT_FIELD_TYPE_DATA: list[ExtendedFieldElement] = [
- (DROPDOWN, gettext_lazy("Dropdown"), validate_select_field, str, "DROPDOWN"),
+ DROPDOWN_FIELD_TYPE_DATA: list[ExtendedFieldElement] = [
+ (DROPDOWN, gettext_lazy("Dropdown"), validate_dropdown_field, str, "DROPDOWN"),
]
USER_FIELD_TYPE_DATA: list[UserFieldElement] = [
(USER, gettext_lazy("Users"), check_valid_user_ids, orjson.loads, "USER"),
]
- SELECT_FIELD_VALIDATORS: dict[int, ExtendedValidator] = {
- item[0]: item[2] for item in SELECT_FIELD_TYPE_DATA
+ DROPDOWN_FIELD_VALIDATORS: dict[int, ExtendedValidator] = {
+ item[0]: item[2] for item in DROPDOWN_FIELD_TYPE_DATA
}
USER_FIELD_VALIDATORS: dict[int, RealmUserValidator] = {
item[0]: item[2] for item in USER_FIELD_TYPE_DATA
@@ -132,7 +132,7 @@ class CustomProfileField(models.Model):
]
ALL_FIELD_TYPES = sorted(
- [*FIELD_TYPE_DATA, *SELECT_FIELD_TYPE_DATA, *USER_FIELD_TYPE_DATA], key=lambda x: x[1]
+ [*FIELD_TYPE_DATA, *DROPDOWN_FIELD_TYPE_DATA, *USER_FIELD_TYPE_DATA], key=lambda x: x[1]
)
FIELD_VALIDATORS: dict[int, Validator[ProfileDataElementValue]] = {
diff --git a/zerver/tests/test_custom_profile_data.py b/zerver/tests/test_custom_profile_data.py
index 11c741cc22d..f400599b211 100644
--- a/zerver/tests/test_custom_profile_data.py
+++ b/zerver/tests/test_custom_profile_data.py
@@ -101,7 +101,7 @@ class CreateCustomProfileFieldTest(CustomProfileFieldTestCase):
result = self.client_post("/json/realm/profile_fields", info=data)
self.assert_json_error(result, "Field type not supported for use for user matching.")
- def test_create_select_field(self) -> None:
+ def test_create_dropdown_field(self) -> None:
self.login("iago")
data: dict[str, str | int] = {}
data["name"] = "Favorite programming language"
@@ -956,13 +956,13 @@ class UpdateCustomProfileFieldTest(CustomProfileFieldTestCase):
if field_dict["id"] == field.id:
self.assertEqual(field_dict["value"], "foobar")
- def test_update_invalid_select_field(self) -> None:
+ def test_update_invalid_dropdown_field(self) -> None:
field_name = "Favorite editor"
self.assert_error_update_invalid_value(
field_name, "foobar", f"'foobar' is not a valid choice for '{field_name}'."
)
- def test_update_select_field_successfully(self) -> None:
+ def test_update_dropdown_field_successfully(self) -> None:
self.login("iago")
realm = get_realm("zulip")
field = CustomProfileField.objects.get(name="Favorite editor", realm=realm)
@@ -1029,7 +1029,7 @@ class UpdateCustomProfileFieldTest(CustomProfileFieldTestCase):
self.set_user_custom_profile_data(iago, data)
mock_notify.assert_not_called()
- def test_removing_option_from_select_field(self) -> None:
+ def test_removing_option_from_dropdown_field(self) -> None:
self.login("iago")
realm = get_realm("zulip")
field = CustomProfileField.objects.get(name="Favorite editor", realm=realm)
diff --git a/zerver/views/custom_profile_fields.py b/zerver/views/custom_profile_fields.py
index 432527d3ab1..7d70ca2c241 100644
--- a/zerver/views/custom_profile_fields.py
+++ b/zerver/views/custom_profile_fields.py
@@ -23,7 +23,7 @@ from zerver.lib.response import json_success
from zerver.lib.typed_endpoint import PathOnly, typed_endpoint
from zerver.lib.types import ProfileDataElementUpdateDict, ProfileFieldData
from zerver.lib.users import validate_user_custom_profile_data
-from zerver.lib.validator import check_capped_string, validate_select_field_data
+from zerver.lib.validator import check_capped_string, validate_custom_profile_field_choices
from zerver.models import CustomProfileField, Realm, UserProfile
from zerver.models.custom_profile_fields import custom_profile_fields_for_realm
@@ -56,7 +56,7 @@ def validate_custom_field_data(field_type: int, field_data: ProfileFieldData) ->
# Choice type field must have at least have one choice
if len(field_data) < 1:
raise JsonableError(_("Field must have at least one choice."))
- validate_select_field_data(field_data)
+ validate_custom_profile_field_choices(field_data)
elif field_type == CustomProfileField.EXTERNAL_ACCOUNT:
validate_external_account_field_data(field_data)
except ValidationError as error: