.blob output renderer

* _blob_hash= checking plus refactored to use new BadRequest class, refs #1050
* Replace BlobView with new .blob renderer, closes #1050
* .blob downloads on arbitrary queries, closes #1051
This commit is contained in:
Simon Willison 2020-10-29 15:01:38 -07:00 committed by GitHub
commit 78b3eeaad9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 165 additions and 121 deletions

View file

@ -1,4 +1,5 @@
import os
import hashlib
import itertools
import jinja2
import json
@ -10,6 +11,7 @@ from datasette.utils import (
validate_sql_select,
is_url,
path_with_added_args,
path_with_format,
path_with_removed_args,
InvalidSql,
)
@ -342,6 +344,24 @@ class QueryView(DataView):
url=jinja2.escape(value.strip())
)
)
elif isinstance(display_value, bytes):
blob_url = path_with_format(
request,
"blob",
extra_qs={
"_blob_column": column,
"_blob_hash": hashlib.sha256(
display_value
).hexdigest(),
},
)
display_value = jinja2.Markup(
'<a class="blob-download" href="{}">&lt;Binary:&nbsp;{}&nbsp;byte{}&gt;</a>'.format(
blob_url,
len(display_value),
"" if len(value) == 1 else "s",
)
)
display_row.append(display_value)
display_rows.append(display_row)