from flask import abort, jsonify, make_response, request from flask_login import current_user, login_required from . import api 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/') @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/') @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') @login_required def corpora(): jsonifyable_corpora = [] for corpus in current_user.corpora.all(): jsonifyable_corpora.append(corpus.to_jsonifyable()) return jsonify(jsonifyable_corpora) @api.route('/v1.0/corpora/') @login_required def corpus(corpus_id): corpus = current_user.corpora.filter_by(id=corpus_id).first() if corpus: return jsonify(corpus.to_jsonifyable()) else: return abort(404) @api.route('/v1.0/jobs') @login_required def jobs(): jsonifyable_jobs = [] for job in current_user.jobs.all(): jsonifyable_jobs.append(job.to_jsonifyable()) return jsonify(jsonifyable_jobs) @api.route('/v1.0/jobs/') @login_required def job(job_id): job = current_user.jobs.filter_by(id=job_id).first() if job: return jsonify(job.to_jsonifyable()) else: return abort(404)