-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapp.py
101 lines (74 loc) · 3.43 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import importlib
import os
import logging
from backend.auth import shib
from backend.db import Administrator, Organizations, Organizer, Profile, RoleType, commit, db, db_testing_setup, Event, make_admin
from configs import *
from flask import Flask, Blueprint, request
def create_app(config_file: Config = DevelopmentConfig) -> Flask:
logging.basicConfig(level=logging.INFO, filename="development.log", filemode="a")
logging.getLogger("sqlalchemy.engine").setLevel(logging.DEBUG)
app = Flask(__name__, static_folder="static/", template_folder="templates/")
app.config.from_object(config_file)
app.jinja_env.cache = None
db.init_app(app)
shib.init_app(app)
blueprint_paths = os.listdir("backend/routes")
blueprints = []
for blueprint_path in blueprint_paths:
if blueprint_path.startswith("__") or blueprint_path.startswith("."):
continue
module = importlib.import_module(f"backend.routes.{blueprint_path[:-3]}")
for _, value in module.__dict__.items():
if isinstance(value, Blueprint):
blueprints.append(value)
for blueprint in blueprints:
app.register_blueprint(blueprint)
logging.info(f"Added '{blueprint.name}' blueprint.")
app.jinja_env.add_extension("jinja2.ext.loopcontrols")
if config_file:
with app.app_context():
db.create_all()
try:
Organizations.COMS = db.session.query(Organizer.organization_id).where(Organizer.name == "Computing Organization for Multicultural Students").first()[0]
Organizations.WIC = db.session.query(Organizer.organization_id).where(Organizer.name == "Women in Computing").first()[0]
except TypeError:
WiC = Organizer(
name = "Women in Computing",
email = "[email protected]"
)
COMS = Organizer(
name = "Computing Organization for Multicultural Students",
email = "[email protected]"
)
commit(WiC, COMS)
Organizations.WIC = WiC.organization_id
Organizations.COMS = COMS.organization_id
defacto_admin = db.session.query(Administrator.id).join(Profile).where(Profile.rit_id == app.config['DEFACTO_ADMIN']['rit_id']).first()
if defacto_admin is None:
profile = db.session.query(Profile.rit_id).where(Profile.rit_id == app.config['DEFACTO_ADMIN']['rit_id']).first()
if profile is None:
profile = Profile(**app.config['DEFACTO_ADMIN'])
commit(profile)
print("Making admin")
make_admin(profile.profile_id, Organizations.COMS, RoleType.ADMIN)
make_admin(profile.profile_id, Organizations.WIC, RoleType.ADMIN)
commit()
return app
def database_setup(app):
with app.app_context():
db.create_all()
if Event.query.first() is None:
db_testing_setup()
db.session.commit()
if __name__ == "__main__":
app = create_app(DevelopmentConfig)
### REMOVE FROM PRODUCTION ###
@app.before_request
def before_request():
request.environ["uid"] = "wls1234"
request.environ["givenName"] = "Will"
request.environ["sn"] = "Smith"
request.environ["email"] = "[email protected]"
database_setup(app)
app.run()