Add api endpoints for testing.

This commit is contained in:
Patrick Jentsch 2019-08-14 16:59:15 +02:00
parent b0c2f69568
commit ea399d2cdc

View File

@ -1,4 +1,4 @@
from flask import abort, current_app, flash, redirect, request, render_template, url_for, send_from_directory from flask import abort, current_app, flash, jsonify, redirect, request, render_template, url_for, send_from_directory
from flask_login import current_user, login_required from flask_login import current_user, login_required
from ..models import Corpus, User, Job from ..models import Corpus, User, Job
from ..tables import AdminUserTable, AdminUserItem from ..tables import AdminUserTable, AdminUserItem
@ -6,9 +6,47 @@ from . import main
from .forms import CreateCorpusForm from .forms import CreateCorpusForm
from ..decorators import admin_required from ..decorators import admin_required
from .. import db from .. import db
import json
import os import os
@main.route('/api/v1.0/corpora')
@login_required
def api_corpora():
corpora = []
for corpus in current_user.corpora.all():
corpora.append({'id': corpus.id,
'creation_date': corpus.creation_date.timestamp(),
'description': corpus.description,
'title': corpus.title})
return jsonify(corpora)
@main.route('/api/v1.0/corpora/<int:corpus_id>')
@login_required
def api_corpus():
corpus = []
@main.route('/api/v1.0/jobs')
@login_required
def api_jobs():
jobs = []
for job in current_user.jobs.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)
@main.route('/') @main.route('/')
def index(): def index():
return render_template('main/index.html.j2', title='Opaque') return render_template('main/index.html.j2', title='Opaque')
@ -26,6 +64,29 @@ def for_admins_only():
table=table) table=table)
@main.route('/corpora/<int:corpus_id>')
@login_required
def corpus(corpus_id):
corpus = current_user.corpora.filter_by(id=corpus_id).first()
if not corpus:
print('Corpus not found.')
abort(404)
dir = os.path.join(current_app.config['OPAQUE_STORAGE'],
str(current_user.id),
'corpora',
str(corpus.id))
files = {}
for file in os.listdir(dir):
files[file] = {}
files[file]['path'] = os.path.join(file)
return render_template('main/corpora/corpus.html.j2',
files=files,
corpus=corpus,
title='Corpus')
@main.route('/dashboard', methods=['GET', 'POST']) @main.route('/dashboard', methods=['GET', 'POST'])
@login_required @login_required
def dashboard(): def dashboard():
@ -64,13 +125,10 @@ def dashboard():
@main.route('/jobs/<int:job_id>') @main.route('/jobs/<int:job_id>')
@login_required @login_required
def job(job_id): def job(job_id):
job = Job.query.filter_by(id=job_id).first() job = current_user.jobs.filter_by(id=job_id).first()
if not job: if not job:
print('Job not found.') print('Job not found.')
abort(404) abort(404)
elif not job.user_id == current_user.id:
print('Job does not belong to current user.')
abort(403)
dir = os.path.join(current_app.config['OPAQUE_STORAGE'], dir = os.path.join(current_app.config['OPAQUE_STORAGE'],
str(current_user.id), str(current_user.id),