Include views on homepage, fix table counts

If we have less than 5 tables we now also show one or more views in the
summary on the homepage.

Also corrected the logic for the row counts - we now count hidden and
visible tables separately.

Closes #373, Refs #460
This commit is contained in:
Simon Willison 2019-05-15 17:28:07 -07:00
commit 5d6b2c30f1
5 changed files with 42 additions and 24 deletions

View file

@ -14,6 +14,9 @@ from datasette.version import __version__
from .base import HASH_LENGTH, RenderMixin
TRUNCATE_AT = 5
class IndexView(RenderMixin):
name = "index"
@ -42,6 +45,21 @@ class IndexView(RenderMixin):
),
}
hidden_tables = [t for t in tables.values() if t["hidden"]]
visible_tables = [t for t in tables.values() if not t["hidden"]]
tables_and_views_truncated = list(
sorted(
(t for t in tables.values() if t not in hidden_tables),
key=lambda t: t["count"] or 0,
reverse=True,
)[:TRUNCATE_AT]
)
# Only add views if this is less than TRUNCATE_AT
if len(tables_and_views_truncated) < TRUNCATE_AT:
num_views_to_add = TRUNCATE_AT - len(tables_and_views_truncated)
for view_name in views[:num_views_to_add]:
tables_and_views_truncated.append({"name": view_name})
databases.append(
{
@ -51,13 +69,10 @@ class IndexView(RenderMixin):
if db.hash
else hashlib.md5(name.encode("utf8")).hexdigest()[:6],
"path": self.database_url(name),
"tables_truncated": sorted(
(t for t in tables.values() if t not in hidden_tables),
key=lambda t: t["count"] or 0,
reverse=True,
)[:5],
"tables_count": len(tables),
"tables_more": len(tables) > 5,
"tables_and_views_truncated": tables_and_views_truncated,
"tables_and_views_more": (len(visible_tables) + len(views))
> TRUNCATE_AT,
"tables_count": len(visible_tables),
"table_rows_sum": sum((t["count"] or 0) for t in tables.values()),
"hidden_table_rows_sum": sum(
t["count"] for t in hidden_tables if t["count"] is not None