diff --git a/app/__init__.py b/app/__init__.py index 7359b4e..1bcb722 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -1,9 +1,11 @@ from flask import Flask -from .routes import main +from flask_socketio import SocketIO from .models import db from .utils import init_password_generator, init_serializer from .auth import init_auth +socketio = SocketIO() + def create_app(): app = Flask(__name__, static_folder='static', static_url_path='/static') app.config.from_object('config.Config') @@ -16,5 +18,8 @@ def create_app(): init_serializer(app) init_auth(app) + socketio.init_app(app) + + from .routes import main app.register_blueprint(main) return app diff --git a/app/routes.py b/app/routes.py index e0f7c5f..3143c5b 100644 --- a/app/routes.py +++ b/app/routes.py @@ -4,6 +4,8 @@ from datetime import datetime from typing import List, cast import flask_login +from flask_socketio import join_room + import pytz from flask import ( Blueprint, @@ -19,6 +21,8 @@ from flask import ( url_for, ) +from . import socketio + from .auth import check_password, user_loader from .forms import DeckForm, InfoBannerForm, LoginForm, PasswordForm from .models import Deck, InfoBanner, Password, db @@ -40,6 +44,10 @@ main = Blueprint('main', __name__) slidev_deck_pattern = re.compile(r"^\/slides\/[^\/]+\/\d*(?:\?.*)?$") slidev_all_pattern = re.compile(r"^\/slides\/[^\/]+(?:\/presenter)?\/\d+(?:\?.*)?$") +@socketio.on("connect") +def on_connect(): + if flask_login.current_user.is_authenticated: + join_room("instructor") @main.before_request def ensure_cookie(): @@ -56,7 +64,7 @@ def ensure_cookie(): @main.after_request def inject_confused_button(response): if True: - if (slidev_deck_pattern.match(request.path) or request.path == '/')and response.content_type.startswith('text/html'): + if (slidev_deck_pattern.match(request.path) or request.path == '/') and response.content_type.startswith('text/html') and not flask_login.current_user.is_authenticated : if response.direct_passthrough: response.direct_passthrough = False body = response.get_data(as_text=True) @@ -157,8 +165,16 @@ def access_link(access_code: str): @main.route('/confused', methods=['POST']) def confused(): - resp = jsonify(success=True) - return resp + + socketio.emit( + "confused", + { + "time": datetime.utcnow().isoformat() + }, + to="instructor" + ) + return ("", 204) + @main.route('/retrieve-token', methods=['POST']) def set_username_token(): @@ -293,3 +309,7 @@ def hide_active_banner(): db.session.commit() return redirect(url_for('main.banner')) +@main.route('/admin/confused') +@flask_login.login_required +def confused_notifications(): + return render_template("confused.html") diff --git a/app/templates/confused.html b/app/templates/confused.html new file mode 100644 index 0000000..b84f18b --- /dev/null +++ b/app/templates/confused.html @@ -0,0 +1,70 @@ + + +
+ +Waiting for students to click “I’m confused…”
+