Skip to content
Snippets Groups Projects
run.py 2.94 KiB
Newer Older
Bo-Chun Chen's avatar
Bo-Chun Chen committed
"""
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.
"""
Bo-Chun Chen's avatar
Bo-Chun Chen committed
# standard imports
Bo-Chun Chen's avatar
Bo-Chun Chen committed
# third-party imports
from flask import session
from flask_socketio import SocketIO, join_room
Bo-Chun Chen's avatar
Bo-Chun Chen committed

# local imports
# pylint: disable=wrong-import-order
import tasks
import app_vars

# pylint: enable=wrong-import-order
from app import create_app

Bo-Chun Chen's avatar
Bo-Chun Chen committed
config_name = os.getenv("FLASK_CONFIG")
app = create_app(config_name)
Bo-Chun Chen's avatar
Bo-Chun Chen committed
app.config["SECRET_KEY"] = app_vars.key
socketio = SocketIO(
    app,
    cors_allowed_origins=app_vars.cors_allowed_origins,
    message_queue=app_vars.message_queue,
)

Bo-Chun Chen's avatar
Bo-Chun Chen committed
@socketio.on("join_room")
Bo-Chun Chen's avatar
Bo-Chun Chen committed
    """
    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"]
Bo-Chun Chen's avatar
Bo-Chun Chen committed
    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}")
Bo-Chun Chen's avatar
Bo-Chun Chen committed
        tasks.celery_create_account.delay(json, session=room)
Bo-Chun Chen's avatar
Bo-Chun Chen committed
        print(
            time.strftime("%m-%d-%Y_%H:%M:%S")
            + "\tError in account creation: ",
            e,
        )
        socketio.emit("Account creation failed", room)
Bo-Chun Chen's avatar
Bo-Chun Chen committed

@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}")
Bo-Chun Chen's avatar
Bo-Chun Chen committed
        tasks.celery_certify_account(json, session=room)
    except Exception as e:
Bo-Chun Chen's avatar
Bo-Chun Chen committed
        print(
            time.strftime("%m-%d-%Y_%H:%M:%S")
            + "\tError in account certification: ",
            e,
        )
        socketio.emit("Account certification failed", room)

Bo-Chun Chen's avatar
Bo-Chun Chen committed

if __name__ == "__main__":
    socketio.run(app, host="0.0.0.0")