sortable_columns also now works with views

This commit is contained in:
Simon Willison 2018-08-05 17:29:23 -07:00
commit 5629aaca67
No known key found for this signature in database
GPG key ID: 17E2DEA2588B7F52
4 changed files with 42 additions and 18 deletions

View file

@ -352,7 +352,8 @@ class TableView(RowTableShared):
sortable_columns = set()
if not is_view:
table_rows_count = table_info["count"]
sortable_columns = self.sortable_columns_for_table(name, table, use_rowid)
sortable_columns = self.sortable_columns_for_table(name, table, use_rowid)
# Allow for custom sort order
sort = special_args.get("_sort")

View file

@ -121,6 +121,23 @@ This will restrict sorting of ``example_table`` to just the ``height`` and
You can also disable sorting entirely by setting ``"sortable_columns": []``
By default, database views in Datasette do not support sorting. You can use ``sortable_columns`` to enable specific sort orders for a view called ``name_of_view`` in the database ``my_database`` like so::
{
"databases": {
"my_database": {
"tables": {
"name_of_view": {
"sortable_columns": [
"clicks",
"impressions"
]
}
}
}
}
}
.. _label_columns:
Specifying the label column for a table

View file

@ -158,6 +158,9 @@ METADATA = {
'primary_key_multiple_columns_explicit_label': {
'label_column': 'content2',
},
'simple_view': {
'sortable_columns': ['content'],
}
},
'queries': {
'pragma_cache_size': 'PRAGMA cache_size;',

View file

@ -40,11 +40,6 @@ def test_sql_time_limit(app_client_shorter_time_limit):
assert expected_html_fragment in response.text
def test_view(app_client):
response = app_client.get('/fixtures/simple_view')
assert response.status == 200
def test_row(app_client):
response = app_client.get(
'/fixtures/simple_primary_key/1',
@ -618,25 +613,33 @@ def test_compound_primary_key_with_foreign_key_references(app_client):
def test_view_html(app_client):
response = app_client.get('/fixtures/simple_view')
response = app_client.get("/fixtures/simple_view")
assert response.status == 200
table = Soup(response.body, 'html.parser').find('table')
assert [
'content', 'upper_content'
] == [th.string.strip() for th in table.select('thead th')]
table = Soup(response.body, "html.parser").find("table")
ths = table.select("thead th")
assert 2 == len(ths)
assert ths[0].find("a") is not None
assert ths[0].find("a")["href"].endswith("/simple_view?_sort=content")
assert ths[0].find("a").string.strip() == "content"
assert ths[1].find("a") is None
assert ths[1].string.strip() == "upper_content"
expected = [
[
'<td class="col-content">hello</td>',
'<td class="col-upper_content">HELLO</td>'
], [
'<td class="col-upper_content">HELLO</td>',
],
[
'<td class="col-content">world</td>',
'<td class="col-upper_content">WORLD</td>'
], [
'<td class="col-upper_content">WORLD</td>',
],
[
'<td class="col-content">\xa0</td>',
'<td class="col-upper_content">\xa0</td>'
]
'<td class="col-upper_content">\xa0</td>',
],
]
assert expected == [
[str(td) for td in tr.select("td")] for tr in table.select("tbody tr")
]
assert expected == [[str(td) for td in tr.select('td')] for tr in table.select('tbody tr')]
def test_index_metadata(app_client):