2022-06-16 15:19:26 +01:00
from app . data import data
from app . models import Entry , Dataset , Test , User
2022-06-11 18:08:24 +01:00
from app . modules import bootstrap , csrf , db , login_manager , mail
2022-06-16 15:19:26 +01:00
from app . tools . data import save
from app . tools . logs import write
2022-06-12 20:50:57 +01:00
from config import Config
2022-06-11 18:08:24 +01:00
2022-06-16 15:19:26 +01:00
from flask import flash , Flask , render_template , request
2022-06-16 13:22:06 +01:00
from flask . helpers import url_for
2022-06-11 18:08:24 +01:00
from flask . json import jsonify
2022-06-16 13:22:06 +01:00
from flask_wtf . csrf import CSRFError
2022-06-16 15:19:26 +01:00
from sqlalchemy_utils import database_exists , create_database
2022-06-11 18:08:24 +01:00
from werkzeug . middleware . proxy_fix import ProxyFix
2022-06-16 15:19:26 +01:00
from cryptography . fernet import Fernet
2022-06-11 18:08:24 +01:00
from datetime import datetime
2022-06-16 15:19:26 +01:00
from os import mkdir , path
2022-06-11 18:08:24 +01:00
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
2022-06-15 23:54:44 +01:00
def _load_user ( id ) :
return User . query . filter_by ( id = id ) . first ( )
2022-06-11 18:08:24 +01:00
2022-06-16 13:22:06 +01:00
@app.before_request
def _check_cookie_consent ( ) :
if request . cookies . get ( ' cookie_consent ' ) :
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. 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 ' )
2022-06-11 18:08:24 +01:00
@app.errorhandler ( 404 )
2022-06-18 02:18:07 +01:00
def _404_handler ( error ) : return render_template ( ' 404.html ' )
2022-06-11 18:08:24 +01:00
@app.errorhandler ( CSRFError )
2022-06-18 02:18:07 +01:00
def _csrf_handler ( ) : return jsonify ( { ' error ' : ' Could not validate a secure connection. ' } ) , 403
2022-06-11 18:08:24 +01:00
@app.context_processor
2022-06-18 02:18:07 +01:00
def _now ( ) : return { ' now ' : datetime . now ( ) }
2022-06-11 18:08:24 +01:00
2022-06-12 20:51:36 +01:00
from app . admin . views import admin
from app . api . views import api
from app . quiz . views import quiz
2022-06-16 13:22:06 +01:00
from app . views import views
2022-06-12 20:51:36 +01:00
2022-06-11 18:08:24 +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-16 15:19:26 +01:00
if not path . isdir ( f ' ./ { data } ' ) : mkdir ( f ' ./ { data } ' )
if not path . isdir ( f ' ./ { data } /questions ' ) : mkdir ( f ' ./ { data } /questions ' )
if not path . isfile ( f ' ./ { data } /.gitignore ' ) :
with open ( f ' ./ { data } /.gitignore ' , ' a+ ' ) as file : file . write ( f ' * ' )
if not path . isfile ( f ' ./ { data } /config.json ' ) : save ( { } , ' config.json ' )
if not path . isdir ( f ' ./ { data } /logs ' ) : mkdir ( f ' ./ { data } /logs ' )
if not path . isfile ( f ' ./ { data } /logs/users.log ' ) : write ( ' users.log ' , ' Log file created. ' )
if not path . isfile ( f ' ./ { data } /logs/system.log ' ) : write ( ' system.log ' , ' Log file created. ' )
if not path . isfile ( f ' ./ { data } /logs/tests.log ' ) : write ( ' tests.log ' , ' Log file created. ' )
if not database_exists ( Config . SQLALCHEMY_DATABASE_URI ) :
create_database ( Config . SQLALCHEMY_DATABASE_URI )
write ( ' system.log ' , ' No database found. Creating a new database. ' )
with app . app_context ( ) : db . create_all ( )
write ( ' system.log ' , ' Creating database schema. ' )
if not path . isfile ( f ' ./ { data } /.encryption.key ' ) :
write ( ' system.log ' , ' No encryption key found. Generating new encryption key. ' )
with open ( f ' ./ { data } /.encryption.key ' , ' wb ' ) as key_file :
key = Fernet . generate_key ( )
key_file . write ( key )
2022-06-11 18:08:24 +01:00
return app
2021-12-05 03:49:31 +00:00
app = create_app ( )
2022-06-11 18:08:24 +01:00
2022-06-11 11:29:15 +01:00
if __name__ == ' __main__ ' :
app . run ( )