From ea399d2cdc6dd1e75105d8054a1e364f517a4674 Mon Sep 17 00:00:00 2001 From: Patrick Jentsch Date: Wed, 14 Aug 2019 16:59:15 +0200 Subject: [PATCH] Add api endpoints for testing. --- app/main/views.py | 68 +++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 63 insertions(+), 5 deletions(-) diff --git a/app/main/views.py b/app/main/views.py index 89a40f78..18585dc9 100644 --- a/app/main/views.py +++ b/app/main/views.py @@ -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 ..models import Corpus, User, Job from ..tables import AdminUserTable, AdminUserItem @@ -6,9 +6,47 @@ from . import main from .forms import CreateCorpusForm from ..decorators import admin_required from .. import db +import json 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/') +@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('/') def index(): return render_template('main/index.html.j2', title='Opaque') @@ -26,6 +64,29 @@ def for_admins_only(): table=table) +@main.route('/corpora/') +@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']) @login_required def dashboard(): @@ -64,13 +125,10 @@ def dashboard(): @main.route('/jobs/') @login_required 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: print('Job not found.') 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'], str(current_user.id),