wcs: add user fields from card schema (#66897)
gitea-wip/combo/pipeline/head There was a failure building this commit Details
gitea/combo/pipeline/head Something is wrong with the build of this commit Details

This commit is contained in:
Lauréline Guérin 2022-09-12 12:04:53 +02:00
parent 0cacfc0349
commit f47971df8a
No known key found for this signature in database
GPG Key ID: 1FAB9B9B4F93D473
5 changed files with 76 additions and 25 deletions

View File

@ -1378,8 +1378,9 @@ class WcsCardCell(CardMixin, CellBase):
# add fake user fields
extra_context['fields_by_varnames'].update(
{
'user:name': {'label': _('User name'), 'type': 'string'},
'user:email': {'label': _('User email'), 'type': 'email'},
'user:%s' % i['varname']: i
for i in (self.cached_json.get('user', {}).get('fields') or [])
if i.get('varname')
}
)
return getattr(self, 'get_cell_extra_context_%s_mode' % self.display_mode)(context, extra_context)
@ -1434,7 +1435,7 @@ class WcsCardCell(CardMixin, CellBase):
# add user fields
card_data['fields'] = card_data.get('fields', {})
card_data['fields'].update(
{('user:%s' % k): v for k, v in card_data.get('user', {}).items() if k in ['name', 'email']}
{('user:%s' % k): v for k, v in card_data.get('user', {}).items() if k not in ['NameId', 'id']}
)
def get_cell_extra_context_table_mode(self, context, extra_context):

View File

@ -55,10 +55,7 @@
<p>
<label>
{% trans "Card Fields" %}
<select name="field_varname">
<option value="user:name">{% trans "Full name (User field)" %}</option>
<option value="user:email">{% trans "Email (User field)" %}</option>
</select>
<select name="field_varname"></select>
</label>
</p>
<p>
@ -224,10 +221,7 @@
<p>
<label>
{% trans "Card Fields" %}
<select name="field_varname">
<option value="user:name">{% trans "Full name (User field)" %}</option>
<option value="user:email">{% trans "Email (User field)" %}</option>
</select>
<select name="field_varname"></select>
</label>
</p>
<p>

View File

@ -492,6 +492,12 @@ Card_cell_custom.prototype = {
return wrapper.firstElementChild;
},
field_with_varname: function(varname) {
if (varname.startsWith('user:')) {
const field = this.cardSchema.user.fields.filter(function(i) {
return i.varname === varname.substring(5);
})
return field[0];
}
const field = this.cardSchema.fields.filter(function(i) {
return i.varname === varname;
})
@ -598,11 +604,18 @@ Card_cell_custom.prototype = {
const varname_select = this.grid_cell_form.field_varname;
this.cardSchema.fields.forEach(function(el, id) {
if (el.varname) {
$(varname_select).find('option:nth-last-child(2)').before(
$('<option />')
.attr('value', el.varname)
.text(el.label)
);
$('<option />')
.attr('value', el.varname)
.text(el.label)
.appendTo(varname_select);
}
});
this.cardSchema.user.fields.forEach(function(el, id) {
if (el.varname) {
$('<option />')
.attr('value', 'user:' + el.varname)
.text(el.label + ' (' + gettext('User field') + ')')
.appendTo(varname_select);
}
});
},
@ -660,7 +673,7 @@ Card_cell_custom.prototype = {
// set cell text
let schema_field = _self.field_with_varname(schema_cell.varname);
let cell_text = "";
if (schema_field || schema_cell.varname == '@custom@' || schema_cell.varname == '@link@' || schema_cell.varname.startsWith('user:')) {
if (schema_field || schema_cell.varname == '@custom@' || schema_cell.varname == '@link@') {
let cell_content = "";
if (schema_cell.varname == '@custom@') {
cell_content = (schema_cell.template || '') + ' (' + gettext('Custom') + ')';
@ -672,10 +685,11 @@ Card_cell_custom.prototype = {
cell_content += (schema_cell.url_template || '');
}
cell_content += ' (' + gettext('Link') + ')';
} else if (schema_cell.varname.startsWith('user:')) {
cell_content += $(this.grid_cell_form).find('select[name="field_varname"] option[value="' + schema_cell.varname + '"]').text();
} else {
cell_content = schema_field.label;
if (schema_cell.varname.startsWith('user:')) {
cell_content += ' (' + gettext('User field') + ')';
}
}
cell_text += $('<span/>').addClass(schema_cell.display_mode).text(cell_content).html();
cell_text += '<span class="cell-meta">';

View File

@ -809,16 +809,20 @@ def test_card_cell_table_mode_render_custom_schema_card_field(mock_send, context
{'varname': 'fieldf'},
{'varname': 'fieldg'},
{'varname': 'fieldh'},
{'varname': 'unknown'},
{'varname': 'user:name'},
{'varname': 'user:email'},
{'varname': 'user:first_name'},
{'varname': 'user:last_name'},
{'varname': 'user:unknown'},
{}, # missing varname
]
cell.save()
result = cell.render(context)
assert PyQuery(result).find('ul li') == []
assert len(PyQuery(result).find('table tr td')) == 11 * 3
assert [PyQuery(td).text() for td in PyQuery(result).find('table tr td')[:11]] == [
assert len(PyQuery(result).find('table tr td')) == 13 * 3
assert [PyQuery(td).text() for td in PyQuery(result).find('table tr:first-child td')] == [
'<i>a</i>',
'yes',
'Sept. 28, 2020',
@ -830,6 +834,8 @@ def test_card_cell_table_mode_render_custom_schema_card_field(mock_send, context
'https://www.example.net/',
'User Foo Bar',
'foo@bar.com',
'User',
'Foo Bar',
]
assert PyQuery(result).find('table tr:first-child td:nth-child(8) a').text().strip() == 'test@localhost'
assert (
@ -1047,6 +1053,9 @@ def test_card_cell_table_mode_render_with_headers(mock_send, context, with_heade
{'varname': 'fieldb'},
{'varname': 'user:name'},
{'varname': 'user:email'},
{'varname': 'user:first_name'},
{'varname': 'user:last_name'},
{'varname': 'user:unknown'},
],
},
display_mode='table',
@ -1060,10 +1069,13 @@ def test_card_cell_table_mode_render_with_headers(mock_send, context, with_heade
def test(value):
result = cell.render(context)
if with_headers:
assert len(PyQuery(result).find('table thead th')) == 6
assert PyQuery(result).find('table thead th:nth-child(1)').text() == value
assert PyQuery(result).find('table thead th:nth-child(2)').text() == 'Field B'
assert PyQuery(result).find('table thead th:nth-child(3)').text() == 'User name'
assert PyQuery(result).find('table thead th:nth-child(4)').text() == 'User email'
assert PyQuery(result).find('table thead th:nth-child(3)').text() == 'Name'
assert PyQuery(result).find('table thead th:nth-child(4)').text() == 'Email'
assert PyQuery(result).find('table thead th:nth-child(5)').text() == 'First name'
assert PyQuery(result).find('table thead th:nth-child(6)').text() == 'Last name'
else:
assert PyQuery(result).find('table thead') == []
@ -1693,17 +1705,37 @@ def test_card_cell_card_mode_render_custom_schema_card_field(mock_send, context)
'field_content': 'label-and-value',
'display_mode': 'text',
},
{
'varname': 'user:first_name',
'field_content': 'label-and-value',
'display_mode': 'text',
},
{
'varname': 'user:last_name',
'field_content': 'label-and-value',
'display_mode': 'text',
},
{
'varname': 'user:unknown',
'field_content': 'label-and-value',
'display_mode': 'text',
},
]
cell.save()
result = cell.render(context)
assert PyQuery(result).find('.cell--body > div > div:nth-child(1) .label').text() == 'User name'
assert len(PyQuery(result).find('.cell--body > div > div')) == 4
assert PyQuery(result).find('.cell--body > div > div:nth-child(1) .label').text() == 'Name'
assert PyQuery(result).find('.cell--body > div > div:nth-child(1) .value').text() == 'User Foo Bar'
assert PyQuery(result).find('.cell--body > div > div:nth-child(2) .label').text() == 'User email'
assert PyQuery(result).find('.cell--body > div > div:nth-child(2) .label').text() == 'Email'
assert PyQuery(result).find('.cell--body > div > div:nth-child(2) .value a').text() == 'foo@bar.com'
assert (
PyQuery(result).find('.cell--body > div > div:nth-child(2) .value a').attr['href']
== 'mailto:foo@bar.com'
)
assert PyQuery(result).find('.cell--body > div > div:nth-child(3) .label').text() == 'First name'
assert PyQuery(result).find('.cell--body > div > div:nth-child(3) .value').text() == 'User'
assert PyQuery(result).find('.cell--body > div > div:nth-child(4) .label').text() == 'Last name'
assert PyQuery(result).find('.cell--body > div > div:nth-child(4) .value').text() == 'Foo Bar'
@mock.patch('requests.Session.send', side_effect=mocked_requests_send)

View File

@ -113,6 +113,8 @@ WCS_CARDS_DATA = {
'user': {
'name': 'User Foo Bar',
'email': 'foo@bar.com',
'first_name': 'User',
'last_name': 'Foo Bar',
},
},
{
@ -236,6 +238,14 @@ WCS_CARDDEF_SCHEMAS = {
{'label': 'Subtitle', 'type': 'subtitle'},
{'label': 'Empty', 'varname': None, 'type': 'string'},
],
'user': {
'fields': [
{'label': 'Name', 'varname': 'name', 'type': 'string'},
{'label': 'Email', 'varname': 'email', 'type': 'email'},
{'label': 'First name', 'varname': 'first_name', 'type': 'string'},
{'label': 'Last name', 'varname': 'last_name', 'type': 'string'},
],
},
},
'card_a': {
'name': 'Card A',