wcs: add user fields in card custom_schema configuration (#66898)
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-09 14:39:34 +02:00
parent 612555a697
commit 0cacfc0349
No known key found for this signature in database
GPG Key ID: 1FAB9B9B4F93D473
5 changed files with 73 additions and 11 deletions

View File

@ -1375,6 +1375,13 @@ class WcsCardCell(CardMixin, CellBase):
extra_context['fields_by_varnames'] = {
i['varname']: i for i in (self.cached_json.get('fields') or []) if i.get('varname')
}
# add fake user fields
extra_context['fields_by_varnames'].update(
{
'user:name': {'label': _('User name'), 'type': 'string'},
'user:email': {'label': _('User email'), 'type': 'email'},
}
)
return getattr(self, 'get_cell_extra_context_%s_mode' % self.display_mode)(context, extra_context)
def complete_card_data(self, card_data, custom_context):
@ -1424,6 +1431,11 @@ class WcsCardCell(CardMixin, CellBase):
target_key='urls',
target_context=card_data,
)
# 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']}
)
def get_cell_extra_context_table_mode(self, context, extra_context):
if not context.get('synchronous'):

View File

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

View File

@ -598,10 +598,11 @@ Card_cell_custom.prototype = {
const varname_select = this.grid_cell_form.field_varname;
this.cardSchema.fields.forEach(function(el, id) {
if (el.varname) {
$('<option />')
.attr('value', el.varname)
.text(el.label)
.appendTo(varname_select);
$(varname_select).find('option:nth-last-child(2)').before(
$('<option />')
.attr('value', el.varname)
.text(el.label)
);
}
});
},
@ -659,7 +660,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@') {
if (schema_field || schema_cell.varname == '@custom@' || schema_cell.varname == '@link@' || schema_cell.varname.startsWith('user:')) {
let cell_content = "";
if (schema_cell.varname == '@custom@') {
cell_content = (schema_cell.template || '') + ' (' + gettext('Custom') + ')';
@ -671,6 +672,8 @@ 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;
}

View File

@ -809,14 +809,16 @@ def test_card_cell_table_mode_render_custom_schema_card_field(mock_send, context
{'varname': 'fieldf'},
{'varname': 'fieldg'},
{'varname': 'fieldh'},
{'varname': 'user:name'},
{'varname': 'user:email'},
{}, # missing varname
]
cell.save()
result = cell.render(context)
assert PyQuery(result).find('ul li') == []
assert len(PyQuery(result).find('table tr td')) == 9 * 3
assert [PyQuery(td).text() for td in PyQuery(result).find('table tr td')[:9]] == [
assert len(PyQuery(result).find('table tr td')) == 11 * 3
assert [PyQuery(td).text() for td in PyQuery(result).find('table tr td')[:11]] == [
'<i>a</i>',
'yes',
'Sept. 28, 2020',
@ -826,6 +828,8 @@ def test_card_cell_table_mode_render_custom_schema_card_field(mock_send, context
"lorem<strong>ipsum hello world",
'test@localhost',
'https://www.example.net/',
'User Foo Bar',
'foo@bar.com',
]
assert PyQuery(result).find('table tr:first-child td:nth-child(8) a').text().strip() == 'test@localhost'
assert (
@ -839,6 +843,10 @@ def test_card_cell_table_mode_render_custom_schema_card_field(mock_send, context
PyQuery(result).find('table tr:first-child td:nth-child(9) a').attr['href']
== 'https://www.example.net/'
)
assert PyQuery(result).find('table tr:first-child td:nth-child(11) a').text().strip() == 'foo@bar.com'
assert (
PyQuery(result).find('table tr:first-child td:nth-child(11) a').attr['href'] == 'mailto:foo@bar.com'
)
@mock.patch('requests.Session.send', side_effect=mocked_requests_send)
@ -1037,6 +1045,8 @@ def test_card_cell_table_mode_render_with_headers(mock_send, context, with_heade
'cells': [
{'varname': '@custom@', 'template': "<b>Foo</b> bar'baz {{ card.fields.fielde }}"},
{'varname': 'fieldb'},
{'varname': 'user:name'},
{'varname': 'user:email'},
],
},
display_mode='table',
@ -1050,7 +1060,10 @@ 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 PyQuery(result).find('table thead th:first-child').text() == value
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'
else:
assert PyQuery(result).find('table thead') == []
@ -1668,6 +1681,30 @@ def test_card_cell_card_mode_render_custom_schema_card_field(mock_send, context)
assert PyQuery(result).find('.label') == []
assert PyQuery(result).find('.value') == []
# user fields
cell.custom_schema['cells'] = [
{
'varname': 'user:name',
'field_content': 'label-and-value',
'display_mode': 'text',
},
{
'varname': 'user:email',
'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 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) .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'
)
@mock.patch('requests.Session.send', side_effect=mocked_requests_send)
def test_card_cell_card_mode_render_custom_schema_card_empty_field(mock_send, context):

View File

@ -110,6 +110,10 @@ WCS_CARDS_DATA = {
'related_raw': 42,
'related_structured': {'id': 42, 'text': 'blah'},
},
'user': {
'name': 'User Foo Bar',
'email': 'foo@bar.com',
},
},
{
'id': 12,
@ -166,7 +170,7 @@ WCS_CARDS_DATA = {
},
},
],
'card_b': [{'id': i, 'fields': []} for i in range(1, 12) if i != 6],
'card_b': [{'id': i, 'fields': {}} for i in range(1, 12) if i != 6],
'card_c': [
{
'id': 6,