From 85ced0cc20ae97294a03d1db9c322e6666b08901 Mon Sep 17 00:00:00 2001 From: viveksantayana Date: Sat, 11 Jun 2022 11:29:15 +0100 Subject: [PATCH] Progress --- ref-test/app/__init__.py | 10 ++----- ref-test/app/bootstrap.py | 3 -- ref-test/app/common/views/privacy.py | 5 ---- ref-test/app/config.py | 19 ++++++------- ref-test/app/csrf.py | 2 -- ref-test/app/data.py | 5 ++++ ref-test/app/database.py | 2 -- ref-test/app/install.py | 31 +++++++++++++++++++-- ref-test/app/login_manager.py | 2 -- ref-test/app/mail.py | 2 -- ref-test/app/modules.py | 10 +++++++ ref-test/app/test.py | 10 ------- ref-test/app/tools/data.py | 11 ++++++++ ref-test/app/tools/logs.py | 11 ++++++++ ref-test/app/{common => }/views/__init__.py | 2 +- ref-test/app/views/privacy.py | 5 ++++ ref-test/main.py | 6 ++-- ref-test/test/subfolder/__init__.py | 8 ------ ref-test/test/subfolder/deeper/__init__.py | 5 ---- ref-test/test/subfolder/deeper/foo.py | 5 ---- ref-test/test/test.py | 5 ---- 21 files changed, 88 insertions(+), 71 deletions(-) delete mode 100644 ref-test/app/bootstrap.py delete mode 100644 ref-test/app/common/views/privacy.py delete mode 100644 ref-test/app/csrf.py create mode 100644 ref-test/app/data.py delete mode 100644 ref-test/app/database.py delete mode 100644 ref-test/app/login_manager.py delete mode 100644 ref-test/app/mail.py create mode 100644 ref-test/app/modules.py delete mode 100644 ref-test/app/test.py create mode 100644 ref-test/app/tools/data.py create mode 100644 ref-test/app/tools/logs.py rename ref-test/app/{common => }/views/__init__.py (88%) create mode 100644 ref-test/app/views/privacy.py delete mode 100644 ref-test/test/subfolder/__init__.py delete mode 100644 ref-test/test/subfolder/deeper/__init__.py delete mode 100644 ref-test/test/subfolder/deeper/foo.py delete mode 100644 ref-test/test/test.py diff --git a/ref-test/app/__init__.py b/ref-test/app/__init__.py index 2555c94..94f38df 100644 --- a/ref-test/app/__init__.py +++ b/ref-test/app/__init__.py @@ -1,9 +1,5 @@ -from .bootstrap import bootstrap +from .modules import bootstrap, csrf, db, login_manager, mail 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_wtf.csrf import CSRFError @@ -14,7 +10,7 @@ from datetime import datetime from .admin.views import admin from .api.views import api -from .common.views import common +from .views import views from .quiz.views import quiz def create_app(): @@ -46,7 +42,7 @@ def create_app(): app.register_blueprint(admin, url_prefix='/admin') app.register_blueprint(api, url_prefix='/api') - app.register_blueprint(common) + app.register_blueprint(views) app.register_blueprint(quiz) return app diff --git a/ref-test/app/bootstrap.py b/ref-test/app/bootstrap.py deleted file mode 100644 index 8f28dc2..0000000 --- a/ref-test/app/bootstrap.py +++ /dev/null @@ -1,3 +0,0 @@ -from ensurepip import bootstrap -from flask_bootstrap import Bootstrap -bootstrap = Bootstrap() \ No newline at end of file diff --git a/ref-test/app/common/views/privacy.py b/ref-test/app/common/views/privacy.py deleted file mode 100644 index 8295b8b..0000000 --- a/ref-test/app/common/views/privacy.py +++ /dev/null @@ -1,5 +0,0 @@ -from . import common - -@common.route('/privacy/') -def _privacy(): - return 'Privacy Policy' \ No newline at end of file diff --git a/ref-test/app/config.py b/ref-test/app/config.py index 09c57b8..26e214f 100644 --- a/ref-test/app/config.py +++ b/ref-test/app/config.py @@ -4,26 +4,25 @@ load_dotenv() class Config(object): APP_HOST = '0.0.0.0' + DATA_FILE_DIRECTORY = os.getenv('DATA_FILE_DIRECTORY') DEBUG = False TESTING = False SECRET_KEY = os.getenv('SECRET_KEY') - SERVER_NAME = os.getenv('SERVER_NAME') SESSION_COOKIE_SECURE = True SQLALCHEMY_DATABASE_URI = 'sqlite:///data/database.db' SQLALCHEMY_TRACK_MODIFICATIONS = False - MAIL_SERVER = os.getenv("MAIL_SERVER") - MAIL_PORT = int(os.getenv("MAIL_PORT")) + MAIL_SERVER = os.getenv('MAIL_SERVER') + MAIL_PORT = int(os.getenv('MAIL_PORT')) MAIL_USE_TLS = False MAIL_USE_SSL = False MAIL_DEBUG = False - MAIL_USERNAME = os.getenv("MAIL_USERNAME") - MAIL_PASSWORD = os.getenv("MAIL_PASSWORD") - MAIL_DEFAULT_SENDER = os.getenv("MAIL_DEFAULT_SENDER") - MAIL_MAX_EMAILS = int(os.getenv("MAIL_MAX_EMAILS")) + MAIL_USERNAME = os.getenv('MAIL_USERNAME') + MAIL_PASSWORD = os.getenv('MAIL_PASSWORD') + MAIL_DEFAULT_SENDER = os.getenv('MAIL_DEFAULT_SENDER') + MAIL_MAX_EMAILS = int(os.getenv('MAIL_MAX_EMAILS')) MAIL_SUPPRESS_SEND = False - MAIL_ASCII_ATTACHMENTS = bool(os.getenv("MAIL_ASCII_ATTACHMENTS")) - DATA_FILE_DIRECTORY = os.getenv("DATA_FILE_DIRECTORY") + MAIL_ASCII_ATTACHMENTS = bool(os.getenv('MAIL_ASCII_ATTACHMENTS')) class ProductionConfig(Config): pass @@ -39,6 +38,6 @@ class DevelopmentConfig(Config): class TestingConfig(DevelopmentConfig): TESTING = True SESSION_COOKIE_SECURE = False - MAIL_SERVER = os.getenv("MAIL_SERVER") + MAIL_SERVER = os.getenv('MAIL_SERVER') MAIL_DEBUG = True MAIL_SUPPRESS_SEND = False \ No newline at end of file diff --git a/ref-test/app/csrf.py b/ref-test/app/csrf.py deleted file mode 100644 index edda995..0000000 --- a/ref-test/app/csrf.py +++ /dev/null @@ -1,2 +0,0 @@ -from flask_wtf.csrf import CSRFProtect -csrf = CSRFProtect() \ No newline at end of file diff --git a/ref-test/app/data.py b/ref-test/app/data.py new file mode 100644 index 0000000..16b25a7 --- /dev/null +++ b/ref-test/app/data.py @@ -0,0 +1,5 @@ +from . import Config +from os import path +from pathlib import Path + +data = Path(Config.DATA_FILE_DIRECTORY) diff --git a/ref-test/app/database.py b/ref-test/app/database.py deleted file mode 100644 index 589c64f..0000000 --- a/ref-test/app/database.py +++ /dev/null @@ -1,2 +0,0 @@ -from flask_sqlalchemy import SQLAlchemy -db = SQLAlchemy() \ No newline at end of file diff --git a/ref-test/app/install.py b/ref-test/app/install.py index ab5f113..2e0e51a 100644 --- a/ref-test/app/install.py +++ b/ref-test/app/install.py @@ -1,2 +1,29 @@ -# if not database_exists(Config.SQLALCHEMY_DATABASE_URI): create_database(Config.SQLALCHEMY_DATABASE_URI) -# with app.app_context(): db.create_all() \ No newline at end of file +from main import app +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) \ No newline at end of file diff --git a/ref-test/app/login_manager.py b/ref-test/app/login_manager.py deleted file mode 100644 index 4a9813d..0000000 --- a/ref-test/app/login_manager.py +++ /dev/null @@ -1,2 +0,0 @@ -from flask_login import LoginManager -login_manager = LoginManager() \ No newline at end of file diff --git a/ref-test/app/mail.py b/ref-test/app/mail.py deleted file mode 100644 index 6636ec6..0000000 --- a/ref-test/app/mail.py +++ /dev/null @@ -1,2 +0,0 @@ -from flask_mail import Mail -mail = Mail() \ No newline at end of file diff --git a/ref-test/app/modules.py b/ref-test/app/modules.py new file mode 100644 index 0000000..873c3d1 --- /dev/null +++ b/ref-test/app/modules.py @@ -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() \ No newline at end of file diff --git a/ref-test/app/test.py b/ref-test/app/test.py deleted file mode 100644 index c6e5283..0000000 --- a/ref-test/app/test.py +++ /dev/null @@ -1,10 +0,0 @@ -from flask import Blueprint - -foo = Blueprint( - name='foo', - import_name=__name__ -) - -@foo.route('/') -def testing(): - return 'Testing' \ No newline at end of file diff --git a/ref-test/app/tools/data.py b/ref-test/app/tools/data.py new file mode 100644 index 0000000..46550dc --- /dev/null +++ b/ref-test/app/tools/data.py @@ -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) \ No newline at end of file diff --git a/ref-test/app/tools/logs.py b/ref-test/app/tools/logs.py new file mode 100644 index 0000000..e4e24c1 --- /dev/null +++ b/ref-test/app/tools/logs.py @@ -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') \ No newline at end of file diff --git a/ref-test/app/common/views/__init__.py b/ref-test/app/views/__init__.py similarity index 88% rename from ref-test/app/common/views/__init__.py rename to ref-test/app/views/__init__.py index 4449aef..e645226 100644 --- a/ref-test/app/common/views/__init__.py +++ b/ref-test/app/views/__init__.py @@ -1,6 +1,6 @@ from flask import Blueprint -common = Blueprint( +views = Blueprint( name='common', import_name=__name__, template_folder='templates', diff --git a/ref-test/app/views/privacy.py b/ref-test/app/views/privacy.py new file mode 100644 index 0000000..6e33bf5 --- /dev/null +++ b/ref-test/app/views/privacy.py @@ -0,0 +1,5 @@ +from . import views + +@views.route('/privacy/') +def _privacy(): + return 'Privacy Policy' \ No newline at end of file diff --git a/ref-test/main.py b/ref-test/main.py index cb27368..3042cee 100644 --- a/ref-test/main.py +++ b/ref-test/main.py @@ -1,5 +1,7 @@ #!/bin/env python -from distutils.log import debug from app import create_app app = create_app() -if __name__ == '__main__': app.run() \ No newline at end of file +if __name__ == '__main__': + from app.install import install_scripts + install_scripts() + app.run() \ No newline at end of file diff --git a/ref-test/test/subfolder/__init__.py b/ref-test/test/subfolder/__init__.py deleted file mode 100644 index 96bcaac..0000000 --- a/ref-test/test/subfolder/__init__.py +++ /dev/null @@ -1,8 +0,0 @@ -from flask import Flask -from .deeper import t - -def create_app(): - app = Flask(__name__) - app.register_blueprint(t) - - return app \ No newline at end of file diff --git a/ref-test/test/subfolder/deeper/__init__.py b/ref-test/test/subfolder/deeper/__init__.py deleted file mode 100644 index bd98253..0000000 --- a/ref-test/test/subfolder/deeper/__init__.py +++ /dev/null @@ -1,5 +0,0 @@ -from flask import Blueprint - -t = Blueprint(name='test', import_name=__name__) - -from . import foo \ No newline at end of file diff --git a/ref-test/test/subfolder/deeper/foo.py b/ref-test/test/subfolder/deeper/foo.py deleted file mode 100644 index bb985bf..0000000 --- a/ref-test/test/subfolder/deeper/foo.py +++ /dev/null @@ -1,5 +0,0 @@ -from . import t - -@t.route('/') -def _t(): - return 'Test' \ No newline at end of file diff --git a/ref-test/test/test.py b/ref-test/test/test.py deleted file mode 100644 index b4edcbd..0000000 --- a/ref-test/test/test.py +++ /dev/null @@ -1,5 +0,0 @@ -from subfolder import create_app - -app = create_app() -app.run() -