Revised JSON design a bit

Closes #63
pull/81/head
Simon Willison 2017-11-11 14:20:00 -08:00
rodzic e4bf66d9b0
commit e9e1def4c0
5 zmienionych plików z 22 dodań i 12 usunięć

Wyświetl plik

@ -149,7 +149,7 @@ class BaseView(HTTPMethodView):
'error': str(e),
}
end = time.time()
data['took_ms'] = (end - start) * 1000
data['query_ms'] = (end - start) * 1000
if as_json:
# Special case for .jsono extension
if as_json == '.jsono':
@ -209,7 +209,7 @@ class IndexView(HTTPMethodView):
)[:5],
'tables_count': len(info['tables'].items()),
'tables_more': len(info['tables'].items()) > 5,
'total_rows': sum(info['tables'].values()),
'table_rows': sum(info['tables'].values()),
}
databases.append(database)
if as_json:
@ -321,9 +321,9 @@ class TableView(BaseView):
where_clause = ''
if where_clauses:
where_clause = 'where {}'.format(' and '.join(where_clauses))
where_clause = 'where {} '.format(' and '.join(where_clauses))
sql = 'select {} from "{}" {} order by {} limit {}'.format(
sql = 'select {} from "{}" {}order by {} limit {}'.format(
select, table, where_clause, order_by, self.page_size + 1,
)
@ -335,7 +335,7 @@ class TableView(BaseView):
display_columns = display_columns[1:]
rows = list(rows)
info = self.ds.metadata()
total_rows = info[name]['tables'].get(table)
table_rows = info[name]['tables'].get(table)
after = None
after_link = None
if len(rows) > self.page_size:
@ -345,11 +345,13 @@ class TableView(BaseView):
'database': name,
'table': table,
'rows': rows[:self.page_size],
'total_rows': total_rows,
'table_rows': table_rows,
'columns': columns,
'primary_keys': pks,
'sql': sql,
'sql_params': params,
'query': {
'sql': sql,
'params': params,
},
'after': after,
}, lambda: {
'database_hash': hash,

Wyświetl plik

@ -16,7 +16,7 @@
<div class="ft">
Powered by <a href="https://github.com/simonw/datasette">Datasette</a>
{% if took_ms %}&middot; Query took {{ took_ms|round(3) }}ms{% endif %}
{% if query_ms %}&middot; Query took {{ query_ms|round(3) }}ms{% endif %}
</div>
</body>

Wyświetl plik

@ -6,7 +6,7 @@
<h1>Datasette</h1>
{% for database in databases %}
<h2 style="padding-left: 10px; border-left: 10px solid #{{ database.hash[:6] }}"><a href="{{ database.path }}">{{ database.name }}</a></h2>
<p>{{ "{:,}".format(database.total_rows) }} rows in {{ database.tables_count }} table{% if database.tables_count != 1 %}s{% endif %}</p>
<p>{{ "{:,}".format(database.table_rows) }} rows in {{ database.tables_count }} table{% if database.tables_count != 1 %}s{% endif %}</p>
<p>{% for table, count in database.tables_truncated %}<a href="{{ database.path }}/{{ table }}" title="{{ count }} rows">{{ table }}</a>{% if not loop.last %}, {% endif %}{% endfor %}{% if database.tables_more %}, <a href="{{ database.path }}">...</a>{% endif %}</p>
{% endfor %}

Wyświetl plik

@ -18,8 +18,8 @@
<h1 style="padding-left: 10px; border-left: 10px solid #{{ database_hash[:6] }}">{{ table }}</h1>
{% if total_rows != None %}
<h2>{{ "{:,}".format(total_rows) }} total row{% if total_rows == 1 %}{% else %}s{% endif %} in this table</h2>
{% if table_rows != None %}
<h2>{{ "{:,}".format(table_rows) }} total row{% if table_rows == 1 %}{% else %}s{% endif %} in this table</h2>
{% endif %}
<table>

Wyświetl plik

@ -36,6 +36,14 @@ def test_database_page(three_table_app_client):
assert 'three_tables' in response.text
def test_table_page(three_table_app_client):
_, response = three_table_app_client.get('/three_tables/simple_primary_key')
assert response.status == 200
_, response = three_table_app_client.get('/three_tables/simple_primary_key.jsono')
assert response.status == 200
data = response.json
assert data['query']['sql'] == 'select * from "simple_primary_key" order by pk limit 51'
assert data['query']['params'] == {}
THREE_TABLES = '''