mirror of
https://github.com/simonw/datasette.git
synced 2025-12-10 16:51:24 +01:00
New sortable_columns option in metadata.json to control sort options
You can now explicitly set which columns in a table can be used for sorting
using the _sort and _sort_desc arguments using metadata.json:
{
"databases": {
"database1": {
"tables": {
"example_table": {
"sortable_columns": [
"height",
"weight"
]
}
}
}
}
}
Refs #189
This commit is contained in:
parent
7ef95d6b96
commit
d1756d7736
7 changed files with 93 additions and 23 deletions
|
|
@ -68,9 +68,19 @@ METADATA = {
|
|||
'simple_primary_key': {
|
||||
'description_html': 'Simple <em>primary</em> key',
|
||||
'title': 'This <em>HTML</em> is escaped',
|
||||
}
|
||||
},
|
||||
'sortable': {
|
||||
'sortable_columns': [
|
||||
'sortable',
|
||||
'sortable_with_nulls',
|
||||
'sortable_with_nulls_2',
|
||||
]
|
||||
},
|
||||
'no_primary_key': {
|
||||
'sortable_columns': [],
|
||||
},
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -412,12 +412,27 @@ def test_sortable_argument_errors(app_client):
|
|||
)
|
||||
assert 'Cannot sort table by badcolumn2' == response.json['error']
|
||||
response = app_client.get(
|
||||
'/test_tables/sortable.json?_sort=content&_sort_desc=pk2',
|
||||
'/test_tables/sortable.json?_sort=sortable_with_nulls&_sort_desc=sortable',
|
||||
gather_request=False
|
||||
)
|
||||
assert 'Cannot use _sort and _sort_desc at the same time' == response.json['error']
|
||||
|
||||
|
||||
def test_sortable_columns_metadata(app_client):
|
||||
response = app_client.get(
|
||||
'/test_tables/sortable.json?_sort=content',
|
||||
gather_request=False
|
||||
)
|
||||
assert 'Cannot sort table by content' == response.json['error']
|
||||
# no_primary_key has ALL sort options disabled
|
||||
for column in ('content', 'a', 'b', 'c'):
|
||||
response = app_client.get(
|
||||
'/test_tables/sortable.json?_sort={}'.format(column),
|
||||
gather_request=False
|
||||
)
|
||||
assert 'Cannot sort table by {}'.format(column) == response.json['error']
|
||||
|
||||
|
||||
@pytest.mark.parametrize('path,expected_rows', [
|
||||
('/test_tables/simple_primary_key.json?content=hello', [
|
||||
['1', 'hello'],
|
||||
|
|
|
|||
|
|
@ -200,14 +200,10 @@ 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')
|
||||
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
|
||||
))
|
||||
# We have disabled sorting for this table using metadata.json
|
||||
assert [
|
||||
'content', 'a', 'b', 'c'
|
||||
] == [th.string.strip() for th in table.select('thead th')[2:]]
|
||||
expected = [
|
||||
[
|
||||
'<td><a href="/test_tables/no_primary_key/{}">{}</a></td>'.format(i, i),
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue