Skip to content

Commit

Permalink
Column headers now link to sort/desc sort - refs #189
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw authored and Simon Willison committed Apr 9, 2018
1 parent 9f2ec39 commit 747a801
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 14 deletions.
8 changes: 8 additions & 0 deletions datasette/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -802,6 +802,12 @@ async def extra_template():
'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)),
Expand Down Expand Up @@ -876,6 +882,8 @@ async def template_data():
'_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, {}),
Expand Down
14 changes: 13 additions & 1 deletion datasette/templates/_rows_and_columns.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
<table>
<thead>
<tr>
{% for column in display_columns %}<th scope="col">{{ column }}</th>{% endfor %}
{% for i, column in enumerate(display_columns) %}
<th scope="col">
{% if i == 0 or disable_sort %}
{{ column }}
{% else %}
{% if column == sort %}
<a href="{{ path_with_added_args(request, {'_sort_desc': column, '_sort': None, '_next': None}) }}" rel="nofollow">{{ column }}&nbsp;▼</a>
{% else %}
<a href="{{ path_with_added_args(request, {'_sort': column, '_sort_desc': None, '_next': None}) }}" rel="nofollow">{{ column }}{% if column == sort_desc %}&nbsp;▲{% endif %}</a>
{% endif %}
{% endif %}
</th>
{% endfor %}
</tr>
</thead>
<tbody>
Expand Down
42 changes: 29 additions & 13 deletions tests/test_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 [
[
'<td><a href="/test_tables/simple_primary_key/1">1</a></td>',
Expand All @@ -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 [
[
'<td>1</td>',
Expand All @@ -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 = [
[
'<td><a href="/test_tables/no_primary_key/{}">{}</a></td>'.format(i, i),
Expand All @@ -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 = [
[
'<td>1</td>',
Expand All @@ -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 = [
[
'<td><a href="/test_tables/compound_primary_key/a,b">a,b</a></td>',
Expand All @@ -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 = [
[
'<td>a</td>',
Expand All @@ -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 = [
[
'<td>hello</td>',
Expand Down

0 comments on commit 747a801

Please sign in to comment.