From 747a801b50487cd4cc20856d44252ac0a6cb346f Mon Sep 17 00:00:00 2001 From: Simon Willison Date: Sun, 8 Apr 2018 19:08:28 -0700 Subject: [PATCH] Column headers now link to sort/desc sort - refs #189 --- datasette/app.py | 8 +++++ datasette/templates/_rows_and_columns.html | 14 +++++++- tests/test_html.py | 42 +++++++++++++++------- 3 files changed, 50 insertions(+), 14 deletions(-) diff --git a/datasette/app.py b/datasette/app.py index 67ee207f..17a82109 100644 --- a/datasette/app.py +++ b/datasette/app.py @@ -802,6 +802,12 @@ class TableView(RowTableShared): 'display_columns': display_columns, 'filter_columns': filter_columns, 'display_rows': display_rows, + 'path_with_added_args': path_with_added_args, + 'request': request, + 'enumerate': enumerate, + 'sort': sort, + 'sort_desc': sort_desc, + 'disable_sort': is_view, 'custom_rows_and_columns_templates': [ '_rows_and_columns-{}-{}.html'.format(to_css_class(name), to_css_class(table)), '_rows_and_columns-table-{}-{}.html'.format(to_css_class(name), to_css_class(table)), @@ -876,6 +882,8 @@ class RowView(RowTableShared): '_rows_and_columns-row-{}-{}.html'.format(to_css_class(name), to_css_class(table)), '_rows_and_columns.html', ], + 'disable_sort': True, + 'enumerate': enumerate, 'metadata': self.ds.metadata.get( 'databases', {} ).get(name, {}).get('tables', {}).get(table, {}), diff --git a/datasette/templates/_rows_and_columns.html b/datasette/templates/_rows_and_columns.html index 7695be3d..4a2a727f 100644 --- a/datasette/templates/_rows_and_columns.html +++ b/datasette/templates/_rows_and_columns.html @@ -1,7 +1,19 @@ - {% for column in display_columns %}{% endfor %} + {% for i, column in enumerate(display_columns) %} + + {% endfor %} diff --git a/tests/test_html.py b/tests/test_html.py index c93fdcdc..691bf27e 100644 --- a/tests/test_html.py +++ b/tests/test_html.py @@ -157,9 +157,15 @@ def test_css_classes_on_body(app_client, path, expected_classes): def test_table_html_simple_primary_key(app_client): response = app_client.get('/test_tables/simple_primary_key', gather_request=False) table = Soup(response.body, 'html.parser').find('table') - assert [ - 'Link', 'pk', 'content' - ] == [th.string for th in table.select('thead th')] + ths = table.findAll('th') + assert 'Link' == ths[0].string.strip() + for expected_col, th in zip(('pk', 'content'), ths[1:]): + a = th.find('a') + assert expected_col == a.string + assert a['href'].endswith('/simple_primary_key?_sort={}'.format( + expected_col + )) + assert ['nofollow'] == a['rel'] assert [ [ '', @@ -182,7 +188,7 @@ def test_row_html_simple_primary_key(app_client): table = Soup(response.body, 'html.parser').find('table') assert [ 'pk', 'content' - ] == [th.string for th in table.select('thead th')] + ] == [th.string.strip() for th in table.select('thead th')] assert [ [ '', @@ -194,9 +200,14 @@ def test_row_html_simple_primary_key(app_client): def test_table_html_no_primary_key(app_client): response = app_client.get('/test_tables/no_primary_key', gather_request=False) table = Soup(response.body, 'html.parser').find('table') - assert [ - 'Link', 'rowid', 'content', 'a', 'b', 'c' - ] == [th.string for th in table.select('thead th')] + ths = table.findAll('th') + assert 'Link' == ths[0].string.strip() + for expected_col, th in zip(('rowid', 'content', 'a', 'b', 'c'), ths[1:]): + a = th.find('a') + assert expected_col == a.string + assert a['href'].endswith('/no_primary_key?_sort={}'.format( + expected_col + )) expected = [ [ ''.format(i, i), @@ -215,7 +226,7 @@ def test_row_html_no_primary_key(app_client): table = Soup(response.body, 'html.parser').find('table') assert [ 'rowid', 'content', 'a', 'b', 'c' - ] == [th.string for th in table.select('thead th')] + ] == [th.string.strip() for th in table.select('thead th')] expected = [ [ '', @@ -231,9 +242,14 @@ def test_row_html_no_primary_key(app_client): def test_table_html_compound_primary_key(app_client): response = app_client.get('/test_tables/compound_primary_key', gather_request=False) table = Soup(response.body, 'html.parser').find('table') - assert [ - 'Link', 'pk1', 'pk2', 'content' - ] == [th.string for th in table.select('thead th')] + ths = table.findAll('th') + assert 'Link' == ths[0].string.strip() + for expected_col, th in zip(('pk1', 'pk2', 'content'), ths[1:]): + a = th.find('a') + assert expected_col == a.string + assert a['href'].endswith('/compound_primary_key?_sort={}'.format( + expected_col + )) expected = [ [ '', @@ -250,7 +266,7 @@ def test_row_html_compound_primary_key(app_client): table = Soup(response.body, 'html.parser').find('table') assert [ 'pk1', 'pk2', 'content' - ] == [th.string for th in table.select('thead th')] + ] == [th.string.strip() for th in table.select('thead th')] expected = [ [ '', @@ -266,7 +282,7 @@ def test_view_html(app_client): table = Soup(response.body, 'html.parser').find('table') assert [ 'content', 'upper_content' - ] == [th.string for th in table.select('thead th')] + ] == [th.string.strip() for th in table.select('thead th')] expected = [ [ '',
{{ column }} + {% if i == 0 or disable_sort %} + {{ column }} + {% else %} + {% if column == sort %} + {{ column }} ▼ + {% else %} + {{ column }}{% if column == sort_desc %} ▲{% endif %} + {% endif %} + {% endif %} +
11{}1a,bahello