Coverage for app/__init__.py: 73%
77 statements
« prev ^ index » next coverage.py v7.10.2, created at 2026-09-04 14:23 +0000
« prev ^ index » next coverage.py v7.10.2, created at 2026-09-04 14:23 +0000
1import os
2import pprint
4from flask import Flask, render_template
5from playhouse.shortcuts import model_to_dict, dict_to_model
6from app.logic.config import load_config_files
8# Initialize our application
9app = Flask(__name__, template_folder="templates")
10app.jinja_env.add_extension('jinja2.ext.loopcontrols')
12app.env = os.environ.get('APP_ENV', 'production')
13load_config_files(app)
14print (" * Environment:", app.env)
15# set the secret key after configuration is set up
16app.secret_key = app.config['secret_key']
18# These imports must happen after configuration
19from app.models.term import Term
20from app.models.user import User
22from peewee import BaseQuery
23from flask import session
24if app.config['show_queries']:
25 old_execute = BaseQuery.execute
26 def new_execute(*args, **kwargs):
27 if session:
28 if 'querycount' not in session:
29 session['querycount'] = 0
31 session['querycount'] += 1
32 print("**Running query {}**".format(session['querycount']))
33 print(args[0])
34 return old_execute(*args, **kwargs)
35 BaseQuery.execute = new_execute
38######### Blueprints #############
39from app.controllers.admin import admin_bp as admin_bp
40app.register_blueprint(admin_bp)
42from app.controllers.events import events_bp as events_bp
43app.register_blueprint(events_bp)
45from app.controllers.main import main_bp as main_bp
46app.register_blueprint(main_bp)
48from app.controllers.serviceLearning import serviceLearning_bp as serviceLearning_bp
49app.register_blueprint(serviceLearning_bp)
51from app.controllers.minor import minor_bp as minor_bp
52app.register_blueprint(minor_bp)
53##################################
55# Make 'ENV' a variable everywhere
56@app.context_processor
57def inject_environment():
58 return dict( env=app.env )
60@app.before_request
61def queryCount():
62 if session:
63 session['querycount'] = 0
65from app.logic.loginManager import getLoginUser
66from flask import g
67@app.before_request
68def load_user():
69 # An exception handles both current_user not being set and a mismatch between models
70 try:
71 g.current_user = dict_to_model(User,session['current_user'])
72 except Exception as e:
73 user = getLoginUser()
74 session['current_user'] = model_to_dict(user)
75 g.current_user = user
77from app.logic.loginManager import getCurrentTerm
78@app.before_request
79def load_currentTerm():
80 # Query the current term from the database on each request to avoid stale session data
81 g.current_term = getCurrentTerm()
83import datetime
84@app.before_request
85def load_currentDateTime():
86 g.currentDateTime = datetime.datetime.now()
88from flask import request
89@app.context_processor
90def load_visibleAccordion():
91 acc = request.args.get("accordion", default = False)
92 return {"visibleAccordion": acc}
93"""
94Error handling for all 403, 404, 500 errors. Works by rendering a custom html
95file located at templates/errors. All abort calls are automatically routed here
96to be handled.
97"""
99supportContactEmail = app.config["support_email_contact"]
101@app.errorhandler(403)
102def handle_bad_request(e):
103 return render_template("/errors/403error.html",
104 supportEmail = supportContactEmail), 403
106@app.errorhandler(404)
107def handle_bad_request(e):
108 return render_template("/errors/404error.html",
109 supportEmail = supportContactEmail), 404
111@app.errorhandler(500)
112def handle_bad_request(e):
113 return render_template("/errors/500error.html",
114 supportEmail = supportContactEmail), 500