Coverage for app/__init__.py: 68%

81 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2025-03-12 21:45 +0000

1import os 

2import pprint 

3 

4from flask import Flask, render_template 

5from playhouse.shortcuts import model_to_dict, dict_to_model 

6from app.logic.config import load_config_files 

7# Initialize our application 

8app = Flask(__name__, template_folder="templates") 

9 

10app.env = os.environ.get('APP_ENV', 'production') 

11load_config_files(app) 

12print (" * Environment:", app.env) 

13# set the secret key after configuration is set up 

14app.secret_key = app.config['secret_key'] 

15 

16# These imports must happen after configuration 

17from app.models.term import Term 

18from app.models.user import User 

19 

20from peewee import BaseQuery 

21from flask import session 

22if app.config['show_queries']: 

23 old_execute = BaseQuery.execute 

24 def new_execute(*args, **kwargs): 

25 if session: 

26 if 'querycount' not in session: 

27 session['querycount'] = 0 

28 

29 session['querycount'] += 1 

30 print("**Running query {}**".format(session['querycount'])) 

31 print(args[0]) 

32 return old_execute(*args, **kwargs) 

33 BaseQuery.execute = new_execute 

34 

35 

36######### Blueprints ############# 

37from app.controllers.admin import admin_bp as admin_bp 

38app.register_blueprint(admin_bp) 

39 

40from app.controllers.events import events_bp as events_bp 

41app.register_blueprint(events_bp) 

42 

43from app.controllers.main import main_bp as main_bp 

44app.register_blueprint(main_bp) 

45 

46from app.controllers.serviceLearning import serviceLearning_bp as serviceLearning_bp 

47app.register_blueprint(serviceLearning_bp) 

48 

49from app.controllers.minor import minor_bp as minor_bp 

50app.register_blueprint(minor_bp) 

51################################## 

52 

53# Make 'ENV' a variable everywhere 

54@app.context_processor 

55def inject_environment(): 

56 return dict( env=app.env ) 

57 

58@app.before_request 

59def queryCount(): 

60 if session: 

61 session['querycount'] = 0 

62 

63from app.logic.loginManager import getLoginUser 

64from flask import g 

65@app.before_request 

66def load_user(): 

67 # An exception handles both current_user not being set and a mismatch between models 

68 try: 

69 g.current_user = dict_to_model(User,session['current_user']) 

70 except Exception as e: 

71 user = getLoginUser() 

72 session['current_user'] = model_to_dict(user) 

73 g.current_user = user 

74 

75from app.logic.loginManager import getCurrentTerm 

76@app.before_request 

77def load_currentTerm(): 

78 # An exception handles both current_term not being set and a mismatch between models 

79 try: 

80 g.current_term = dict_to_model(Term, session['current_term']) 

81 except Exception as e: 

82 term = getCurrentTerm() 

83 session['current_term'] = model_to_dict(term) 

84 g.current_term = term 

85 

86import datetime 

87@app.before_request 

88def load_currentDateTime(): 

89 g.currentDateTime = datetime.datetime.now() 

90 

91from flask import request 

92@app.context_processor 

93def load_visibleAccordion(): 

94 acc = request.args.get("accordion", default = False) 

95 return {"visibleAccordion": acc} 

96""" 

97Error handling for all 403, 404, 500 errors. Works by rendering a custom html 

98file located at templates/errors. All abort calls are automatically routed here 

99to be handled. 

100""" 

101 

102supportContactEmail = app.config["support_email_contact"] 

103 

104@app.errorhandler(403) 

105def handle_bad_request(e): 

106 return render_template("/errors/403error.html", 

107 supportEmail = supportContactEmail) 

108 

109@app.errorhandler(404) 

110def handle_bad_request(e): 

111 return render_template("/errors/404error.html", 

112 supportEmail = supportContactEmail) 

113 

114@app.errorhandler(500) 

115def handle_bad_request(e): 

116 return render_template("/errors/500error.html", 

117 supportEmail = supportContactEmail)