wcs: preserve ordering of static item field values in card filters (#81289) #161
|
@ -198,29 +198,37 @@ class WcsCardCellFiltersForm(forms.Form):
|
|||
continue
|
||||
field_schema = field_schemas[0]
|
||||
|
||||
options = {}
|
||||
for card in card_objects:
|
||||
card_fields = card.get('fields', {})
|
||||
card_fields.update(card.get('workflow', {}).get('fields', {}))
|
||||
value = card_fields.get(filter_id + '_raw')
|
||||
if not value:
|
||||
continue
|
||||
|
||||
display_value = card_fields[filter_id]
|
||||
if field_schema['type'] == 'item':
|
||||
options[value] = display_value
|
||||
else:
|
||||
for option_key, option_label in zip(value, display_value.split(', ')):
|
||||
options[option_key] = option_label
|
||||
if 'items' in field_schema:
|
||||
choices = [(x, x) for x in field_schema['items']]
|
||||
else:
|
||||
options = self.get_options_from_cards(card_objects, filter_id, field_schema)
|
||||
choices = sorted(options.items(), key=lambda x: x[1])
|
||||
|
||||
self.fields[filter_id] = forms.MultipleChoiceField(
|
||||
label=field_schema['label'],
|
||||
choices=sorted(options.items(), key=lambda x: x[1]),
|
||||
choices=choices,
|
||||
widget=MultipleSelect2Widget,
|
||||
)
|
||||
|
||||
self.prefix = 'c%s' % cell.get_reference()
|
||||
|
||||
def get_options_from_cards(self, card_objects, filter_id, field_schema):
|
||||
options = {}
|
||||
for card in card_objects:
|
||||
card_fields = card.get('fields', {})
|
||||
card_fields.update(card.get('workflow', {}).get('fields', {}))
|
||||
value = card_fields.get(filter_id + '_raw')
|
||||
if not value:
|
||||
continue
|
||||
|
||||
display_value = card_fields[filter_id]
|
||||
if field_schema['type'] == 'item':
|
||||
options[value] = display_value
|
||||
else:
|
||||
for option_key, option_label in zip(value, display_value.split(', ')):
|
||||
options[option_key] = option_label
|
||||
return options
|
||||
|
||||
|
||||
class WcsCategoryCellForm(forms.ModelForm):
|
||||
class Meta:
|
||||
|
|
|
@ -756,6 +756,7 @@ def test_manager_card_cell_filters(mock_send, app, admin_user):
|
|||
('', True, '---------'),
|
||||
('status', False, 'Status'),
|
||||
('fieldj', False, 'Field J'),
|
||||
('fieldl', False, 'Field L'),
|
||||
('related', False, 'Related'),
|
||||
('item', False, 'Item'),
|
||||
]
|
||||
|
@ -1632,11 +1633,11 @@ def test_card_cell_table_mode_render_filters(mock_send, settings, context, app):
|
|||
{},
|
||||
]
|
||||
|
||||
cell.filters = ['related', 'fieldj', 'status', 'item']
|
||||
cell.filters = ['related', 'fieldj', 'status', 'item', 'fieldl']
|
||||
cell.save()
|
||||
|
||||
resp = TestResponse(cell.render(context))
|
||||
assert len(resp.form.fields) == 4
|
||||
assert len(resp.form.fields) == 5
|
||||
assert resp.form['c%s-fieldj' % cell.get_reference()].options == [
|
||||
('first value', False, 'First Value'),
|
||||
('second value \'', False, 'Second Value \''),
|
||||
|
@ -1650,6 +1651,11 @@ def test_card_cell_table_mode_render_filters(mock_send, settings, context, app):
|
|||
('bar', False, 'bar'),
|
||||
('foo', False, 'foo'),
|
||||
]
|
||||
assert resp.form['c%s-fieldl' % cell.get_reference()].options == [
|
||||
('A', False, 'A'),
|
||||
('C', False, 'C'),
|
||||
('B', False, 'B'),
|
||||
]
|
||||
assert [x.attrib for x in resp.pyquery('.list-of-cards li')] == [
|
||||
{
|
||||
'data-c%s-related' % cell.get_reference(): '["42"]',
|
||||
|
|
|
@ -291,6 +291,7 @@ WCS_CARDDEF_SCHEMAS = {
|
|||
{'label': 'Field I', 'varname': 'fieldi', 'type': 'text', 'display_mode': 'rich'},
|
||||
{'label': 'Field J', 'varname': 'fieldj', 'type': 'items'},
|
||||
{'label': 'Field K', 'type': 'item'},
|
||||
{'label': 'Field L', 'varname': 'fieldl', 'type': 'item', 'items': ['A', 'C', 'B']},
|
||||
{'label': 'Empty', 'varname': 'empty', 'type': 'string'},
|
||||
{'label': 'Empty Email', 'varname': 'empty_email', 'type': 'email'},
|
||||
{'label': 'Related', 'varname': 'related', 'type': 'item'},
|
||||
|
|
Loading…
Reference in New Issue