custom profile fields: Pass value as part of a dictionary.

While we're at it, we remove the JSON parsing that was part of the
user field code path, since this function isn't responsible for
rendering user fields.
This commit is contained in:
Hemanth V. Alluri 2018-12-31 11:11:06 +05:30 committed by Tim Abbott
parent bdaeccbca1
commit 28d344b4b5
8 changed files with 38 additions and 22 deletions

View File

@ -244,7 +244,7 @@ run_test('set_custom_profile_field_data', () => {
people.set_custom_profile_field_data(person.user_id, {});
assert.deepEqual(person.profile_data, {});
people.set_custom_profile_field_data(person.user_id, field);
assert.equal(person.profile_data[field.id], 'Field value');
assert.equal(person.profile_data[field.id].value, 'Field value');
});
run_test('get_rest_of_realm', () => {

View File

@ -591,7 +591,7 @@ run_test('compose_private_stream_alert', () => {
run_test('custom_user_profile_field', () => {
var field = {name: "GitHub user name", id: 2, hint: "Or link to profile"};
var args = {field: field, field_value: "@GitHub", field_type: "text"};
var args = {field: field, field_value: {value: "@GitHub"}, field_type: "text"};
var html = render('custom-user-profile-field', args);
assert.equal($(html).attr('data-field-id'), 2);
assert.equal($(html).find('.custom_user_field_value').val(), "@GitHub");

View File

@ -159,7 +159,7 @@ run_test('updates', () => {
me.profile_data = {};
user_events.update_person({user_id: me.user_id, custom_profile_field: {id: 3, value: 'Value'}});
person = people.get_by_email(me.email);
assert.equal(person.profile_data[3], 'Value');
assert.equal(person.profile_data[3].value, 'Value');
var updated = false;
settings_account.update_email = (email) => {

View File

@ -981,7 +981,9 @@ exports.set_custom_profile_field_data = function (user_id, field) {
blueslip.error("Unknown field id " + field.id);
return;
}
people_by_user_id_dict.get(user_id).profile_data[field.id] = field.value;
people_by_user_id_dict.get(user_id).profile_data[field.id] = {
value: field.value,
};
};
exports.is_current_user = function (email) {

View File

@ -236,21 +236,28 @@ exports.show_user_profile = function (user) {
profile_field.is_user_field = false;
profile_field.is_link = field_type === field_types.URL.id;
profile_field.type = field_type;
if (!field_value) {
return;
}
if (!field_value.value) {
return;
}
switch (field_type) {
case field_types.DATE.id:
profile_field.value = moment(field_value).format(localFormat);
profile_field.value = moment(field_value.value).format(localFormat);
break;
case field_types.USER.id:
profile_field.id = field.id;
profile_field.is_user_field = true;
profile_field.value = field_value;
profile_field.value = field_value.value;
break;
case field_types.CHOICE.id:
var field_choice_dict = JSON.parse(field.field_data);
profile_field.value = field_choice_dict[field_value].text;
profile_field.value = field_choice_dict[field_value.value].text;
break;
default:
profile_field.value = field_value;
profile_field.value = field_value.value;
break;
}
profile_data.push(profile_field);

View File

@ -70,7 +70,10 @@ exports.append_custom_profile_fields = function (element_id, user_id) {
all_custom_fields.forEach(function (field) {
var field_type = field.type;
var type;
var value = people.get_custom_profile_data(user_id, field.id);
var field_value = people.get_custom_profile_data(user_id, field.id);
if (field_value === undefined || field_value === null) {
field_value = {value: "", rendered_value: ""};
}
var is_long_text = field_type === field_types.LONG_TEXT.id;
var is_choice_field = field_type === field_types.CHOICE.id;
var is_user_field = field_type === field_types.USER.id;
@ -87,7 +90,7 @@ exports.append_custom_profile_fields = function (element_id, user_id) {
field_choices[field_choice_dict[choice].order] = {
value: choice,
text: field_choice_dict[choice].text,
selected: choice === value,
selected: choice === field_value.value,
};
}
}
@ -101,15 +104,10 @@ exports.append_custom_profile_fields = function (element_id, user_id) {
blueslip.error("Undefined field type.");
}
if (value === undefined || value === null) {
// If user has not set value for field.
value = "";
}
var html = templates.render("custom-user-profile-field", {
field: field,
field_type: type,
field_value: value,
field_value: field_value,
is_long_text_field: is_long_text,
is_choice_field: is_choice_field,
is_user_field: is_user_field,
@ -134,6 +132,10 @@ exports.intialize_custom_user_type_fields = function (element_id, user_id, is_ed
page_params.custom_profile_fields.forEach(function (field) {
var field_value_raw = people.get_custom_profile_data(user_id, field.id);
if (field_value_raw) {
field_value_raw = field_value_raw.value;
}
// If field is not editable and field value is null, we don't expect
// pill container for that field and proceed further
if (field.type === field_types.USER.id && (field_value_raw || is_editable)) {

View File

@ -1,7 +1,7 @@
<div class="user-name-section custom_user_field" name="{{ field.name }}" data-field-id="{{ field.id }}">
<label for="{{ field.name }}" class="title">{{ field.name }}</label>
{{#if is_long_text_field}}
<textarea maxlength="500" class="custom_user_field_value">{{ field_value }}</textarea>
<textarea maxlength="500" class="custom_user_field_value">{{ field_value.value }}</textarea>
{{else if is_choice_field}}
<select class="custom_user_field_value">
<option value=""></option>
@ -15,10 +15,10 @@
</div>
{{else if is_date_field }}
<input class="custom_user_field_value datepicker" data-field-id="{{ field.id }}" type="text"
value="{{ field_value }}" />
value="{{ field_value.value }}" />
<span class="remove_date"><i class="fa fa-close"></i></span>
{{else}}
<input class="custom_user_field_value" type="{{ field_type }}" value="{{ field_value }}" maxlength="50" />
<input class="custom_user_field_value" type="{{ field_type }}" value="{{ field_value.value }}" maxlength="50" />
{{/if}}
<div class="field_hint">{{ field.hint }}</div>
</div>

View File

@ -58,11 +58,14 @@ def get_raw_user_data(realm_id: int, client_gravatar: bool) -> Dict[int, Dict[st
user_dicts = get_realm_user_dicts(realm_id)
# TODO: Consider optimizing this query away with caching.
custom_profile_field_values = CustomProfileFieldValue.objects.filter(user_profile__realm_id=realm_id)
custom_profile_field_values = CustomProfileFieldValue.objects.select_related(
"field").filter(user_profile__realm_id=realm_id)
profiles_by_user_id = defaultdict(dict) # type: Dict[int, Dict[str, Any]]
for profile_field in custom_profile_field_values:
user_id = profile_field.user_profile_id
profiles_by_user_id[user_id][profile_field.field_id] = profile_field.value
profiles_by_user_id[user_id][profile_field.field_id] = {
"value": profile_field.value
}
def user_data(row: Dict[str, Any]) -> Dict[str, Any]:
avatar_url = get_avatar_field(
@ -404,7 +407,9 @@ def apply_event(state: Dict[str, Any],
if 'custom_profile_field' in person:
custom_field_id = person['custom_profile_field']['id']
custom_field_new_value = person['custom_profile_field']['value']
p['profile_data'][custom_field_id] = custom_field_new_value
p['profile_data'][custom_field_id] = {
'value': custom_field_new_value
}
elif event['type'] == 'realm_bot':
if event['op'] == 'add':