Skip to content

Commit

Permalink
New hidden: True option for table metadat, closes #239
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw committed Apr 26, 2018
1 parent d3a0069 commit 02ee31c
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 10 deletions.
4 changes: 3 additions & 1 deletion datasette/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -1219,6 +1219,7 @@ def inspect(self):
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:
Expand Down Expand Up @@ -1253,13 +1254,14 @@ def inspect(self):
).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)
Expand Down
18 changes: 18 additions & 0 deletions docs/metadata.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
------------------------------

Expand Down
1 change: 1 addition & 0 deletions tests/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ def generate_sortable_rows(num):
},
'no_primary_key': {
'sortable_columns': [],
'hidden': True,
},
'units': {
'units': {
Expand Down
18 changes: 9 additions & 9 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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']


Expand Down

0 comments on commit 02ee31c

Please sign in to comment.