Coverage for app/__init__.py: 68%
82 statements
« prev ^ index » next coverage.py v7.10.2, created at 2025-12-22 18:34 +0000
« prev ^ index » next coverage.py v7.10.2, created at 2025-12-22 18:34 +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 # An exception handles both current_term not being set and a mismatch between models
81 try:
82 g.current_term = dict_to_model(Term, session['current_term'])
83 except Exception as e:
84 term = getCurrentTerm()
85 session['current_term'] = model_to_dict(term)
86 g.current_term = term
88import datetime
89@app.before_request
90def load_currentDateTime():
91 g.currentDateTime = datetime.datetime.now()
93from flask import request
94@app.context_processor
95def load_visibleAccordion():
96 acc = request.args.get("accordion", default = False)
97 return {"visibleAccordion": acc}
98"""
99Error handling for all 403, 404, 500 errors. Works by rendering a custom html
100file located at templates/errors. All abort calls are automatically routed here
101to be handled.
102"""
104supportContactEmail = app.config["support_email_contact"]
106@app.errorhandler(403)
107def handle_bad_request(e):
108 return render_template("/errors/403error.html",
109 supportEmail = supportContactEmail), 403
111@app.errorhandler(404)
112def handle_bad_request(e):
113 return render_template("/errors/404error.html",
114 supportEmail = supportContactEmail), 404
116@app.errorhandler(500)
117def handle_bad_request(e):
118 return render_template("/errors/500error.html",
119 supportEmail = supportContactEmail), 500