This commit is contained in:
Vivek Santayana 2022-06-11 11:29:15 +01:00
parent eac6cac7bc
commit 9539ba22fe
21 changed files with 88 additions and 71 deletions

View File

@ -1,9 +1,5 @@
from .bootstrap import bootstrap from .modules import bootstrap, csrf, db, login_manager, mail
from .config import DevelopmentConfig as Config from .config import DevelopmentConfig as Config
from .csrf import csrf
from .database import db
from .login_manager import login_manager
from .mail import mail
from flask import Flask from flask import Flask
from flask_wtf.csrf import CSRFError from flask_wtf.csrf import CSRFError
@ -14,7 +10,7 @@ from datetime import datetime
from .admin.views import admin from .admin.views import admin
from .api.views import api from .api.views import api
from .common.views import common from .views import views
from .quiz.views import quiz from .quiz.views import quiz
def create_app(): def create_app():
@ -46,7 +42,7 @@ def create_app():
app.register_blueprint(admin, url_prefix='/admin') app.register_blueprint(admin, url_prefix='/admin')
app.register_blueprint(api, url_prefix='/api') app.register_blueprint(api, url_prefix='/api')
app.register_blueprint(common) app.register_blueprint(views)
app.register_blueprint(quiz) app.register_blueprint(quiz)
return app return app

View File

@ -1,3 +0,0 @@
from ensurepip import bootstrap
from flask_bootstrap import Bootstrap
bootstrap = Bootstrap()

View File

@ -1,5 +0,0 @@
from . import common
@common.route('/privacy/')
def _privacy():
return 'Privacy Policy'

View File

@ -4,26 +4,25 @@ load_dotenv()
class Config(object): class Config(object):
APP_HOST = '0.0.0.0' APP_HOST = '0.0.0.0'
DATA_FILE_DIRECTORY = os.getenv('DATA_FILE_DIRECTORY')
DEBUG = False DEBUG = False
TESTING = False TESTING = False
SECRET_KEY = os.getenv('SECRET_KEY') SECRET_KEY = os.getenv('SECRET_KEY')
SERVER_NAME = os.getenv('SERVER_NAME')
SESSION_COOKIE_SECURE = True SESSION_COOKIE_SECURE = True
SQLALCHEMY_DATABASE_URI = 'sqlite:///data/database.db' SQLALCHEMY_DATABASE_URI = 'sqlite:///data/database.db'
SQLALCHEMY_TRACK_MODIFICATIONS = False SQLALCHEMY_TRACK_MODIFICATIONS = False
MAIL_SERVER = os.getenv("MAIL_SERVER") MAIL_SERVER = os.getenv('MAIL_SERVER')
MAIL_PORT = int(os.getenv("MAIL_PORT")) MAIL_PORT = int(os.getenv('MAIL_PORT'))
MAIL_USE_TLS = False MAIL_USE_TLS = False
MAIL_USE_SSL = False MAIL_USE_SSL = False
MAIL_DEBUG = False MAIL_DEBUG = False
MAIL_USERNAME = os.getenv("MAIL_USERNAME") MAIL_USERNAME = os.getenv('MAIL_USERNAME')
MAIL_PASSWORD = os.getenv("MAIL_PASSWORD") MAIL_PASSWORD = os.getenv('MAIL_PASSWORD')
MAIL_DEFAULT_SENDER = os.getenv("MAIL_DEFAULT_SENDER") MAIL_DEFAULT_SENDER = os.getenv('MAIL_DEFAULT_SENDER')
MAIL_MAX_EMAILS = int(os.getenv("MAIL_MAX_EMAILS")) MAIL_MAX_EMAILS = int(os.getenv('MAIL_MAX_EMAILS'))
MAIL_SUPPRESS_SEND = False MAIL_SUPPRESS_SEND = False
MAIL_ASCII_ATTACHMENTS = bool(os.getenv("MAIL_ASCII_ATTACHMENTS")) MAIL_ASCII_ATTACHMENTS = bool(os.getenv('MAIL_ASCII_ATTACHMENTS'))
DATA_FILE_DIRECTORY = os.getenv("DATA_FILE_DIRECTORY")
class ProductionConfig(Config): class ProductionConfig(Config):
pass pass
@ -39,6 +38,6 @@ class DevelopmentConfig(Config):
class TestingConfig(DevelopmentConfig): class TestingConfig(DevelopmentConfig):
TESTING = True TESTING = True
SESSION_COOKIE_SECURE = False SESSION_COOKIE_SECURE = False
MAIL_SERVER = os.getenv("MAIL_SERVER") MAIL_SERVER = os.getenv('MAIL_SERVER')
MAIL_DEBUG = True MAIL_DEBUG = True
MAIL_SUPPRESS_SEND = False MAIL_SUPPRESS_SEND = False

