diff --git a/app/main/__init__.py b/app/main/__init__.py index 484be0c1..356af527 100644 --- a/app/main/__init__.py +++ b/app/main/__init__.py @@ -3,7 +3,7 @@ from flask import Blueprint main = Blueprint('main', __name__) -from . import views, errors +from . import errors, events, views from ..models import Permission diff --git a/app/main/events.py b/app/main/events.py new file mode 100644 index 00000000..827598b6 --- /dev/null +++ b/app/main/events.py @@ -0,0 +1,19 @@ +from flask_login import current_user, login_required +from flask_socketio import emit +from .. import socketio +import json + + +@socketio.on('connect') +@login_required +def connect(): + corpora = [] + jobs = [] + + for corpus in current_user.corpora: + corpora.append(corpus.to_dict()) + for job in current_user.jobs: + jobs.append(job.to_dict()) + + emit('corpora', {'data': json.dumps(corpora)}) + emit('jobs', {'data': json.dumps(jobs)}) diff --git a/app/main/views.py b/app/main/views.py index 90ef5559..676b9528 100644 --- a/app/main/views.py +++ b/app/main/views.py @@ -1,12 +1,10 @@ from flask import (abort, current_app, flash, redirect, request, render_template, url_for, send_from_directory) from flask_login import current_user, login_required -from flask_socketio import emit from . import main from .forms import CreateCorpusForm -from .. import db, socketio +from .. import db from ..models import Corpus -import json import os @@ -15,27 +13,6 @@ def index(): return render_template('main/index.html.j2', title='Opaque') -@socketio.on('my event') -@login_required -def handle_message(message): - print('received message: ' + str(message)) - - -@socketio.on('connect') -@login_required -def connect(): - corpora = [] - jobs = [] - - for corpus in current_user.corpora: - corpora.append(corpus.to_dict()) - for job in current_user.jobs: - jobs.append(job.to_dict()) - - emit('corpora', {'data': json.dumps(corpora)}) - emit('jobs', {'data': json.dumps(jobs)}) - - @main.route('/corpora/') @login_required def corpus(corpus_id):