Coverage for app/__init__.py: 68%

81 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2024-11-19 23:53 +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 

8# Initialize our application 

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

10 

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

12load_config_files(app) 

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

14# set the secret key after configuration is set up 

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

16 

17# These imports must happen after configuration 

18from app.models.term import Term 

19from app.models.user import User 

20 

21from peewee import BaseQuery 

22from flask import session 

23if app.config['show_queries']: 

24 old_execute = BaseQuery.execute 

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

26 if session: 

27 if 'querycount' not in session: 

28 session['querycount'] = 0 

29 

30 session['querycount'] += 1 

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

32 print(args[0]) 

33 return old_execute(*args, **kwargs) 

34 BaseQuery.execute = new_execute 

35 

36 

37######### Blueprints ############# 

38from app.controllers.admin import admin_bp as admin_bp 

39app.register_blueprint(admin_bp) 

40 

41from app.controllers.events import events_bp as events_bp 

42app.register_blueprint(events_bp) 

43 

44from app.controllers.main import main_bp as main_bp 

45app.register_blueprint(main_bp) 

46 

47from app.controllers.serviceLearning import serviceLearning_bp as serviceLearning_bp 

48app.register_blueprint(serviceLearning_bp) 

49 

50from app.controllers.minor import minor_bp as minor_bp 

51app.register_blueprint(minor_bp) 

52################################## 

53 

54# Make 'ENV' a variable everywhere 

55@app.context_processor 

56def inject_environment(): 

57 return dict( env=app.env ) 

58 

59@app.before_request 

60def queryCount(): 

61 if session: 

62 session['querycount'] = 0 

63 

64from app.logic.loginManager import getLoginUser 

65from flask import g 

66@app.before_request 

67def load_user(): 

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

69 try: 

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

71 except Exception as e: 

72 user = getLoginUser() 

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

74 g.current_user = user 

75 

76from app.logic.loginManager import getCurrentTerm 

77@app.before_request 

78def load_currentTerm(): 

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

80 try: 

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

82 except Exception as e: 

83 term = getCurrentTerm() 

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

85 g.current_term = term 

86 

87import datetime 

88@app.before_request 

89def load_currentDateTime(): 

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

91 

92from flask import request 

93@app.context_processor 

94def load_visibleAccordion(): 

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

96 return {"visibleAccordion": acc} 

97""" 

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

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

100to be handled. 

101""" 

102 

103supportContactEmail = app.config["support_email_contact"] 

104 

105@app.errorhandler(403) 

106def handle_bad_request(e): 

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

108 supportEmail = supportContactEmail) 

109 

110@app.errorhandler(404) 

111def handle_bad_request(e): 

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

113 supportEmail = supportContactEmail) 

114 

115@app.errorhandler(500) 

116def handle_bad_request(e): 

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

118 supportEmail = supportContactEmail)