View File

@ -1,2 +0,0 @@
from flask_wtf.csrf import CSRFProtect
csrf = CSRFProtect()

5
ref-test/app/data.py Normal file
View File

@ -0,0 +1,5 @@
from . import Config
from os import path
from pathlib import Path
data = Path(Config.DATA_FILE_DIRECTORY)

View File

@ -1,2 +0,0 @@
from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy()

View File

@ -1,2 +1,29 @@
# if not database_exists(Config.SQLALCHEMY_DATABASE_URI): create_database(Config.SQLALCHEMY_DATABASE_URI) from main import app
# with app.app_context(): db.create_all() from . import Config
from .data import data
from .modules import db
from .tools.data import save
from .tools.logs import write
from os import mkdir, path, system
from cryptography.fernet import Fernet
from sqlalchemy_utils import database_exists, create_database
def install_scripts():
if not path.isdir(f'./{data}'): mkdir(f'./{data}')
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('commands.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)

View File

@ -1,2 +0,0 @@
from flask_login import LoginManager
login_manager = LoginManager()

View File

@ -1,2 +0,0 @@
from flask_mail import Mail
mail = Mail()

10
ref-test/app/modules.py Normal file
View File

@ -0,0 +1,10 @@
from flask_bootstrap import Bootstrap
bootstrap = Bootstrap()
from flask_wtf.csrf import CSRFProtect
csrf = CSRFProtect()
from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy()
from flask_login import LoginManager
login_manager = LoginManager()
from flask_mail import Mail
mail = Mail()

View File

@ -1,10 +0,0 @@
from flask import Blueprint
foo = Blueprint(
name='foo',
import_name=__name__
)
@foo.route('/')
def testing():
return 'Testing'

View File

@ -0,0 +1,11 @@
from .. import Config
from ..data import data_dir
import json
def load(filename:str):
with open(f'./{data_dir}/{filename}') as file:
return json.load(file)
def save(data:dict, filename:str):
with open(f'./{data_dir}/{filename}', 'w') as file:
json.dump(data, file, indent=4)

View File

@ -0,0 +1,11 @@
from .. import Config
from ..data import data
from datetime import datetime
def read(filename:str):
with open(f'./{data}/logs/{filename}') as file:
return file.readlines()
def write(filename:str, message:str):
with open(f'./{data}/logs/{filename}', 'a+') as file:
file.write(f'{datetime.now().strftime("%Y-%m-%d-%X")}: {message}\n')

View File

@ -1,6 +1,6 @@
from flask import Blueprint from flask import Blueprint
common = Blueprint( views = Blueprint(
name='common', name='common',
import_name=__name__, import_name=__name__,
template_folder='templates', template_folder='templates',

View File

@ -0,0 +1,5 @@
from . import views
@views.route('/privacy/')
def _privacy():
return 'Privacy Policy'

View File

@ -1,5 +1,7 @@
#!/bin/env python #!/bin/env python
from distutils.log import debug
from app import create_app from app import create_app
app = create_app() app = create_app()
if __name__ == '__main__': app.run() if __name__ == '__main__':
from app.install import install_scripts
install_scripts()
app.run()

View File

@ -1,8 +0,0 @@
from flask import Flask
from .deeper import t
def create_app():
app = Flask(__name__)
app.register_blueprint(t)
return app

View File

@ -1,5 +0,0 @@
from flask import Blueprint
t = Blueprint(name='test', import_name=__name__)
from . import foo

View File

@ -1,5 +0,0 @@
from . import t
@t.route('/')
def _t():
return 'Test'

View File

@ -1,5 +0,0 @@
from subfolder import create_app
app = create_app()
app.run()