mirror of
https://github.com/simonw/datasette.git
synced 2025-12-10 16:51:24 +01:00
"sort" and "sort_desc" metadata properties, closes #702
This commit is contained in:
parent
e1a817411a
commit
236aa065b2
4 changed files with 85 additions and 2 deletions
|
|
@ -402,8 +402,8 @@ class TableView(RowTableShared):
|
||||||
)
|
)
|
||||||
|
|
||||||
# Allow for custom sort order
|
# Allow for custom sort order
|
||||||
sort = special_args.get("_sort")
|
sort = special_args.get("_sort") or table_metadata.get("sort")
|
||||||
sort_desc = special_args.get("_sort_desc")
|
sort_desc = special_args.get("_sort_desc") or table_metadata.get("sort_desc")
|
||||||
|
|
||||||
if sort and sort_desc:
|
if sort and sort_desc:
|
||||||
raise DatasetteError("Cannot use _sort and _sort_desc at the same time")
|
raise DatasetteError("Cannot use _sort and _sort_desc at the same time")
|
||||||
|
|
|
||||||
|
|
@ -109,6 +109,39 @@ registered with Pint::
|
||||||
.. _unit registry: https://github.com/hgrecco/pint/blob/master/pint/default_en.txt
|
.. _unit registry: https://github.com/hgrecco/pint/blob/master/pint/default_en.txt
|
||||||
.. _custom units: http://pint.readthedocs.io/en/latest/defining.html
|
.. _custom units: http://pint.readthedocs.io/en/latest/defining.html
|
||||||
|
|
||||||
|
.. _metadata_default_sort:
|
||||||
|
|
||||||
|
Setting a default sort order
|
||||||
|
----------------------------
|
||||||
|
|
||||||
|
By default Datasette tables are sorted by primary key. You can over-ride this default for a specific table using the ``"sort"`` or ``"sort_desc"`` metadata properties::
|
||||||
|
|
||||||
|
{
|
||||||
|
"databases": {
|
||||||
|
"mydatabase": {
|
||||||
|
"tables": {
|
||||||
|
"example_table": {
|
||||||
|
"sort": "created"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Or use ``"sort_desc"`` to sort in descending order::
|
||||||
|
|
||||||
|
{
|
||||||
|
"databases": {
|
||||||
|
"mydatabase": {
|
||||||
|
"tables": {
|
||||||
|
"example_table": {
|
||||||
|
"sort_desc": "created"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
.. _metadata_sortable_columns:
|
.. _metadata_sortable_columns:
|
||||||
|
|
||||||
Setting which columns can be used for sorting
|
Setting which columns can be used for sorting
|
||||||
|
|
|
||||||
|
|
@ -304,6 +304,8 @@ METADATA = {
|
||||||
"fts_table": "searchable_fts",
|
"fts_table": "searchable_fts",
|
||||||
"fts_pk": "pk",
|
"fts_pk": "pk",
|
||||||
},
|
},
|
||||||
|
"attraction_characteristic": {"sort_desc": "pk"},
|
||||||
|
"facet_cities": {"sort": "name"},
|
||||||
},
|
},
|
||||||
"queries": {
|
"queries": {
|
||||||
"𝐜𝐢𝐭𝐢𝐞𝐬": "select id, name from facet_cities order by id limit 1;",
|
"𝐜𝐢𝐭𝐢𝐞𝐬": "select id, name from facet_cities order by id limit 1;",
|
||||||
|
|
|
||||||
|
|
@ -1095,3 +1095,51 @@ def test_config_template_debug_off(app_client):
|
||||||
response = app_client.get("/fixtures/facetable?_context=1")
|
response = app_client.get("/fixtures/facetable?_context=1")
|
||||||
assert response.status == 200
|
assert response.status == 200
|
||||||
assert not response.text.startswith("<pre>{")
|
assert not response.text.startswith("<pre>{")
|
||||||
|
|
||||||
|
|
||||||
|
def test_metadata_sort(app_client):
|
||||||
|
response = app_client.get("/fixtures/facet_cities")
|
||||||
|
assert response.status == 200
|
||||||
|
table = Soup(response.body, "html.parser").find("table")
|
||||||
|
assert table["class"] == ["rows-and-columns"]
|
||||||
|
ths = table.findAll("th")
|
||||||
|
assert ["id", "name\xa0▼"] == [th.find("a").string.strip() for th in ths]
|
||||||
|
rows = [[str(td) for td in tr.select("td")] for tr in table.select("tbody tr")]
|
||||||
|
assert [
|
||||||
|
[
|
||||||
|
'<td class="col-id"><a href="/fixtures/facet_cities/3">3</a></td>',
|
||||||
|
'<td class="col-name">Detroit</td>',
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'<td class="col-id"><a href="/fixtures/facet_cities/2">2</a></td>',
|
||||||
|
'<td class="col-name">Los Angeles</td>',
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'<td class="col-id"><a href="/fixtures/facet_cities/4">4</a></td>',
|
||||||
|
'<td class="col-name">Memnonia</td>',
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'<td class="col-id"><a href="/fixtures/facet_cities/1">1</a></td>',
|
||||||
|
'<td class="col-name">San Francisco</td>',
|
||||||
|
],
|
||||||
|
] == rows
|
||||||
|
|
||||||
|
|
||||||
|
def test_metadata_sort_desc(app_client):
|
||||||
|
response = app_client.get("/fixtures/attraction_characteristic")
|
||||||
|
assert response.status == 200
|
||||||
|
table = Soup(response.body, "html.parser").find("table")
|
||||||
|
assert table["class"] == ["rows-and-columns"]
|
||||||
|
ths = table.findAll("th")
|
||||||
|
assert ["pk\xa0▲", "name"] == [th.find("a").string.strip() for th in ths]
|
||||||
|
rows = [[str(td) for td in tr.select("td")] for tr in table.select("tbody tr")]
|
||||||
|
assert [
|
||||||
|
[
|
||||||
|
'<td class="col-pk"><a href="/fixtures/attraction_characteristic/2">2</a></td>',
|
||||||
|
'<td class="col-name">Paranormal</td>',
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'<td class="col-pk"><a href="/fixtures/attraction_characteristic/1">1</a></td>',
|
||||||
|
'<td class="col-name">Museum</td>',
|
||||||
|
],
|
||||||
|
] == rows
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue