Add download function to imported result.

This commit is contained in:
Stephan Porada 2020-07-08 16:03:44 +02:00
parent ea7bb82661
commit 84f1d7dc39
3 changed files with 28 additions and 2 deletions

View File

@ -9,7 +9,6 @@ from .forms import (AddCorpusFileForm, AddCorpusForm, EditCorpusFileForm,
from .. import db from .. import db
from ..models import Corpus, CorpusFile from ..models import Corpus, CorpusFile
import os import os
from .. import logger
@corpora.route('/add', methods=['GET', 'POST']) @corpora.route('/add', methods=['GET', 'POST'])

View File

@ -6,7 +6,7 @@ from ..models import Result, ResultFile, User
from .forms import ImportResultsForm from .forms import ImportResultsForm
from datetime import datetime from datetime import datetime
from flask import (abort, render_template, current_app, request, redirect, from flask import (abort, render_template, current_app, request, redirect,
flash, url_for, make_response) flash, url_for, make_response, send_from_directory)
from flask_login import current_user, login_required from flask_login import current_user, login_required
import json import json
import os import os
@ -71,9 +71,11 @@ def results_overview():
# get all results of current user # get all results of current user
results = User.query.get(current_user.id).results results = User.query.get(current_user.id).results
logger.warning(results) logger.warning(results)
def __p_time(time_str): def __p_time(time_str):
# helper to convert the datetime into a nice readable string # helper to convert the datetime into a nice readable string
return datetime.strptime(time_str, '%Y-%m-%dT%H:%M:%S.%f') return datetime.strptime(time_str, '%Y-%m-%dT%H:%M:%S.%f')
# convert results into a list of dicts to add the measier to list.js in # convert results into a list of dicts to add the measier to list.js in
# the template # the template
results = [dict(query=r.corpus_metadata['query'], results = [dict(query=r.corpus_metadata['query'],
@ -82,6 +84,7 @@ def results_overview():
corpus_creation_date=__p_time(r.corpus_metadata['corpus_creation_date']), # noqa corpus_creation_date=__p_time(r.corpus_metadata['corpus_creation_date']), # noqa
corpus_analysis_date=__p_time(r.corpus_metadata['corpus_analysis_date']), # noqa corpus_analysis_date=__p_time(r.corpus_metadata['corpus_analysis_date']), # noqa
corpus_type=r.corpus_metadata['corpus_type'], corpus_type=r.corpus_metadata['corpus_type'],
file_id=r.file[0].id,
id=r.id) id=r.id)
for r in results] for r in results]
return render_template('results/results.html.j2', return render_template('results/results.html.j2',
@ -141,3 +144,19 @@ def result_delete(result_id):
tasks.delete_result(result_id) tasks.delete_result(result_id)
flash('Result deleted!') flash('Result deleted!')
return redirect(url_for('results.results_overview')) return redirect(url_for('results.results_overview'))
@results.route('/<int:result_id>/file/<int:result_file_id>/download')
@login_required
def result_download(result_id, result_file_id):
result_file = ResultFile.query.get_or_404(result_file_id)
if not result_file.result_id == result_id:
abort(404)
if not (result_file.result.creator == current_user
or current_user.is_administrator()):
abort(403)
dir = os.path.join(current_app.config['NOPAQUE_STORAGE'],
result_file.dir)
return send_from_directory(as_attachment=True,
directory=dir,
filename=result_file.filename)

View File

@ -114,6 +114,7 @@ RessourceList.dataMapper = {
corpus_type : result.corpus_type, corpus_type : result.corpus_type,
"details-link": `${result.id}/details`, "details-link": `${result.id}/details`,
"inspect-link": `${result.id}/inspect`, "inspect-link": `${result.id}/inspect`,
"download-link": `${result.id}/file/${result.file_id}/download`,
"delete-modal": `delete-result-${result.id}-modal` "delete-modal": `delete-result-${result.id}-modal`
}), }),
// Mapping for user entities shown in admin table // Mapping for user entities shown in admin table
@ -285,6 +286,12 @@ RessourceList.options = {
data-tooltip="View Results"> data-tooltip="View Results">
<i class="material-icons">search</i> <i class="material-icons">search</i>
</a> </a>
<a class="btn-floating tooltipped download-link
waves-effect waves-light"
data-position="top"
data-tooltip="Download">
<i class="material-icons">file_download</i>
</a>
<a class="btn-floating tooltipped red delete-modal <a class="btn-floating tooltipped red delete-modal
waves-effect waves-light modal-trigger" waves-effect waves-light modal-trigger"
data-position="top" data-position="top"
@ -303,6 +310,7 @@ RessourceList.options = {
"corpus_type", "corpus_type",
{name: "details-link", attr: "href"}, {name: "details-link", attr: "href"},
{name: "inspect-link", attr: "href"}, {name: "inspect-link", attr: "href"},
{name: "download-link", attr: "href"},
{name: "delete-modal", attr: "data-target"}] {name: "delete-modal", attr: "data-target"}]
}, },
// User entity blueprint setting html strucuture per entity per row // User entity blueprint setting html strucuture per entity per row