2021-11-23 13:00:03 +00:00
from datetime import datetime
2021-12-04 17:40:01 +00:00
from flask import Flask , flash , request , render_template
2021-11-23 13:00:03 +00:00
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
2021-11-28 02:30:46 +00:00
from common . security import check_keyfile_exists , generate_keyfile
2021-11-23 13:00:03 +00:00
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 ( )
2021-11-28 02:30:46 +00:00
from common . blueprints import cookie_consent
2021-11-24 17:17:56 +00:00
from admin . views import views as admin_views
2021-11-23 13:00:03 +00:00
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 )
2021-12-04 17:40:01 +00:00
@app.errorhandler ( 404 )
def _404_handler ( e ) :
return render_template ( ' /quiz/404.html ' ) , 404
2021-11-23 13:00:03 +00:00
app . run ( host = app . config [ ' APP_HOST ' ] )