from datetime import datetime from flask import Flask, flash, request from flask.helpers import url_for from flask.json import jsonify from flask_bootstrap import Bootstrap from pymongo import MongoClient from pymongo.errors import ConnectionFailure from flask_wtf.csrf import CSRFProtect, CSRFError from flask_mail import Mail from security import check_keyfile_exists, generate_keyfile app = Flask(__name__) app.config.from_object('config.DevelopmentConfig') Bootstrap(app) csrf = CSRFProtect(app) @app.errorhandler(CSRFError) def csrf_error_handler(error): return jsonify({ 'error': 'Could not validate a secure connection.'} ), 400 try: mongo = MongoClient(app.config['MONGO_URI']) db = mongo[app.config['MONGO_INITDB_DATABASE']] except ConnectionFailure as error: print(error) try: mail = Mail(app) except Exception as error: print(error) if __name__ == '__main__': if not check_keyfile_exists(): generate_keyfile() from common import cookie_consent from admin.views import views as admin_views from admin.auth import auth as admin_auth from admin.results import results from quiz.views import views as quiz_views from quiz.auth import auth as quiz_auth app.register_blueprint(quiz_views, url_prefix = '/') app.register_blueprint(quiz_auth, url_prefix = '/') app.register_blueprint(admin_views, url_prefix = '/admin/') app.register_blueprint(admin_auth, url_prefix = '/admin/') app.register_blueprint(results, url_prefix = '/admin/results/') app.register_blueprint(cookie_consent, url_prefix = '/cookies/') @app.before_request def check_cookie_consent(): if request.cookies.get('cookie_consent') == 'True': return if any([ request.path.startswith(x) for x in [ '/admin/static/', '/static/', '/cookies/' ] ]): return flash(f'Cookie Consent: This web site only stores minimal, functional cookies. By using this site, you consent to this use of cookies. For more information, see our privacy policy.', 'cookie_alert') from admin.views import check_login, get_user_from_db, get_id_from_cookie @app.context_processor def inject_now(): return {'now': datetime.utcnow()} @app.context_processor def _check_login(): return dict(check_login = check_login) @app.context_processor def _get_user_from_db(): return dict(get_user_from_db = get_user_from_db) @app.context_processor def _get_id_from_cookie(): return dict(get_id_from_cookie = get_id_from_cookie) app.run(host=app.config['APP_HOST'])