Newer
Older
Mitchell Moore
committed
from __future__ import print_function
Mitchell Moore
committed
import uuid
# third-party imports
from flask import Flask, render_template, request, session
Mitchell Moore
committed
from flask_bootstrap import Bootstrap
from flask_cors import CORS
# local imports
import app_vars
import messages
sys.path.append(app_vars.rabbitmq_agents_loc)
# pylint: disable=wrong-import-order,wrong-import-position
import rc_util # noqa: E402
# pylint: enable=wrong-import-order,wrong-import-position
Mitchell Moore
committed
def create_app(config_name):
"""
Create main flask app
input:
config_name: environment of the app running
output:
Flask instance
"""
app = Flask(
__name__, static_folder="static"
) # initialization of the flask app
CORS(app, resources={r"/*": {"origins": app_vars.cors_allowed_origins}})
Bootstrap(app) # allowing app to use bootstrap
Mitchell Moore
committed
def get_authorized_user():
user = {
"username": request.headers.get("REMOTE_USER"),
"fullname": request.headers.get("Displayname"),
"email": request.headers.get("Mail"),
"eppa": request.headers.get("Unscoped-Affiliation"),
@app.route(
"/", methods=["GET", "POST"]
) # initial route to display the reg page
Mitchell Moore
committed
def index():
if "uid" not in session:
session["uid"] = str(uuid.uuid4())
Mitchell Moore
committed
session["user"] = get_authorized_user()
session["return_url"] = request.args.get(
"redir", app_vars.default_referrer
)
if "eppa" in request.headers:
if not any(
item in session["user"].get("eppa") for item in valid_eppa
):
return render_template(
"account/unauthorized.html",
unauthorized_msg=messages.unauthorized_message,
)
if rc_util.check_state(session["user"].get("username")) == "hold":
return render_template(
"account/hold.html",
account_hold_msg=messages.account_hold_message,
)
if (
rc_util.check_state(session["user"].get("username"))
== "certification"
or rc_util.check_state(session["user"].get("username"))
== "pre_certification"
):
return render_template(
"account/certify.html",
room_id=session["uid"],
username=session["user"].get("username"),
fullname=session["user"].get("fullname"),
email=session["user"].get("email"),
referrer=session["return_url"],
cancel_url=app_vars.default_referrer,
cancel_msg=messages.cancel_message,
pre_certification_msg=messages.pre_certification_message,
certification_msg=messages.certification_message,
)
if rc_util.check_state(session["user"].get("username")) == "ok":
return render_template(
"account/good_standing.html",
good_standing_msg=messages.good_standing_message,
docs_url=app_vars.docs_url,
)
return render_template(
"auth/SignUp.html",
room_id=session["uid"],
username=session["user"].get("username"),
fullname=session["user"].get("fullname"),
email=session["user"].get("email"),
referrer=session["return_url"],
cancel_url=app_vars.default_referrer,
welcome_msg=messages.welcome_message,
cancel_msg=messages.cancel_message,
error_msg=messages.error_message,
)
Mitchell Moore
committed
# misc page error catching
@app.errorhandler(403)
def forbidden(error):
return render_template("errors/403.html", title="Forbidden"), 403
@app.errorhandler(404)
def page_not_found(error):
return render_template("errors/404.html", title="Page Not Found"), 404
@app.errorhandler(500)
def internal_server_error(error):
return render_template("errors/500.html", title="Server Error"), 500
return app