zerver/lib/events.py: Add FIELD_TYPE_CHOICES_DICT to page_params.

This commit add FIELD_TYPE_CHOICES_DICT to page_params and replace
FIELD_TYPE_CHOICES.

FIELD_TYPE_CHOICES_DICT includes all field types with keyword, id
and display name. Using this field-type-dict, we can access field
type information by it's keyword, and remove all static use of
field-type'a name or id in frontend.
This commit also modifies functions in js where this page_params
field-types is used.
This commit is contained in:
Yashashvi Dave
2018-08-15 15:05:18 +05:30
committed by Tim Abbott
parent 621a5cdc35
commit 6e65235a6d
6 changed files with 46 additions and 31 deletions

View File

@@ -7,13 +7,19 @@ set_global('loading', {});
set_global('Sortable', {create: () => {}}); set_global('Sortable', {create: () => {}});
const SHORT_TEXT = 1; const SHORT_TEXT_ID = 1;
const CHOICE = 3; const CHOICE_ID = 3;
page_params.custom_profile_field_types = [ page_params.custom_profile_field_types = {
[SHORT_TEXT, 'short_text'], SHORT_TEXT: {
[CHOICE, 'choice'], id: SHORT_TEXT_ID,
]; name: "Short Text",
},
CHOICE: {
id: CHOICE_ID,
name: "Choice",
},
};
function test_populate(opts) { function test_populate(opts) {
const fields_data = opts.fields_data; const fields_data = opts.fields_data;
@@ -48,14 +54,14 @@ function test_populate(opts) {
run_test('populate_profile_fields', () => { run_test('populate_profile_fields', () => {
const fields_data = [ const fields_data = [
{ {
type: SHORT_TEXT, type: SHORT_TEXT_ID,
id: 10, id: 10,
name: 'favorite color', name: 'favorite color',
hint: 'blue?', hint: 'blue?',
field_data: '', field_data: '',
}, },
{ {
type: CHOICE, type: CHOICE_ID,
id: 30, id: 30,
name: 'meal', name: 'meal',
hint: 'lunch', hint: 'lunch',
@@ -77,7 +83,7 @@ run_test('populate_profile_fields', () => {
id: 10, id: 10,
name: 'favorite color', name: 'favorite color',
hint: 'blue?', hint: 'blue?',
type: 'short_text', type: 'Short Text',
choices: [], choices: [],
is_choice_field: false, is_choice_field: false,
}, },
@@ -88,7 +94,7 @@ run_test('populate_profile_fields', () => {
id: 30, id: 30,
name: 'meal', name: 'meal',
hint: 'lunch', hint: 'lunch',
type: 'choice', type: 'Choice',
choices: [ choices: [
{order: 0, value: 0, text: 'lunch'}, {order: 0, value: 0, text: 'lunch'},
{order: 1, value: 1, text: 'dinner'}, {order: 1, value: 1, text: 'dinner'},

View File

@@ -178,16 +178,17 @@ function show_user_profile(element, user) {
var profile_data = {}; var profile_data = {};
var localFormat = moment.localeData().longDateFormat('L'); var localFormat = moment.localeData().longDateFormat('L');
var field_types = page_params.custom_profile_field_types;
page_params.custom_profile_fields.forEach(function (field) { page_params.custom_profile_fields.forEach(function (field) {
var field_value = people.get_custom_profile_data(user.user_id, field.id); var field_value = people.get_custom_profile_data(user.user_id, field.id);
var field_type = settings_profile_fields.field_type_id_to_string(field.type); var field_type = field.type;
if (field_value) { if (field_value) {
if (field_type === "Date") { if (field_type === field_types.DATE.id) {
profile_data[field.name] = moment(field_value).format(localFormat); profile_data[field.name] = moment(field_value).format(localFormat);
} else if (field_type === "User") { } else if (field_type === field_types.USER.id) {
profile_data[field.name] = people.safe_full_names(field_value); profile_data[field.name] = people.safe_full_names(field_value);
} else if (field_type === "Choice") { } else if (field_type === field_types.CHOICE.id) {
var field_choice_dict = JSON.parse(field.field_data); var field_choice_dict = JSON.parse(field.field_data);
profile_data[field.name] = field_choice_dict[field_value].text; profile_data[field.name] = field_choice_dict[field_value].text;
} else { } else {

View File

@@ -76,20 +76,21 @@ exports.add_custom_profile_fields_to_settings = function () {
} }
var all_custom_fields = page_params.custom_profile_fields; var all_custom_fields = page_params.custom_profile_fields;
var field_types = page_params.custom_profile_field_types;
all_custom_fields.forEach(function (field) { all_custom_fields.forEach(function (field) {
var field_type = settings_profile_fields.field_type_id_to_string(field.type); var field_type = field.type;
var type; var type;
var value = people.my_custom_profile_data(field.id); var value = people.my_custom_profile_data(field.id);
var is_long_text = field_type === "Long text"; var is_long_text = field_type === field_types.LONG_TEXT.id;
var is_choice_field = field_type === "Choice"; var is_choice_field = field_type === field_types.CHOICE.id;
var is_user_field = field_type === "User"; var is_user_field = field_type === field_types.USER.id;
var is_date_field = field_type === "Date"; var is_date_field = field_type === field_types.DATE.id;
var field_choices = []; var field_choices = [];
if (field_type === "Long text" || field_type === "Short text") { if (is_long_text || field_type === field_types.SHORT_TEXT.id) {
type = "text"; type = "text";
} else if (field_type === "Choice") { } else if (is_choice_field) {
type = "choice"; type = "choice";
var field_choice_dict = JSON.parse(field.field_data); var field_choice_dict = JSON.parse(field.field_data);
for (var choice in field_choice_dict) { for (var choice in field_choice_dict) {
@@ -101,11 +102,11 @@ exports.add_custom_profile_fields_to_settings = function () {
}; };
} }
} }
} else if (field_type === "Date") { } else if (is_date_field) {
type = "date"; type = "date";
} else if (field_type === "URL") { } else if (field_type === field_types.URL.id) {
type = "url"; type = "url";
} else if (field_type === "User") { } else if (is_user_field) {
if (value) { if (value) {
value = JSON.parse(value); value = JSON.parse(value);
} }

View File

@@ -9,10 +9,17 @@ var meta = {
var order = []; var order = [];
exports.field_type_id_to_string = function (type_id) { exports.field_type_id_to_string = function (type_id) {
var name = _.find(page_params.custom_profile_field_types, function (type) { var field_types = page_params.custom_profile_field_types;
return type[0] === type_id; var field_type_str;
})[1];
return name; _.every(field_types, function (field_type) {
if (field_type.id === type_id) {
field_type_str = field_type.name;
return false;
}
return true;
});
return field_type_str;
}; };
function delete_profile_field(e) { function delete_profile_field(e) {
@@ -156,7 +163,7 @@ function open_edit_form(e) {
profile_field.form.find('input[name=name]').val(field.name); profile_field.form.find('input[name=name]').val(field.name);
profile_field.form.find('input[name=hint]').val(field.hint); profile_field.form.find('input[name=hint]').val(field.hint);
if (exports.field_type_id_to_string(field.type) === "Choice") { if (parseInt(field.type, 10) === page_params.custom_profile_field_types.CHOICE.id) {
// Re-render field choices in edit form to load initial choice data // Re-render field choices in edit form to load initial choice data
var choice_list = profile_field.form.find('.edit_profile_field_choices_container'); var choice_list = profile_field.form.find('.edit_profile_field_choices_container');
choice_list.off(); choice_list.off();

View File

@@ -31,7 +31,7 @@
<label for="profile_field_type" class="control-label">{{t "Type" }}</label> <label for="profile_field_type" class="control-label">{{t "Type" }}</label>
<select id="profile_field_type" name="field_type"> <select id="profile_field_type" name="field_type">
{{#each custom_profile_field_types}} {{#each custom_profile_field_types}}
<option value='{{this.[0]}}'>{{this.[1]}}</option> <option value='{{this.id}}'>{{this.name}}</option>
{{/each}} {{/each}}
</select> </select>
</div> </div>

View File

@@ -127,7 +127,7 @@ def fetch_initial_state_data(user_profile: UserProfile,
if want('custom_profile_fields'): if want('custom_profile_fields'):
fields = custom_profile_fields_for_realm(realm.id) fields = custom_profile_fields_for_realm(realm.id)
state['custom_profile_fields'] = [f.as_dict() for f in fields] state['custom_profile_fields'] = [f.as_dict() for f in fields]
state['custom_profile_field_types'] = CustomProfileField.FIELD_TYPE_CHOICES state['custom_profile_field_types'] = CustomProfileField.FIELD_TYPE_CHOICES_DICT
if want('hotspots'): if want('hotspots'):
state['hotspots'] = get_next_hotspots(user_profile) state['hotspots'] = get_next_hotspots(user_profile)