Compare commits

..

No commits in common. "cb53b27ebff9f2ed6db7331fd0d6c972f64512a3" and "bb60a2ba6719caf3d41c4fde05a9da3939b1e90d" have entirely different histories.

9 changed files with 98 additions and 108 deletions

View File

@ -1,13 +1,18 @@
from flask import Blueprint
from flask_login import login_required
bp = Blueprint('jobs', __name__)
@bp.before_request
@login_required
def before_request():
'''
Ensures that the routes in this package can only be visited by users that
are logged in.
'''
pass
from . import routes
from .inputs import bp as inputs_bp
bp.register_blueprint(inputs_bp, url_prefix='/<hashid:job_id>/inputs')
from .results import bp as results_bp
bp.register_blueprint(results_bp, url_prefix='/<hashid:job_id>/results')

View File

@ -1,7 +0,0 @@
from flask import Blueprint
bp = Blueprint('inputs', __name__)
from . import routes

View File

@ -1,27 +0,0 @@
from flask import abort, send_from_directory
from flask_login import current_user, login_required
from app.models import JobInput
from . import bp
@bp.route('/<hashid:job_input_id>/download')
@login_required
def download_job_input(job_id: int, job_input_id: int):
job_input = JobInput.query.filter_by(
job_id=job_id,
id=job_input_id
).first_or_404()
if not (
job_input.job.user == current_user
or current_user.is_administrator
):
abort(403)
return send_from_directory(
job_input.path.parent,
job_input.path.name,
as_attachment=True,
download_name=job_input.filename,
mimetype=job_input.mimetype
)

View File

@ -1,7 +0,0 @@
from flask import Blueprint
bp = Blueprint('results', __name__)
from . import routes

View File

@ -1,27 +0,0 @@
from flask import abort, send_from_directory
from flask_login import current_user, login_required
from app.models import JobResult
from . import bp
@bp.route('/<hashid:job_result_id>/download')
@login_required
def download_job_result(job_id: int, job_result_id: int):
job_result = JobResult.query.filter_by(
job_id=job_id,
id=job_result_id
).first_or_404()
if not (
job_result.job.user == current_user
or current_user.is_administrator
):
abort(403)
return send_from_directory(
job_result.path.parent,
job_result.path.name,
as_attachment=True,
download_name=job_result.filename,
mimetype=job_result.mimetype
)

View File

@ -5,24 +5,36 @@ from flask import (
jsonify,
redirect,
render_template,
send_from_directory,
url_for
)
from flask_login import current_user, login_required
from flask_login import current_user
from threading import Thread
from app import db
from app.decorators import admin_required
from app.models import Job, JobStatus
from app.models import Job, JobInput, JobResult, JobStatus
from . import bp
def _delete_job(app: Flask, job_id: int):
with app.app_context():
job = Job.query.get(job_id)
job.delete()
db.session.commit()
def _restart_job(app: Flask, job_id: int):
with app.app_context():
job = Job.query.get(job_id)
job.restart()
db.session.commit()
@bp.route('')
@login_required
def index():
def jobs():
return redirect(url_for('main.dashboard', _anchor='jobs'))
@bp.route('/<hashid:job_id>')
@login_required
def job(job_id: int):
job = Job.query.get_or_404(job_id)
@ -39,15 +51,7 @@ def job(job_id: int):
)
def _delete_job(app: Flask, job_id: int):
with app.app_context():
job = Job.query.get(job_id)
job.delete()
db.session.commit()
@bp.route('/<hashid:job_id>', methods=['DELETE'])
@login_required
def delete_job(job_id: int):
job = Job.query.get_or_404(job_id)
@ -67,10 +71,12 @@ def delete_job(job_id: int):
@bp.route('/<hashid:job_id>/log')
@admin_required
def job_log(job_id: int):
def get_job_log(job_id: int):
job = Job.query.get_or_404(job_id)
if not current_user.is_administrator:
abort(403)
if job.status not in [JobStatus.COMPLETED, JobStatus.FAILED]:
abort(409)
@ -81,15 +87,7 @@ def job_log(job_id: int):
return jsonify(log)
def _restart_job(app: Flask, job_id: int):
with app.app_context():
job = Job.query.get(job_id)
job.restart()
db.session.commit()
@bp.route('/<hashid:job_id>/restart', methods=['POST'])
@login_required
@bp.route('/<hashid:job_id>/restart')
def restart_job(job_id: int):
job = Job.query.get_or_404(job_id)
@ -109,3 +107,47 @@ def restart_job(job_id: int):
thread.start()
return jsonify(f'Job "{job.title}" marked for restarting.'), 202
@bp.route('/<hashid:job_id>/inputs/<hashid:job_input_id>/download')
def download_job_input(job_id: int, job_input_id: int):
job_input = JobInput.query.filter_by(
job_id=job_id,
id=job_input_id
).first_or_404()
if not (
job_input.job.user == current_user
or current_user.is_administrator
):
abort(403)
return send_from_directory(
job_input.path.parent,
job_input.path.name,
as_attachment=True,
download_name=job_input.filename,
mimetype=job_input.mimetype
)
@bp.route('/<hashid:job_id>/results/<hashid:job_result_id>/download')
def download_job_result(job_id: int, job_result_id: int):
job_result = JobResult.query.filter_by(
job_id=job_id,
id=job_result_id
).first_or_404()
if not (
job_result.job.user == current_user
or current_user.is_administrator
):
abort(403)
return send_from_directory(
job_result.path.parent,
job_result.path.name,
as_attachment=True,
download_name=job_result.filename,
mimetype=job_result.mimetype
)

View File

@ -20,6 +20,14 @@ class JobInput(FileMixin, HashidMixin, db.Model):
def __repr__(self):
return f'<JobInput {self.filename}>'
@property
def content_url(self):
return url_for(
'jobs.download_job_input',
job_id=self.job.id,
job_input_id=self.id
)
@property
def jsonpatch_path(self):
return f'{self.job.jsonpatch_path}/inputs/{self.hashid}'
@ -32,7 +40,7 @@ class JobInput(FileMixin, HashidMixin, db.Model):
def url(self):
return url_for(
'jobs.job',
job_input_id=self.id,
job_id=self.job_id,
_anchor=f'job-{self.job.hashid}-input-{self.hashid}'
)

View File

@ -22,6 +22,14 @@ class JobResult(FileMixin, HashidMixin, db.Model):
def __repr__(self):
return f'<JobResult {self.filename}>'
@property
def download_url(self):
return url_for(
'jobs.download_job_result',
job_id=self.job_id,
job_result_id=self.id
)
@property
def jsonpatch_path(self):
return f'{self.job.jsonpatch_path}/results/{self.hashid}'
@ -33,8 +41,8 @@ class JobResult(FileMixin, HashidMixin, db.Model):
@property
def url(self):
return url_for(
'job_results.job_result',
job_result_id=self.id,
'jobs.job',
job_id=self.job_id,
_anchor=f'job-{self.job.hashid}-result-{self.hashid}'
)

View File

@ -1,8 +1,4 @@
nopaque.app.endpoints.Jobs = class Jobs {
constructor(app) {
this.app = app;
}
async delete(jobId) {
const options = {
headers: {
@ -38,8 +34,7 @@ nopaque.app.endpoints.Jobs = class Jobs {
const options = {
headers: {
Accept: 'application/json'
},
method: 'POST'
}
};
const response = await fetch(`/jobs/${jobId}/restart`, options);