misc: include label (+ index) in all column headers (#76363) #507

Merged
fpeters merged 1 commits from wip/76363-column-header-names into main 2023-07-17 15:34:52 +02:00
2 changed files with 24 additions and 15 deletions

View File

@ -225,7 +225,12 @@ def test_items(pub):
assert field.get_options() == [('1', 'foo', '1'), ('2', 'bar', '2')]
assert field.get_options() == [('1', 'foo', '1'), ('2', 'bar', '2')] # twice for cached behaviour
assert field.get_csv_heading() == ['plop (identifier)', 'plop (label)', '(continued)', '(continued)']
assert field.get_csv_heading() == [
'plop 1 (identifier)',
'plop 1 (label)',
'plop 2 (identifier)',
'plop 2 (label)',
]
assert field.get_csv_value(['a', 'b'], structured_value=json.loads(field.data_source['value'])) == [
'1',
'foo',
@ -235,7 +240,7 @@ def test_items(pub):
# check values is cut on max choices
field.max_choices = 1
assert field.get_csv_heading() == ['plop (identifier)', 'plop (label)']
assert field.get_csv_heading() == ['plop 1 (identifier)', 'plop 1 (label)']
assert field.get_csv_value(['a', 'b'], structured_value=json.loads(field.data_source['value'])) == [
'1',
'foo',
@ -248,12 +253,12 @@ def test_items(pub):
'value'
] = '[{"id": "1", "text": "foo"}, {"id": "2", "text": "bar"}, {"id": "3", "text": "baz"}]'
assert field.get_csv_heading() == [
'plop (identifier)',
'plop (label)',
'(continued)',
'(continued)',
'(continued)',
'(continued)',
'plop 1 (identifier)',
'plop 1 (label)',
'plop 2 (identifier)',
'plop 2 (label)',
'plop 3 (identifier)',
'plop 3 (label)',
]
assert field.get_csv_value(['a', 'b'], structured_value=json.loads(field.data_source['value'])[:2]) == [
'1',

View File

@ -2969,16 +2969,20 @@ class ItemsField(WidgetField, ItemFieldMixin, ItemWithImageFieldMixin):
return item_items_stats(self, values)
def get_csv_heading(self):
label = str(self.label or '-')
quals = ['']
if self.data_source:
labels = ['%s (%s)' % (self.label, _('identifier')), '%s (%s)' % (self.label, _('label'))]
next_columns = [_('(continued)')] * 2
else:
labels = [self.label]
next_columns = [_('(continued)')]
quals = ['(%s)' % _('identifier'), '(%s)' % _('label')]
nb_columns = 1
if self.max_choices:
labels.extend(next_columns * (self.max_choices - 1))
nb_columns = self.max_choices
elif len(self.get_options()):
labels.extend(next_columns * (len(self.get_options()) - 1))
nb_columns = len(self.get_options())
labels = []
for i in range(nb_columns):
for q in quals:
labels.append(' '.join((label, str(i + 1), q)).strip())
return labels
def get_csv_value(self, element, structured_value=None, **kwargs):