From 4586aa506a054d07e674cde8143a3008e6bc5d78 Mon Sep 17 00:00:00 2001 From: Russ Garrett Date: Sun, 15 Apr 2018 22:49:01 +0100 Subject: [PATCH] Don't duplicate simple primary keys in the link column When there's a simple (single-column) primary key, it looks weird to duplicate it in the link column. This change removes the second PK column and treats the link column as if it were the PK column from a header/sorting perspective. --- datasette/app.py | 35 +++++++++++++++++++++++------------ 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/datasette/app.py b/datasette/app.py index 337fcdae..5e2d21d1 100644 --- a/datasette/app.py +++ b/datasette/app.py @@ -532,8 +532,13 @@ class RowTableShared(BaseView): ) ), }) + for value, column_dict in zip(row, columns): column = column_dict['name'] + if link_column and len(pks) == 1 and column == pks[0]: + # If there's a simple primary key, don't repeat the value as it's + # already shown in the link column. + continue if (column, value) in labeled_fks: other_table, label = labeled_fks[(column, value)] display_value = jinja2.Markup( @@ -560,17 +565,17 @@ class RowTableShared(BaseView): url=jinja2.escape(value.strip()) ) ) + elif column in table_metadata.get('units', {}) and value != '': + # Interpret units using pint + value = value * ureg(table_metadata['units'][column]) + # Pint uses floating point which sometimes introduces errors in the compact + # representation, which we have to round off to avoid ugliness. In the vast + # majority of cases this rounding will be inconsequential. I hope. + value = round(value.to_compact(), 6) + display_value = jinja2.Markup('{:~P}'.format(value).replace(' ', ' ')) else: - if column in table_metadata.get('units', {}) and value != '': - # Interpret units using pint - value = value * ureg(table_metadata['units'][column]) - # Pint uses floating point which sometimes introduces errors in the compact - # representation, which we have to round off to avoid ugliness. In the vast - # majority of cases this rounding will be inconsequential. I hope. - value = round(value.to_compact(), 6) - display_value = jinja2.Markup('{:~P}'.format(value).replace(' ', ' ')) - else: - display_value = str(value) + display_value = str(value) + cells.append({ 'column': column, 'value': display_value, @@ -578,9 +583,15 @@ class RowTableShared(BaseView): cell_rows.append(cells) if link_column: + # Add the link column header. + # If it's a simple primary key, we have to remove and re-add that column name at + # the beginning of the header row. + if len(pks) == 1: + columns = [col for col in columns if col['name'] != pks[0]] + columns = [{ - 'name': 'Link', - 'sortable': False, + 'name': pks[0] if len(pks) == 1 else 'Link', + 'sortable': len(pks) == 1, }] + columns return columns, cell_rows