Newer
Older
"""
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.
"""
Mitchell Moore
committed
import time
Mitchell Moore
committed
Mitchell Moore
committed
from flask import session
from flask_socketio import SocketIO, join_room
Ravi Tripathi
committed
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
Ravi Tripathi
committed
monkey.patch_all(subprocess=True)
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,
)
Mitchell Moore
committed
Mitchell Moore
committed
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"]
Mitchell Moore
committed
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}")
Mitchell Moore
committed
try:
tasks.celery_create_account.delay(json, session=room)
Mitchell Moore
committed
except Exception as e:
print(
time.strftime("%m-%d-%Y_%H:%M:%S")
+ "\tError in account creation: ",
e,
)
Mitchell Moore
committed
socketio.emit("Account creation failed", room)
Mitchell Moore
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}")
tasks.celery_certify_account(json, session=room)
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")