Compare commits

...

3 Commits

Author SHA1 Message Date
Patrick Jentsch
cb53b27ebf Rename function 2024-12-12 15:55:39 +01:00
Patrick Jentsch
6684257bc4 added job inputs/results blueprints 2024-12-12 15:32:56 +01:00
Patrick Jentsch
0d1805fb76 Update Job Blueprint package 2024-12-12 15:26:01 +01:00
9 changed files with 108 additions and 98 deletions

View File

@ -1,18 +1,13 @@
from flask import Blueprint from flask import Blueprint
from flask_login import login_required
bp = Blueprint('jobs', __name__) 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 . 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

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

View File

@ -0,0 +1,27 @@
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

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

View File

@ -0,0 +1,27 @@
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,36 +5,24 @@ from flask import (
jsonify, jsonify,
redirect, redirect,
render_template, render_template,
send_from_directory,
url_for url_for
) )
from flask_login import current_user from flask_login import current_user, login_required
from threading import Thread from threading import Thread
from app import db from app import db
from app.models import Job, JobInput, JobResult, JobStatus from app.decorators import admin_required
from app.models import Job, JobStatus
from . import bp 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('') @bp.route('')
def jobs(): @login_required
def index():
return redirect(url_for('main.dashboard', _anchor='jobs')) return redirect(url_for('main.dashboard', _anchor='jobs'))
@bp.route('/<hashid:job_id>') @bp.route('/<hashid:job_id>')
@login_required
def job(job_id: int): def job(job_id: int):
job = Job.query.get_or_404(job_id) job = Job.query.get_or_404(job_id)
@ -51,7 +39,15 @@ 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']) @bp.route('/<hashid:job_id>', methods=['DELETE'])
@login_required
def delete_job(job_id: int): def delete_job(job_id: int):
job = Job.query.get_or_404(job_id) job = Job.query.get_or_404(job_id)
@ -71,12 +67,10 @@ def delete_job(job_id: int):
@bp.route('/<hashid:job_id>/log') @bp.route('/<hashid:job_id>/log')
def get_job_log(job_id: int): @admin_required
def job_log(job_id: int):
job = Job.query.get_or_404(job_id) 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]: if job.status not in [JobStatus.COMPLETED, JobStatus.FAILED]:
abort(409) abort(409)
@ -87,7 +81,15 @@ def get_job_log(job_id: int):
return jsonify(log) return jsonify(log)
@bp.route('/<hashid:job_id>/restart') 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
def restart_job(job_id: int): def restart_job(job_id: int):
job = Job.query.get_or_404(job_id) job = Job.query.get_or_404(job_id)
@ -107,47 +109,3 @@ def restart_job(job_id: int):
thread.start() thread.start()
return jsonify(f'Job "{job.title}" marked for restarting.'), 202 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,14 +20,6 @@ class JobInput(FileMixin, HashidMixin, db.Model):
def __repr__(self): def __repr__(self):
return f'<JobInput {self.filename}>' 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 @property
def jsonpatch_path(self): def jsonpatch_path(self):
return f'{self.job.jsonpatch_path}/inputs/{self.hashid}' return f'{self.job.jsonpatch_path}/inputs/{self.hashid}'
@ -40,7 +32,7 @@ class JobInput(FileMixin, HashidMixin, db.Model):
def url(self): def url(self):
return url_for( return url_for(
'jobs.job', 'jobs.job',
job_id=self.job_id, job_input_id=self.id,
_anchor=f'job-{self.job.hashid}-input-{self.hashid}' _anchor=f'job-{self.job.hashid}-input-{self.hashid}'
) )

View File

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

View File

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