2022-06-22 02:01:34 +01:00
from . config import Production as Config
2022-06-20 12:15:28 +01:00
from . models import User
2022-06-20 12:10:37 +01:00
from . extensions import bootstrap , csrf , db , login_manager , mail
2022-08-20 10:56:43 +01:00
from . tools . logs import write
2022-06-20 12:10:37 +01:00
from flask import flash , Flask , render_template , request
2022-08-20 10:56:43 +01:00
from flask . helpers import abort , url_for
2022-06-20 12:10:37 +01:00
from flask . json import jsonify
from flask_wtf . csrf import CSRFError
2022-08-20 10:56:43 +01:00
from sqlalchemy . exc import SQLAlchemyError
2022-06-20 12:10:37 +01:00
from werkzeug . middleware . proxy_fix import ProxyFix
from datetime import datetime
def create_app ( ) :
app = Flask ( __name__ )
app . config . from_object ( Config ( ) )
app . wsgi_app = ProxyFix ( app . wsgi_app , x_proto = 1 , x_host = 1 )
bootstrap . init_app ( app )
csrf . init_app ( app )
db . init_app ( app )
login_manager . init_app ( app )
mail . init_app ( app )
login_manager . login_view = ' admin._login '
@login_manager.user_loader
def _load_user ( id ) :
2022-08-20 10:56:43 +01:00
try : return User . query . filter_by ( id = id ) . first ( )
2022-08-20 14:47:46 +01:00
except Exception as exception :
2022-08-20 10:56:43 +01:00
write ( ' system.log ' , f ' Database error when loading user fo login manager: { exception } ' )
return abort ( 500 )
2022-06-20 12:10:37 +01:00
@app.before_request
def _check_cookie_consent ( ) :
if request . cookies . get ( ' cookie_consent ' ) :
return
2022-08-19 13:29:29 +01:00
if any ( [ request . path . startswith ( x ) for x in [ ' /admin/static/ ' , ' /root/ ' , ' /quiz/static ' , ' /cookies/ ' , ' /admin/editor/static ' , ' /admin/view/static ' ] ] ) :
2022-06-20 12:10:37 +01:00
return
flash ( f ' <strong>Cookie Consent</strong>: This web site only stores minimal, functional cookies. It does not store any tracking information. By using this site, you consent to this use of cookies. For more information, see our <a href= " { url_for ( " views._privacy " ) } " >privacy policy</a>. ' , ' cookie_alert ' )
@app.errorhandler ( 404 )
def _404_handler ( error ) : return render_template ( ' 404.html ' )
@app.errorhandler ( CSRFError )
def _csrf_handler ( ) : return jsonify ( { ' error ' : ' Could not validate a secure connection. ' } ) , 403
@app.context_processor
def _now ( ) : return { ' now ' : datetime . now ( ) }
from . admin . views import admin
from . api . views import api
from . quiz . views import quiz
from . views import views
2022-06-20 12:22:29 +01:00
from . editor . views import editor
2022-08-17 16:32:58 +01:00
from . view . views import view
2022-06-20 12:10:37 +01:00
app . register_blueprint ( admin , url_prefix = ' /admin ' )
app . register_blueprint ( api , url_prefix = ' /api ' )
app . register_blueprint ( views )
app . register_blueprint ( quiz )
2022-06-20 12:22:29 +01:00
app . register_blueprint ( editor , url_prefix = ' /admin/editor ' )
2022-08-17 16:32:58 +01:00
app . register_blueprint ( view , url_prefix = ' /admin/view ' )
2022-06-20 12:10:37 +01:00
return app