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
pull/479/head
Simon Willison 2019-05-15 17:28:07 -07:00
rodzic e04511410f
commit 5d6b2c30f1
5 zmienionych plików z 42 dodań i 24 usunięć

Wyświetl plik

@ -12,16 +12,16 @@
{% for database in databases %}
<h2 style="padding-left: 10px; border-left: 10px solid #{{ database.color }}"><a href="{{ database.path }}">{{ database.name }}</a></h2>
<p>
{{ "{:,}".format(database.table_rows_sum) }} rows in {{ database.tables_count }} table{% if database.tables_count != 1 %}s{% endif %}{% if database.tables_count and database.hidden_tables_count %}, {% endif %}
{% if database.hidden_tables_count %}
{{ "{:,}".format(database.hidden_table_rows_sum) }} rows in {{ database.hidden_tables_count }} hidden table{% if database.hidden_tables_count != 1 %}s{% endif %}
{% endif %}
{% if database.views_count %}
{% if database.tables_count or database.hidden_tables_count %} - {% endif %}
{{ "{:,}".format(database.table_rows_sum) }} rows in {{ database.tables_count }} table{% if database.tables_count != 1 %}s{% endif %}{% if database.tables_count and database.hidden_tables_count %}, {% endif -%}
{% if database.hidden_tables_count -%}
{{ "{:,}".format(database.hidden_table_rows_sum) }} rows in {{ database.hidden_tables_count }} hidden table{% if database.hidden_tables_count != 1 %}s{% endif -%}
{% endif -%}
{% if database.views_count -%}
{% if database.tables_count or database.hidden_tables_count %}, {% endif -%}
{{ "{:,}".format(database.views_count) }} view{% if database.views_count != 1 %}s{% endif %}
{% endif %}
</p>
<p>{% for table in database.tables_truncated %}<a href="{{ database.path }}/{{ table.name|quote_plus }}" title="{{ table.count }} rows">{{ table.name }}</a>{% if not loop.last %}, {% endif %}{% endfor %}{% if database.tables_more %}, <a href="{{ database.path }}">...</a>{% endif %}</p>
<p>{% for table in database.tables_and_views_truncated %}<a href="{{ database.path }}/{{ table.name|quote_plus }}"{% if table.count %} title="{{ table.count }} rows"{% endif %}>{{ table.name }}</a>{% if not loop.last %}, {% endif %}{% endfor %}{% if database.tables_and_views_more %}, <a href="{{ database.path }}">...</a>{% endif %}</p>
{% endfor %}
{% endblock %}

Wyświetl plik

@ -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

Wyświetl plik

@ -612,6 +612,8 @@ CREATE TABLE searchable (
text2 text
);
CREATE VIEW searchable_view AS SELECT * FROM searchable;
INSERT INTO searchable VALUES (1, 'barry cat', 'terry dog');
INSERT INTO searchable VALUES (2, 'terry dog', 'sara weasel');

Wyświetl plik

@ -24,9 +24,9 @@ def test_homepage(app_client):
assert response.json.keys() == {"fixtures": 0}.keys()
d = response.json["fixtures"]
assert d["name"] == "fixtures"
assert d["tables_count"] == 26
assert len(d["tables_truncated"]) == 5
assert d["tables_more"] is True
assert d["tables_count"] == 21
assert len(d["tables_and_views_truncated"]) == 5
assert d["tables_and_views_more"] is True
# 4 hidden FTS tables + no_primary_key (hidden in metadata)
assert d["hidden_tables_count"] == 5
# 201 in no_primary_key, plus 5 in other hidden tables:
@ -448,8 +448,8 @@ def test_no_files_uses_memory_database(app_client_no_files):
"path": "/:memory:",
"table_rows_sum": 0,
"tables_count": 0,
"tables_more": False,
"tables_truncated": [],
"tables_and_views_more": False,
"tables_and_views_truncated": [],
"views_count": 0,
}
} == response.json

Wyświetl plik

@ -32,16 +32,17 @@ def test_homepage(app_client_two_attached_databases):
h2 = soup.select("h2")[1]
assert "extra_database" == h2.text.strip()
counts_p, links_p = h2.find_all_next("p")
assert "7 rows in 5 tables, 5 rows in 4 hidden tables" == counts_p.text.strip().replace(
" ", ""
).replace(
"\n", ""
assert (
"7 rows in 1 table, 5 rows in 4 hidden tables, 1 view" == counts_p.text.strip()
)
# We should only show visible, not hidden tables here:
table_links = [
{"href": a["href"], "text": a.text.strip()} for a in links_p.findAll("a")
]
assert [{"href": "/extra_database/searchable", "text": "searchable"}] == table_links
assert [
{"href": "/extra_database/searchable", "text": "searchable"},
{"href": "/extra_database/searchable_view", "text": "searchable_view"},
] == table_links
def test_memory_database_page(app_client_with_memory):