Addition to last commit.

This commit is contained in:
Patrick Jentsch 2019-08-16 09:49:47 +02:00
parent 4a5804460f
commit 22f6c6b4b6

View File

@ -1,76 +1,103 @@
from flask import abort, jsonify, request from flask import abort, jsonify, make_response, request
from flask_login import current_user, login_required from flask_login import current_user, login_required
from . import api from . import api
from ..models import Job from ..decorators import admin_required
from ..models import Corpus, Job, User
@api.route('/v1.0/admin/corpora')
@login_required
@admin_required
def admin_corpora():
user_id = request.args.get('user_id', default=str(current_user.id))
if user_id == '*':
corpora = Corpus.query.all()
else:
user = User.query.filter_by(id=user_id).first()
if user:
corpora = user.corpora.all()
else:
return abort(404)
jsonifyable_corpora = []
for corpus in corpora:
jsonifyable_corpora.append(corpus.to_jsonifyable())
return jsonify(jsonifyable_corpora)
@api.route('/v1.0/admin/corpora/<int:corpus_id>')
@login_required
@admin_required
def admin_coprus(corpus_id):
corpus = Corpus.query.filter_by(id=corpus_id).first()
if corpus:
return jsonify(corpus.to_jsonifyable())
else:
return abort(404)
@api.route('/v1.0/admin/jobs')
@login_required
@admin_required
def admin_jobs():
user_id = request.args.get('user_id', default=str(current_user.id))
if user_id == '*':
jobs = Job.query.all()
else:
user = User.query.filter_by(id=user_id).first()
if user:
jobs = user.jobs.all()
else:
return abort(404)
jsonifyable_jobs = []
for job in jobs:
jsonifyable_jobs.append(job.to_jsonifyable())
return jsonify(jsonifyable_jobs)
@api.route('/v1.0/admin/jobs/<int:job_id>')
@login_required
@admin_required
def admin_job(job_id):
job = Job.query.filter_by(id=job_id).first()
if job:
return jsonify(job.to_jsonifyable())
else:
return abort(404)
@api.route('/v1.0/corpora') @api.route('/v1.0/corpora')
@login_required @login_required
def corpora(): def corpora():
corpora = [] jsonifyable_corpora = []
for corpus in current_user.corpora.all(): for corpus in current_user.corpora.all():
corpora.append({'id': corpus.id, jsonifyable_corpora.append(corpus.to_jsonifyable())
'creation_date': corpus.creation_date.timestamp(), return jsonify(jsonifyable_corpora)
'description': corpus.description,
'title': corpus.title})
return jsonify(corpora)
@api.route('/v1.0/corpora/<int:corpus_id>') @api.route('/v1.0/corpora/<int:corpus_id>')
@login_required @login_required
def corpus(corpus_id): def corpus(corpus_id):
corpus = current_user.corpora.filter_by(id=corpus_id).first() corpus = current_user.corpora.filter_by(id=corpus_id).first()
if not corpus: if corpus:
return jsonify(corpus.to_jsonifyable())
else:
return abort(404) return abort(404)
return jsonify({'id': corpus.id,
'creation_date': corpus.creation_date,
'description': corpus.description,
'title': corpus.title})
@api.route('/v1.0/jobs') @api.route('/v1.0/jobs')
@login_required @login_required
def jobs(): def jobs():
jobs = [] jsonifyable_jobs = []
all = request.args.get('all') for job in current_user.jobs.all():
if all and all.lower() == 'true': jsonifyable_jobs.append(job.to_jsonifyable())
if current_user.is_administrator(): return jsonify(jsonifyable_jobs)
jobs_query = Job.query
else:
return abort(403)
else:
jobs_query = current_user.jobs
for job in jobs_query.all():
jobs.append({'id': job.id,
'creation_date': job.creation_date.timestamp(),
'description': job.description,
'end_date': (job.end_date.timestamp() if job.end_date else
None),
'mem_mb': job.mem_mb,
'n_cores': job.n_cores,
'service': job.service,
'service_args': job.service_args,
'service_version': job.service_version,
'status': job.status,
'title': job.title})
return jsonify(jobs)
@api.route('/v1.0/jobs/<int:job_id>') @api.route('/v1.0/jobs/<int:job_id>')
@login_required @login_required
def job(job_id): def job(job_id):
job = current_user.jobs.filter_by(id=job_id).first() job = current_user.jobs.filter_by(id=job_id).first()
if not job: if job:
return jsonify(job.to_jsonifyable())
else:
return abort(404) return abort(404)
return jsonify({'id': job.id,
'creation_date': job.creation_date.timestamp(),
'description': job.description,
'end_date': (job.end_date.timestamp() if job.end_date else
None),
'mem_mb': job.mem_mb,
'n_cores': job.n_cores,
'service': job.service,
'service_args': job.service_args,
'service_version': job.service_version,
'status': job.status,
'title': job.title})