__init__.py 3.47 KB
Newer Older
Mitchell Moore's avatar
Mitchell Moore committed
1
# app/__init__.py
2

3
4
# local imports
from __future__ import print_function
5
import vars
6
import messages
7
# third-party imports
8
import uuid
9
from flask import Flask, redirect, url_for, request, render_template, flash, session, send_from_directory
10
from flask_cors import CORS
11
from flask_bootstrap import Bootstrap
12
import random
13
14
import os
import json
Krish Moodbidri's avatar
Krish Moodbidri committed
15
import sys
Krish Moodbidri's avatar
Krish Moodbidri committed
16
import re
Krish Moodbidri's avatar
Krish Moodbidri committed
17
18
19

sys.path.append(vars.rabbitmq_agents_loc)
import rc_util
20

21
def create_app(config_name):
22
    app = Flask(__name__, static_folder='static') # initialization of the flask app
23
    cors = CORS(app, resources={r"/*": {"origins": vars.cors_allowed_origins}})
24
    Bootstrap(app) # allowing app to use bootstrap
25

26
27
28
    def get_authorized_user():

        user = {
29
30
31
32
            "username": re.search("([^!]+?)(@uab\.edu)?$", request.headers.get("Persistent-Id")).group(1),
            "fullname": f'{request.headers.get("Givenname")} {request.headers.get("Sn")}',
            "email": request.headers.get("Mail"),
            "eppa": request.headers.get("Unscoped-Affiliation"),
33
34
35
36
        }

        return user

37
38
    @app.route('/', methods=['GET', 'POST']) # initial route to display the reg page
    def index():
39

40
        valid_eppa = vars.valid_eppa 
Krish Moodbidri's avatar
Krish Moodbidri committed
41

42
43
44
        if 'uid' not in session:
            session['uid']=str(uuid.uuid4())

45
46
47
        if 'user' not in session:
            session["user"] = get_authorized_user()

48
        session['return_url'] = request.args.get('redir', vars.default_referrer)
49

50
        if (not any(item in session['user'].get('eppa') for item in valid_eppa)):
51
            return render_template('account/unauthorized.html', unauthorized_msg=messages.unauthorized_message)
52
53

        if rc_util.check_state(session['user'].get('username')) == "blocked":
54
            return render_template('account/blocked.html', unauthorized_msg=messages.unauthorized_message)
55

56
        elif rc_util.check_state(session['user'].get('username')) == "certification":
57
              return render_template('account/certify.html', room_id=session['uid'],
58
59
60
                               username=session['user'].get('username'),
                               fullname=session['user'].get('fullname'), email=session['user'].get('email'),
                               referrer=session['return_url'], cancel_url=vars.default_referrer,
61
62
63
64
                               cancel_msg=messages.cancel_message,
                               pre_certification_msg=messages.pre_certification_message,
                               certification_msg=messages.certification_message)

Krish Moodbidri's avatar
Krish Moodbidri committed
65
66
        else:
            return render_template('auth/SignUp.html', room_id=session['uid'],
67
68
                               username=session['user'].get('username'),
                               fullname=session['user'].get('fullname'), email=session['user'].get('email'),
69
70
71
                               referrer=session['return_url'], cancel_url=vars.default_referrer,
                               welcome_msg=messages.welcome_message,
                               cancel_msg=messages.cancel_message,
Krish Moodbidri's avatar
Krish Moodbidri committed
72
                               error_msg=messages.error_message)
73

74
    # misc page error catching
75
76
77
78
79
80
81
82
83
84
85
86
87
    @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