Add link to export filename to view in browser

This commit is contained in:
JonnyWong16 2020-08-04 10:34:57 -07:00
parent 61c692ad4e
commit 14bb377794
No known key found for this signature in database
GPG key ID: B1F1F9807184697A
3 changed files with 58 additions and 2 deletions

View file

@ -69,8 +69,12 @@ export_table_options = {
"data": "filename",
"createdCell": function (td, cellData, rowData, row, col) {
if (cellData !== '') {
if (rowData['complete'] === 1 && rowData['exists']) {
$(td).html('<a href="view_export?export_id=' + rowData['export_id'] + '" target="_blank">' + cellData + '</a>');
} else {
$(td).html(cellData);
}
}
},
"width": "50%",
"className": "no-wrap"

View file

@ -984,7 +984,7 @@ def _real_export(export_id, items, attrs, file_format, filename):
def get_export(export_id):
db = database.MonitorDatabase()
result = db.select_single('SELECT filename, complete '
result = db.select_single('SELECT filename, file_format, complete '
'FROM exports WHERE id = ?',
[export_id])

View file

@ -22,6 +22,7 @@ from future.builtins import str
from io import open
import base64
import csv
import json
import linecache
import os
@ -6514,6 +6515,57 @@ class WebInterface(object):
else:
return {'result': 'error', 'message': 'Failed to export metadata.'}
@cherrypy.expose
@requireAuth(member_of("admin"))
def view_export(self, export_id=None, **kwargs):
""" Download an exported metadata file
```
Required parameters:
export_id (int): The row id of the exported file to view
Optional parameters:
None
Returns:
download
```
"""
result = exporter.get_export(export_id=export_id)
if result and result['complete'] == 1 and result['exists']:
if result['file_format'] == 'json':
return serve_file(exporter.get_export_filepath(result['filename']), name=result['filename'],
content_type='application/json')
elif result['file_format'] == 'csv':
with open(exporter.get_export_filepath(result['filename']), 'r', encoding='utf-8') as f:
reader = csv.DictReader(f)
table = '<table><tr><th>' + \
'</th><th>'.join(reader.fieldnames) + \
'</th></tr><tr>' + \
'</tr><tr>'.join(
'<td>' + '</td><td>'.join(row.values()) + '</td>' for row in reader) + \
'</tr></table>'
style = '<style>' \
'body {margin: 0;}' \
'table {border-collapse: collapse; overflow-y: auto; height: 100px;} ' \
'th {position: sticky; top: 0; background: #ddd; box-shadow: inset 1px 1px #000, 0 1px #000;}' \
'td {box-shadow: inset 1px -1px #000;}' \
'th, td {padding: 3px; white-space: nowrap;}' \
'</style>'
return style + '<pre>%s</pre>' % table
else:
if result and result.get('complete') == 0:
msg = 'Export is still being processed.'
elif result and result.get('complete') == -1:
msg = 'Export failed to process.'
elif result and not result.get('exists'):
msg = 'Export file does not exist.'
else:
msg = 'Invalid export_id provided.'
cherrypy.response.headers['Content-Type'] = 'application/json;charset=UTF-8'
return json.dumps({'result': 'error', 'message': msg}).encode('utf-8')
@cherrypy.expose
@requireAuth(member_of("admin"))
@addtoapi()