""" This python script conatins functions that talk with Flask frontend over socketio. It has functions to join a unique room, creating an account and certifying an account. """ # run.py # standard imports import os import time # third-party imports from flask import session from flask_socketio import SocketIO, join_room from gevent import monkey # local imports # pylint: disable=wrong-import-order import tasks import app_vars # pylint: enable=wrong-import-order from app import create_app monkey.patch_all(subprocess=True) config_name = os.getenv("FLASK_CONFIG") app = create_app(config_name) app.config["SECRET_KEY"] = app_vars.key socketio = SocketIO( app, cors_allowed_origins=app_vars.cors_allowed_origins, message_queue=app_vars.message_queue, ) @socketio.on("join_room") def on_room(json): """ This function creates a unique room/flask session id, and joins it Input: json: conatins config information for the flask session Output: Join the unique room. """ room = str(session["uid"]) referrer = json["referrer"] join_room(room) print("\t\t\t|-----Room ID: " + room) print("\t\t\t|-----Referrer: " + referrer) @socketio.on("request account") def request_account(json): """ This function is called by the Flask frontend on an account request. Input: json: This contains information needed for the user that needs to be created from the frontend. methods: Defaults to ["GET", "POST"]. Output: Send the json to Celery tasks file for account creation. """ print( time.strftime("%m-%d-%Y_%H:%M:%S") + "\tQueue request received: " + str(json) ) room = str(session["uid"]) print(f"Room: {room}") try: tasks.celery_create_account.delay(json, session=room) except Exception as e: print( time.strftime("%m-%d-%Y_%H:%M:%S") + "\tError in account creation: ", e, ) socketio.emit("Account creation failed", room) @socketio.on("request certification") def certify_account(json): """ This function is called by the Flask frontend from self certification page. Inputs: json: Conatins information about the user that needs to be certified from the frontend. methods: Defaults to ["GET", "POST"]. Outputs: Send the json to Celery tasks file for user certification. """ print( time.strftime("%m-%d-%Y_%H:%M:%S") + "\tQueue request received: " + str(json) ) room = str(session["uid"]) print(f"CERTIFY Room: {room}") try: tasks.celery_certify_account(json, session=room) except Exception as e: print( time.strftime("%m-%d-%Y_%H:%M:%S") + "\tError in account certification: ", e, ) socketio.emit("Account certification failed", room) if __name__ == "__main__": socketio.run(app, host="0.0.0.0")