From 02ee31c8b45b872fff91e2059c15a20532e2d035 Mon Sep 17 00:00:00 2001 From: Simon Willison Date: Wed, 25 Apr 2018 20:42:57 -0700 Subject: [PATCH] New hidden: True option for table metadat, closes #239 --- datasette/app.py | 4 +++- docs/metadata.rst | 18 ++++++++++++++++++ tests/fixtures.py | 1 + tests/test_api.py | 18 +++++++++--------- 4 files changed, 31 insertions(+), 10 deletions(-) diff --git a/datasette/app.py b/datasette/app.py index 5b532c58..cf0b6ab7 100644 --- a/datasette/app.py +++ b/datasette/app.py @@ -1219,6 +1219,7 @@ class Datasette: break m.update(data) # List tables and their row counts + database_metadata = self.metadata.get('databases', {}).get(name, {}) tables = {} views = [] with sqlite3.connect('file:{}?immutable=1'.format(path), uri=True) as conn: @@ -1253,13 +1254,14 @@ class Datasette: ).fetchall()] if column_names and len(column_names) == 2 and 'id' in column_names: label_column = [c for c in column_names if c != 'id'][0] + table_metadata = database_metadata.get('tables', {}).get(table, {}) tables[table] = { 'name': table, 'columns': column_names, 'primary_keys': primary_keys, 'count': count, 'label_column': label_column, - 'hidden': False, + 'hidden': table_metadata.get('hidden') or False, } foreign_keys = get_all_foreign_keys(conn) diff --git a/docs/metadata.rst b/docs/metadata.rst index c00f06d6..3dd62940 100644 --- a/docs/metadata.rst +++ b/docs/metadata.rst @@ -142,6 +142,24 @@ used for the link label with the ``label_column`` property:: } } +Hiding tables +------------- + +You can hide tables from the database listing view (in the same way that FTS and +Spatialite tables are automatically hidden) using ``"hidden": true``:: + + { + "databases": { + "database1": { + "tables": { + "example_table": { + "hidden": true + } + } + } + } + } + Generating a metadata skeleton ------------------------------ diff --git a/tests/fixtures.py b/tests/fixtures.py index 95f7a3da..564306b1 100644 --- a/tests/fixtures.py +++ b/tests/fixtures.py @@ -90,6 +90,7 @@ METADATA = { }, 'no_primary_key': { 'sortable_columns': [], + 'hidden': True, }, 'units': { 'units': { diff --git a/tests/test_api.py b/tests/test_api.py index 130f30a3..39d8f132 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -17,7 +17,7 @@ def test_homepage(app_client): assert response.json.keys() == {'test_tables': 0}.keys() d = response.json['test_tables'] assert d['name'] == 'test_tables' - assert d['tables_count'] == 15 + assert d['tables_count'] == 14 def test_database_page(app_client): @@ -113,14 +113,6 @@ def test_database_page(app_client): }, 'label_column': None, 'primary_keys': ['pk'], - }, { - 'columns': ['content', 'a', 'b', 'c'], - 'name': 'no_primary_key', - 'count': 201, - 'hidden': False, - 'foreign_keys': {'incoming': [], 'outgoing': []}, - 'label_column': None, - 'primary_keys': [], }, { 'columns': ['id', 'content', 'content2'], 'name': 'primary_key_multiple_columns', @@ -213,6 +205,14 @@ def test_database_page(app_client): 'foreign_keys': {'incoming': [], 'outgoing': []}, 'label_column': None, 'primary_keys': ['pk'], + }, { + 'columns': ['content', 'a', 'b', 'c'], + 'name': 'no_primary_key', + 'count': 201, + 'hidden': True, + 'foreign_keys': {'incoming': [], 'outgoing': []}, + 'label_column': None, + 'primary_keys': [], }] == data['tables']