mirror of
https://github.com/simonw/datasette.git
synced 2025-12-10 16:51:24 +01:00
Refactored views into new views/ modules, refs #256
This commit is contained in:
parent
4301a8f3ac
commit
1f69269fe9
6 changed files with 1165 additions and 1132 deletions
59
datasette/views/index.py
Normal file
59
datasette/views/index.py
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
from .base import RenderMixin, HASH_LENGTH
|
||||
from sanic import response
|
||||
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
|
||||
self.jinja_env = datasette.jinja_env
|
||||
self.executor = datasette.executor
|
||||
|
||||
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']]
|
||||
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']),
|
||||
}
|
||||
databases.append(database)
|
||||
if as_json:
|
||||
headers = {}
|
||||
if self.ds.cors:
|
||||
headers['Access-Control-Allow-Origin'] = '*'
|
||||
return response.HTTPResponse(
|
||||
json.dumps(
|
||||
{db['name']: db for db in databases},
|
||||
cls=CustomJSONEncoder
|
||||
),
|
||||
content_type='application/json',
|
||||
headers=headers,
|
||||
)
|
||||
else:
|
||||
return self.render(
|
||||
['index.html'],
|
||||
databases=databases,
|
||||
metadata=self.ds.metadata,
|
||||
datasette_version=__version__,
|
||||
extra_css_urls=self.ds.extra_css_urls(),
|
||||
extra_js_urls=self.ds.extra_js_urls(),
|
||||
)
|
||||
Loading…
Add table
Add a link
Reference in a new issue