From d362820c3c47752e6cbc6bb38471698d474baa32 Mon Sep 17 00:00:00 2001 From: Mo8it Date: Mon, 8 Aug 2022 22:51:52 +0200 Subject: [PATCH] Move scripts --- advlabdb/__init__.py | 15 +-- advlabdb/config.py | 8 +- advlabdb/database_import.py | 53 ++++----- advlabdb/model_dependent_funs.py | 7 +- advlabdb/scripts/__init__.py | 0 advlabdb/scripts/maintain/__init__.py | 0 .../scripts/maintain/copy_admin_templates.py | 55 ---------- .../scripts/maintain/reset_admin_password.py | 55 ---------- advlabdb/scripts/setup/__init__.py | 0 advlabdb/scripts/setup/generate_secrets.py | 4 - advlabdb/scripts/setup/init_database.py | 85 --------------- advlabdb/scripts/terminal_utils.py | 103 ------------------ advlabdb/scripts/test/__init__.py | 0 .../setup/logged_server_setup.py | 0 .../scripts => scripts}/test/test_database.py | 0 15 files changed, 37 insertions(+), 348 deletions(-) delete mode 100644 advlabdb/scripts/__init__.py delete mode 100644 advlabdb/scripts/maintain/__init__.py delete mode 100644 advlabdb/scripts/maintain/copy_admin_templates.py delete mode 100644 advlabdb/scripts/maintain/reset_admin_password.py delete mode 100644 advlabdb/scripts/setup/__init__.py delete mode 100644 advlabdb/scripts/setup/generate_secrets.py delete mode 100644 advlabdb/scripts/setup/init_database.py delete mode 100644 advlabdb/scripts/terminal_utils.py delete mode 100644 advlabdb/scripts/test/__init__.py rename {advlabdb/scripts => scripts}/setup/logged_server_setup.py (100%) rename {advlabdb/scripts => scripts}/test/test_database.py (100%) diff --git a/advlabdb/__init__.py b/advlabdb/__init__.py index 1b7605b..7838cd0 100644 --- a/advlabdb/__init__.py +++ b/advlabdb/__init__.py @@ -1,15 +1,18 @@ +from os import environ + from flask import Flask from flask_admin import Admin from flask_security import Security, SQLAlchemyUserDatastore from flask_security.models import fsqla_v2 as fsqla from flask_sqlalchemy import SQLAlchemy from flask_migrate import Migrate + from .config import set_config app = Flask(__name__) -set_config(app) - +# Config +settings = set_config(app) # Setup Flask-SQLAlchemy db = SQLAlchemy(app) @@ -41,10 +44,4 @@ from . import models user_datastore = SQLAlchemyUserDatastore(db, models.User, models.Role) Security(app, user_datastore) -try: - from . import routes, adminModelViews, assistantModelViews -except Exception as ex: - print( - "\nYou are probably initializing the database with a script. If not, then you have to worry about not being able to import in __init__.py!\n" - ) - print(str(ex)) +from . import routes, adminModelViews, assistantModelViews diff --git a/advlabdb/config.py b/advlabdb/config.py index 7d75d66..493968d 100644 --- a/advlabdb/config.py +++ b/advlabdb/config.py @@ -3,7 +3,7 @@ from configparser import ConfigParser from pathlib import Path -def load_config(*files): +def load_config(app, *files): config = ConfigParser() for file in files: @@ -19,7 +19,7 @@ def load_config(*files): def set_config(app): - config = load_config("secrets.ini", "settings.ini") + config = load_config(app, "secrets.ini", "settings.ini") secrets = config["Secrets"] settings = config["Settings"] @@ -53,5 +53,7 @@ def set_config(app): "check_deliverability": check_email_deliverability, } app.config["SECURITY_PASSWORD_SALT"] = secrets["SECURITY_PASSWORD_SALT"] - app.config["SECURITY_PASSWORD_LENGTH_MIN"] = settings.getint(SECURITY_PASSWORD_LENGTH_MIN, 15) + app.config["SECURITY_PASSWORD_LENGTH_MIN"] = settings.getint("SECURITY_PASSWORD_LENGTH_MIN", 15) # TODO: app.config["SECURITY_LOGIN_USER_TEMPLATE"] = + + return settings diff --git a/advlabdb/database_import.py b/advlabdb/database_import.py index 64390e0..aed03e5 100644 --- a/advlabdb/database_import.py +++ b/advlabdb/database_import.py @@ -1,12 +1,11 @@ from datetime import datetime -from os import environ from pathlib import Path from shutil import copy2 from flask import flash, has_request_context from sqlalchemy import select -from . import db +from . import db, settings from .exceptions import DataBaseImportException from .model_independent_funs import get_first from .models import ( @@ -24,12 +23,6 @@ from .models import ( User, ) -relative_db_dir = Path(environ["RELATIVE_DB_DIR"]) -relative_db_path = relative_db_dir / "advlab.db" - -relative_db_bk_dir = relative_db_dir / "backups" -relative_db_bk_dir.mkdir(exist_ok=True) - def now(): return datetime.now().strftime("%d_%m_%Y_%H_%M_%S") @@ -54,16 +47,16 @@ def not_nullable(entry): def importFromFile(filePath): + db_path = Path(settings["SQLITE_DB_PATH"]) + + db_bk_dir = db_path.parent / "backups" + db_bk_dir.mkdir(exist_ok=True) + if filePath[-4:] != ".txt": raise DataBaseImportException( "The import file has to be a text file with txt extension (.txt at the end of the filename)!" ) - if has_request_context(): - show = flash - else: - show = print - semesters = {} parts = {} students = {} @@ -74,7 +67,7 @@ def importFromFile(filePath): appointments = {} with open(filePath) as f: - show("Reading file...") + flash("Reading file...") expectingTable = True readHeader = False @@ -139,7 +132,7 @@ def importFromFile(filePath): try: # Semester - show("Semester...") + flash("Semester...") if len(semesters["label"]) != 1: raise DataBaseImportException("Only one semester is allowed in an import file!") @@ -154,7 +147,7 @@ def importFromFile(filePath): ) # Part - show("Part...") + flash("Part...") dbParts = {} for i, id in enumerate(parts["id"]): @@ -179,7 +172,7 @@ def importFromFile(filePath): dbParts[id] = dbPart # Student - show("Student...") + flash("Student...") dbStudents = {} for i, studentNumber in enumerate(students["student_number"]): @@ -207,7 +200,7 @@ def importFromFile(filePath): dbStudents[studentNumber] = dbStudent # Group - show("Group...") + flash("Group...") dbGroups = {} for i, id in enumerate(groups["id"]): @@ -222,7 +215,7 @@ def importFromFile(filePath): dbGroups[id] = dbGroup # PartStudent - show("PartStudent...") + flash("PartStudent...") for i, studentNumber in enumerate(partStudents["student_number"]): studentNumber = int(not_nullable(studentNumber)) @@ -234,7 +227,7 @@ def importFromFile(filePath): db.session.add(dbPartStudent) # Experiment - show("Experiment...") + flash("Experiment...") dbSemesterExperiments = {} for i, id in enumerate(experiments["id"]): @@ -268,7 +261,7 @@ def importFromFile(filePath): dbSemesterExperiments[id] = dbSemesterExperiment # GroupExperiment - show("GroupExperiment...") + flash("GroupExperiment...") dbGroupExperiments = {} for i, id in enumerate(groupExperiments["id"]): @@ -281,7 +274,7 @@ def importFromFile(filePath): dbGroupExperiments[id] = dbGroupExperiment # Appointment - show("Appointment...") + flash("Appointment...") for i, date in enumerate(appointments["date"]): date = not_nullable(date) @@ -302,10 +295,10 @@ def importFromFile(filePath): db.session.add(dbAppointment) # Backup - dest = relative_db_bk_dir / f"before_{dbSemester}_import_{now()}.db" - copy2(relative_db_path, dest) + dest = db_bk_dir / f"before_{dbSemester}_import_{now()}.db" + copy2(db_path, dest) - show(f"Made a backup of the database before committing the import at {dest}") + flash(f"Made a backup of the database before committing the import at {dest}") db.session.commit() except Exception as ex: @@ -313,10 +306,10 @@ def importFromFile(filePath): raise ex - dest = relative_db_bk_dir / f"after_{dbSemester}_import_{now()}.db" - copy2(relative_db_path, dest) + dest = db_bk_dir / f"after_{dbSemester}_import_{now()}.db" + copy2(db_path, dest) - show(f"Made a backup of the database after the import at {dest}") + flash(f"Made a backup of the database after the import at {dest}") - show("\nImport done!") - show("Please check that everything worked as expected. Restore the database backup otherwise!") + flash("\nImport done!") + flash("Please check that everything worked as expected. Restore the database backup otherwise!") diff --git a/advlabdb/model_dependent_funs.py b/advlabdb/model_dependent_funs.py index 47e65d7..cf84142 100644 --- a/advlabdb/model_dependent_funs.py +++ b/advlabdb/model_dependent_funs.py @@ -3,7 +3,6 @@ Functions dependent on advlabdb.models. """ from functools import cache -from os import environ from flask import flash from flask_admin.menu import MenuLink @@ -11,7 +10,7 @@ from flask_security import current_user from wtforms.fields import BooleanField, IntegerField, SelectField, StringField from wtforms.validators import DataRequired, NumberRange, Optional -from . import app +from . import app, settings from .models import MAX_MARK, MIN_MARK, Semester @@ -24,7 +23,7 @@ def initActiveSemesterMenuLinks(space): MenuLink( name=str(semester), # Using SERVER_NAME because of missing request context - url=environ["SERVER_NAME"] + "/set_semester" + "?semester_id=" + str(semester.id), + url=settings["SERVER_NAME"] + "/set_semester" + "?semester_id=" + str(semester.id), category="Active semester", ) ) @@ -33,7 +32,7 @@ def initActiveSemesterMenuLinks(space): "ERROR: The Semester table does not exist yet! Therefore, menu links could not be generated. You can ignore this error if you are just initializing the database." ) else: - space.add_link(MenuLink(name="Logout", url=environ["SERVER_NAME"] + "/logout")) + space.add_link(MenuLink(name="Logout", url=settings["SERVER_NAME"] + "/logout")) def active_semester_str(): diff --git a/advlabdb/scripts/__init__.py b/advlabdb/scripts/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/advlabdb/scripts/maintain/__init__.py b/advlabdb/scripts/maintain/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/advlabdb/scripts/maintain/copy_admin_templates.py b/advlabdb/scripts/maintain/copy_admin_templates.py deleted file mode 100644 index 670fead..0000000 --- a/advlabdb/scripts/maintain/copy_admin_templates.py +++ /dev/null @@ -1,55 +0,0 @@ -from os.path import exists -from shutil import copytree, rmtree - -from flask_admin import __file__ as flaskAdminPath - - -def copyAdminTemplates(): - src = flaskAdminPath.removesuffix("__init__.py") + "templates/bootstrap4/admin" - if not exists(src): - print("Templates could not be found in", src) - print("You can also copy them manually.") - return False - - dist = "advlabdb/templates/admin" - if exists(dist): - while True: - ans = input( - f"The directory {dist} already exists. Enter 'o' to override it and update the templates or enter 's' to stop the process: " - ).lower() - if ans == "s": - print("Process stopped!") - return False - elif ans == "o": - break - rmtree(dist) - print("Old templates deleted!") - - copytree(src, dist) - print("Copied", src, "->", dist) - - print( - f""" - _________ - | WARNING - | ------- - | - | You might have to edit the file {dist}/base.html by adding nav in the following way: - | This line:\t