nopaque/app/api/views.py

104 lines
2.6 KiB
Python
Raw Normal View History

2019-08-16 07:49:47 +00:00
from flask import abort, jsonify, make_response, request
2019-08-15 10:02:50 +00:00
from flask_login import current_user, login_required
from . import api
2019-08-16 07:49:47 +00:00
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_dict())
2019-08-16 07:49:47 +00:00
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_dict())
2019-08-16 07:49:47 +00:00
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_dict())
2019-08-16 07:49:47 +00:00
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_dict())
2019-08-16 07:49:47 +00:00
else:
return abort(404)
2019-08-15 10:02:50 +00:00
@api.route('/v1.0/corpora')
@login_required
def corpora():
2019-08-16 07:49:47 +00:00
jsonifyable_corpora = []
2019-08-15 10:02:50 +00:00
for corpus in current_user.corpora.all():
jsonifyable_corpora.append(corpus.to_dict())
2019-08-16 07:49:47 +00:00
return jsonify(jsonifyable_corpora)
2019-08-15 10:02:50 +00:00
@api.route('/v1.0/corpora/<int:corpus_id>')
@login_required
def corpus(corpus_id):
corpus = current_user.corpora.filter_by(id=corpus_id).first()
2019-08-16 07:49:47 +00:00
if corpus:
return jsonify(corpus.to_dict())
2019-08-16 07:49:47 +00:00
else:
return abort(404)
2019-08-15 10:02:50 +00:00
@api.route('/v1.0/jobs')
@login_required
def jobs():
2019-08-16 07:49:47 +00:00
jsonifyable_jobs = []
for job in current_user.jobs.all():
jsonifyable_jobs.append(job.to_dict())
2019-08-16 07:49:47 +00:00
return jsonify(jsonifyable_jobs)
2019-08-15 10:02:50 +00:00
@api.route('/v1.0/jobs/<int:job_id>')
@login_required
def job(job_id):
job = current_user.jobs.filter_by(id=job_id).first()
2019-08-16 07:49:47 +00:00
if job:
return jsonify(job.to_dict())
2019-08-16 07:49:47 +00:00
else:
return abort(404)