Add download helper for dev process.

This commit is contained in:
Patrick Jentsch 2019-08-12 10:36:51 +02:00
parent e00d34ef81
commit fa2827608c
2 changed files with 25 additions and 5 deletions

View File

@ -1,4 +1,4 @@
from flask import abort, current_app, flash, redirect, render_template, url_for from flask import abort, current_app, flash, redirect, request, render_template, url_for, send_from_directory
from flask_login import current_user, login_required from flask_login import current_user, login_required
from ..models import Corpus, User, Job from ..models import Corpus, User, Job
from ..tables import AdminUserTable, AdminUserItem from ..tables import AdminUserTable, AdminUserItem
@ -81,17 +81,37 @@ def job(job_id):
if file == 'output': if file == 'output':
continue continue
files[file] = {} files[file] = {}
files[file]['path'] = os.path.join(dir, file) files[file]['path'] = os.path.join(file)
if job.status == 'complete': if job.status == 'complete':
files[file]['results'] = {} files[file]['results'] = {}
results_dir = os.path.join(dir, 'output', file) results_dir = os.path.join(dir, 'output', file)
for result in os.listdir(results_dir): for result in os.listdir(results_dir):
files[file]['results'][result] = {} files[file]['results'][result] = {}
files[file]['results'][result]['path'] = os.path.join( files[file]['results'][result]['path'] = os.path.join(
results_dir, result 'output', files[file]['path'], result
) )
return render_template('main/jobs/job.html.j2', return render_template('main/jobs/job.html.j2',
files=files, files=files,
job=job, job=job,
title='Job') title='Job')
@main.route('/jobs/<int:job_id>/download')
@login_required
def job_download(job_id):
file = request.args.get('file')
job = Job.query.filter_by(id=job_id).first()
if not file or not job:
print('Job not found.')
abort(404)
elif not job.user_id == current_user.id:
print('Job does not belong to current user.')
abort(403)
dir = os.path.join(current_app.config['OPAQUE_STORAGE'],
str(current_user.id),
'jobs',
str(job.id))
print(dir)
print(file)
return send_from_directory(directory=dir, filename=file, as_attachment=True)

View File

@ -72,7 +72,7 @@
</div> </div>
<p> <p>
{% for file in files %} {% for file in files %}
<a href="file://{{ files[file]['path'] }}" class="waves-effect waves-light btn-small"> <a href="{{ url_for('main.job_download', job_id=job.id, file=files[file]['path']) }}" class="waves-effect waves-light btn-small">
<i class="material-icons left">file_download</i>{{ file }} <i class="material-icons left">file_download</i>{{ file }}
</a> </a>
{% endfor %} {% endfor %}
@ -84,7 +84,7 @@
<p> <p>
{% for file in files %} {% for file in files %}
{% for result in files[file]['results'] %} {% for result in files[file]['results'] %}
<a href="file://{{ files[file]['results'][result]['path'] }}" class="waves-effect waves-light btn-small"> <a href="{{ url_for('main.job_download', job_id=job.id, file=files[file]['results'][result]['path']) }}" class="waves-effect waves-light btn-small">
<i class="material-icons left">file_download</i>{{ result }} <i class="material-icons left">file_download</i>{{ result }}
</a> </a>
{% endfor %} {% endfor %}