__init__.py 3 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
15
16

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

21
    def get_authorized_user():
22
23
24
        username_key = list(filter(lambda key: (request.headers.get(key)  is not None), vars.username_key))
        fullname_key = list(filter(lambda key: (request.headers.get(key) is not None), vars.fullname_key))
        email_key = list(filter(lambda key: (request.headers.get(key) is not None), vars.email_key))
25
26

        user = {
27
28
29
            "username": (request.headers.get(username_key[0]) if len(username_key) > 0 else None),
            "fullname": (request.headers.get(fullname_key[0]) if len(fullname_key) > 0 else None),
            "email": (request.headers.get(email_key[0]) if len(email_key) > 0 else None),
30
31
32
33
        }

        return user

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

37
38
39
        if 'uid' not in session:
            session['uid']=str(uuid.uuid4())

40
41
42
        if 'user' not in session:
            session["user"] = get_authorized_user()

43
        if "redir" in request.args and 'return_url' not in session: # check for redir arg in url
44
            session['return_url'] = request.args.get("redir")
45

46
47
48
49
        elif "redir" not in request.args and 'return_url' not in session:
            session['return_url'] = vars.default_referrer
        else:
            session['return_url'] = request.referrer
50

51
52
53
        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'),
54
55
56
57
                               referrer=session['return_url'], cancel_url=vars.default_referrer,
                               welcome_msg=messages.welcome_message,
                               cancel_msg=messages.cancel_message,
                               error_msg=messages.error_message)
root's avatar
root committed
58

Krish Moodbidri's avatar
Krish Moodbidri committed
59
60
61
62
63
    @app.route('/error_account')
    def error_account_create():    
        return render_template('errors/error.html', title='account creation failed')


64
    # misc page error catching
65
66
67
68
69
70
71
72
73
74
75
76
77
    @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