84 lines
2.8 KiB
Python
84 lines
2.8 KiB
Python
from datetime import datetime
|
|
|
|
from flask import Flask, flash, request, render_template
|
|
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 werkzeug.middleware.proxy_fix import ProxyFix
|
|
|
|
from common.security import check_keyfile_exists, generate_keyfile
|
|
import config
|
|
|
|
def create_app():
|
|
app = Flask(__name__)
|
|
app.config.from_object(config.TestingConfig())
|
|
|
|
from common.blueprints 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
|
|
|
|
app.register_blueprint(quiz_views, 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'<strong>Cookie Consent</strong>: This web site only stores minimal, functional cookies. By using this site, you consent to this use of cookies. For more information, see our <a href="{url_for("quiz_views.privacy")}">privacy policy</a>.', '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.errorhandler(404)
|
|
def _404_handler(e):
|
|
return render_template('/quiz/404.html'), 404
|
|
|
|
@app.errorhandler(CSRFError)
|
|
def csrf_error_handler(error):
|
|
return jsonify({ 'error': 'Could not validate a secure connection.'} ), 400
|
|
|
|
if not check_keyfile_exists():
|
|
generate_keyfile()
|
|
|
|
Bootstrap(app)
|
|
csrf = CSRFProtect(app)
|
|
|
|
app.wsgi_app = ProxyFix(app.wsgi_app, x_proto=1, x_host=1)
|
|
|
|
return app
|
|
|
|
app = create_app()
|
|
mongo = MongoClient(app.config['MONGO_URI'])
|
|
db = mongo[app.config['MONGO_INITDB_DATABASE']]
|
|
mail = Mail(app)
|
|
|
|
if __name__ == '__main__':
|
|
app.run(host=app.config['APP_HOST']) |