Ran black source formatting tool against new views/ and app.py

This commit is contained in:
Simon Willison 2018-05-13 09:55:15 -03:00 committed by Simon Willison
commit 3686385551
5 changed files with 737 additions and 627 deletions

View file

@ -1,13 +1,12 @@
from .base import RenderMixin, HASH_LENGTH
from sanic import response
from datasette.utils import (
CustomJSONEncoder,
)
from datasette.utils import CustomJSONEncoder
from datasette.version import __version__
import json
class IndexView(RenderMixin):
def __init__(self, datasette):
self.ds = datasette
self.files = datasette.files
@ -17,40 +16,38 @@ class IndexView(RenderMixin):
async def get(self, request, as_json):
databases = []
for key, info in sorted(self.ds.inspect().items()):
tables = [t for t in info['tables'].values() if not t['hidden']]
hidden_tables = [t for t in info['tables'].values() if t['hidden']]
tables = [t for t in info["tables"].values() if not t["hidden"]]
hidden_tables = [t for t in info["tables"].values() if t["hidden"]]
database = {
'name': key,
'hash': info['hash'],
'path': '{}-{}'.format(key, info['hash'][:HASH_LENGTH]),
'tables_truncated': sorted(
tables,
key=lambda t: t['count'],
reverse=True
)[:5],
'tables_count': len(tables),
'tables_more': len(tables) > 5,
'table_rows_sum': sum(t['count'] for t in tables),
'hidden_table_rows_sum': sum(t['count'] for t in hidden_tables),
'hidden_tables_count': len(hidden_tables),
'views_count': len(info['views']),
"name": key,
"hash": info["hash"],
"path": "{}-{}".format(key, info["hash"][:HASH_LENGTH]),
"tables_truncated": sorted(
tables, key=lambda t: t["count"], reverse=True
)[
:5
],
"tables_count": len(tables),
"tables_more": len(tables) > 5,
"table_rows_sum": sum(t["count"] for t in tables),
"hidden_table_rows_sum": sum(t["count"] for t in hidden_tables),
"hidden_tables_count": len(hidden_tables),
"views_count": len(info["views"]),
}
databases.append(database)
if as_json:
headers = {}
if self.ds.cors:
headers['Access-Control-Allow-Origin'] = '*'
headers["Access-Control-Allow-Origin"] = "*"
return response.HTTPResponse(
json.dumps(
{db['name']: db for db in databases},
cls=CustomJSONEncoder
),
content_type='application/json',
json.dumps({db["name"]: db for db in databases}, cls=CustomJSONEncoder),
content_type="application/json",
headers=headers,
)
else:
return self.render(
['index.html'],
["index.html"],
databases=databases,
metadata=self.ds.metadata,
datasette_version=__version__,