From 11fb5f5428a2280ff0ccd9bb72d1e5a3afc63a7e Mon Sep 17 00:00:00 2001 From: Mo8it Date: Sun, 8 May 2022 21:26:25 +0200 Subject: [PATCH] Use relative imports --- advlabdb/__init__.py | 8 ++++---- advlabdb/adminModelViews.py | 16 ++++++++-------- advlabdb/assistantModelViews.py | 12 ++++++------ advlabdb/customClasses.py | 6 +++--- advlabdb/database_import.py | 6 +++--- advlabdb/dependent_funs.py | 4 ++-- advlabdb/models.py | 6 +++--- advlabdb/routes.py | 4 ++-- advlabdb/scripts/__init__.py | 0 advlabdb/scripts/maintain/__init__.py | 0 advlabdb/scripts/maintain/root_update.py | 4 ++-- advlabdb/scripts/maintain/shared.py | 2 +- advlabdb/scripts/maintain/user_update.py | 6 +++--- advlabdb/scripts/setup/__init__.py | 0 .../setup/{init_db.py => init_database.py} | 8 ++++---- advlabdb/scripts/setup/logged_server_setup.py | 9 ++------- advlabdb/scripts/setup/server_setup.py | 8 +++----- advlabdb/scripts/setup/shared.py | 4 ++-- advlabdb/scripts/test/__init__.py | 0 advlabdb/scripts/test/test_database.py | 4 ++-- docs/user_docs/server_setup.adoc | 3 ++- 21 files changed, 52 insertions(+), 58 deletions(-) create mode 100644 advlabdb/scripts/__init__.py create mode 100644 advlabdb/scripts/maintain/__init__.py create mode 100644 advlabdb/scripts/setup/__init__.py rename advlabdb/scripts/setup/{init_db.py => init_database.py} (92%) create mode 100644 advlabdb/scripts/test/__init__.py diff --git a/advlabdb/__init__.py b/advlabdb/__init__.py index b95e392..382d859 100644 --- a/advlabdb/__init__.py +++ b/advlabdb/__init__.py @@ -8,7 +8,7 @@ from flask_sqlalchemy import SQLAlchemy from dotenv import load_dotenv -from advlabdb.independent_funs import parse_bool +from .independent_funs import parse_bool def set_from_env(app, var): @@ -39,7 +39,7 @@ db = SQLAlchemy(app) fsqla.FsModels.set_db_info(db) -from advlabdb import customClasses +from . import customClasses adminSpace = Admin( app, @@ -58,14 +58,14 @@ assistantSpace = Admin( index_view=customClasses.SecureAssistantIndexView(name="Assistant", url="/assistant", endpoint="assistantSpace"), ) -from advlabdb import models +from . import models # Setup Flask-Security user_datastore = SQLAlchemyUserDatastore(db, models.User, models.Role) Security(app, user_datastore) try: - from advlabdb import routes, adminModelViews, assistantModelViews + 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" diff --git a/advlabdb/adminModelViews.py b/advlabdb/adminModelViews.py index d1f52b7..6998b81 100644 --- a/advlabdb/adminModelViews.py +++ b/advlabdb/adminModelViews.py @@ -30,24 +30,24 @@ from wtforms.fields import ( from wtforms.validators import URL, DataRequired, Email, NumberRange, Optional from wtforms.widgets import NumberInput -from advlabdb import adminSpace, app, assistantSpace, db, user_datastore -from advlabdb.configUtils import getConfig -from advlabdb.customClasses import ( +from . import adminSpace, app, assistantSpace, db, user_datastore +from .configUtils import getConfig +from .customClasses import ( CustomIdEndpointLinkRowAction, SecureAdminBaseView, SecureAdminModelView, ) -from advlabdb.database_import import importFromFile -from advlabdb.dependent_funs import ( +from .database_import import importFromFile +from .dependent_funs import ( flashRandomPassword, initActiveSemesterMenuLinks, setUserActiveSemester, sortedSemestersStartingWithNewest, userActiveSemester, ) -from advlabdb.exceptions import DataBaseException, ModelViewException -from advlabdb.independent_funs import randomPassword -from advlabdb.models import ( +from .exceptions import DataBaseException, ModelViewException +from .independent_funs import randomPassword +from .models import ( MAX_MARK, MAX_YEAR, MIN_MARK, diff --git a/advlabdb/assistantModelViews.py b/advlabdb/assistantModelViews.py index 1da843d..8d24536 100644 --- a/advlabdb/assistantModelViews.py +++ b/advlabdb/assistantModelViews.py @@ -7,16 +7,16 @@ from wtforms import BooleanField, Form, RadioField, SelectField, StringField from wtforms.fields import DateField from wtforms.validators import NumberRange -from advlabdb import assistantSpace, db -from advlabdb.customClasses import SecureAssistantBaseView, SecureAssistantModelView -from advlabdb.dependent_funs import ( +from . import assistantSpace, db +from .customClasses import SecureAssistantBaseView, SecureAssistantModelView +from .dependent_funs import ( flashRandomPassword, initActiveSemesterMenuLinks, userActiveSemester, ) -from advlabdb.exceptions import DataBaseException, ModelViewException -from advlabdb.independent_funs import randomPassword -from advlabdb.models import ( +from .exceptions import DataBaseException, ModelViewException +from .independent_funs import randomPassword +from .models import ( MAX_MARK, MIN_MARK, Appointment, diff --git a/advlabdb/customClasses.py b/advlabdb/customClasses.py index 148b7ed..3f4ceb7 100644 --- a/advlabdb/customClasses.py +++ b/advlabdb/customClasses.py @@ -7,9 +7,9 @@ from flask_admin.model.template import EndpointLinkRowAction from flask_security import current_user from sqlalchemy import and_ -from advlabdb.dependent_funs import reportBadAttempt, userActiveSemester -from advlabdb.exceptions import DataBaseException, ModelViewException -from advlabdb.models import ( +from .dependent_funs import reportBadAttempt, userActiveSemester +from .exceptions import DataBaseException, ModelViewException +from .models import ( Assistant, ExperimentMark, GroupExperiment, diff --git a/advlabdb/database_import.py b/advlabdb/database_import.py index 584d7d1..02d8ed1 100644 --- a/advlabdb/database_import.py +++ b/advlabdb/database_import.py @@ -5,9 +5,9 @@ from shutil import copy2 from flask import flash, has_request_context -from advlabdb import db -from advlabdb.exceptions import DataBaseImportException -from advlabdb.models import ( +from . import db +from .exceptions import DataBaseImportException +from .models import ( Appointment, Assistant, Experiment, diff --git a/advlabdb/dependent_funs.py b/advlabdb/dependent_funs.py index 75ac4c6..605bda3 100644 --- a/advlabdb/dependent_funs.py +++ b/advlabdb/dependent_funs.py @@ -2,8 +2,8 @@ from flask import flash, url_for from flask_admin.menu import MenuLink from flask_security import current_user -from advlabdb import app, db -from advlabdb.models import Semester +from . import app, db +from .models import Semester def flashRandomPassword(password): diff --git a/advlabdb/models.py b/advlabdb/models.py index 82018a5..16c44ee 100644 --- a/advlabdb/models.py +++ b/advlabdb/models.py @@ -12,9 +12,9 @@ from flask import flash from flask_security import current_user from flask_security.models.fsqla_v2 import FsRoleMixin, FsUserMixin -from advlabdb import db -from advlabdb.configUtils import getConfig -from advlabdb.exceptions import DataBaseException +from . import db +from .configUtils import getConfig +from .exceptions import DataBaseException MIN_MARK = 0 MAX_MARK = 15 diff --git a/advlabdb/routes.py b/advlabdb/routes.py index 8291b33..7a15a4f 100644 --- a/advlabdb/routes.py +++ b/advlabdb/routes.py @@ -1,8 +1,8 @@ from flask import flash, redirect, render_template, request, url_for from flask_security import auth_required, current_user, roles_accepted, roles_required -from advlabdb import app -from advlabdb.dependent_funs import setUserActiveSemester, userActiveSemester +from . import app +from .dependent_funs import setUserActiveSemester, userActiveSemester @app.context_processor diff --git a/advlabdb/scripts/__init__.py b/advlabdb/scripts/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/advlabdb/scripts/maintain/__init__.py b/advlabdb/scripts/maintain/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/advlabdb/scripts/maintain/root_update.py b/advlabdb/scripts/maintain/root_update.py index 65999d5..03f9a83 100644 --- a/advlabdb/scripts/maintain/root_update.py +++ b/advlabdb/scripts/maintain/root_update.py @@ -1,5 +1,5 @@ -from advlabdb.scripts.maintain.shared import show_update_datetime -from advlabdb.scripts.terminal_utils import box, run +from ..terminal_utils import box, run +from .shared import show_update_datetime def main(): diff --git a/advlabdb/scripts/maintain/shared.py b/advlabdb/scripts/maintain/shared.py index 9b98eab..7ceb298 100644 --- a/advlabdb/scripts/maintain/shared.py +++ b/advlabdb/scripts/maintain/shared.py @@ -1,6 +1,6 @@ from datetime import datetime -from advlabdb.scripts.terminal_utils import box, spaced_hl +from ..terminal_utils import box, spaced_hl def show_update_datetime(): diff --git a/advlabdb/scripts/maintain/user_update.py b/advlabdb/scripts/maintain/user_update.py index 59a66e7..6ff8d56 100644 --- a/advlabdb/scripts/maintain/user_update.py +++ b/advlabdb/scripts/maintain/user_update.py @@ -1,6 +1,6 @@ -from advlabdb.scripts.maintain.shared import show_update_datetime -from advlabdb.scripts.setup.shared import LOCAL_BIN, install_latest_pipx, poetry_update -from advlabdb.scripts.terminal_utils import box, run +from ..setup.shared import LOCAL_BIN, install_latest_pipx, poetry_update +from ..terminal_utils import box, run +from .shared import show_update_datetime def main(): diff --git a/advlabdb/scripts/setup/__init__.py b/advlabdb/scripts/setup/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/advlabdb/scripts/setup/init_db.py b/advlabdb/scripts/setup/init_database.py similarity index 92% rename from advlabdb/scripts/setup/init_db.py rename to advlabdb/scripts/setup/init_database.py index 1c40326..baf75c3 100644 --- a/advlabdb/scripts/setup/init_db.py +++ b/advlabdb/scripts/setup/init_database.py @@ -1,10 +1,10 @@ from email_validator import validate_email from flask_security import hash_password -from advlabdb import app, db, user_datastore -from advlabdb.independent_funs import randomPassword -from advlabdb.models import MAX_YEAR, MIN_YEAR, Admin, Semester -from advlabdb.scripts.terminal_utils import box, confirm, validating_input +from ... import app, db, user_datastore +from ...independent_funs import randomPassword +from ...models import MAX_YEAR, MIN_YEAR, Admin, Semester +from ..terminal_utils import box, confirm, validating_input def main(): diff --git a/advlabdb/scripts/setup/logged_server_setup.py b/advlabdb/scripts/setup/logged_server_setup.py index 78ab2ed..a4e5b1e 100644 --- a/advlabdb/scripts/setup/logged_server_setup.py +++ b/advlabdb/scripts/setup/logged_server_setup.py @@ -1,12 +1,7 @@ from pathlib import Path -from advlabdb.scripts.setup.shared import ( - LOCAL_BIN, - LOGS_DIR, - install_latest_pipx, - poetry_update, -) -from advlabdb.scripts.terminal_utils import run, step +from ..terminal_utils import run, step +from .shared import LOCAL_BIN, LOGS_DIR, install_latest_pipx, poetry_update def main(): diff --git a/advlabdb/scripts/setup/server_setup.py b/advlabdb/scripts/setup/server_setup.py index 4a5c1e4..53dc42e 100644 --- a/advlabdb/scripts/setup/server_setup.py +++ b/advlabdb/scripts/setup/server_setup.py @@ -1,8 +1,6 @@ -from advlabdb.scripts.setup.logged_server_setup import ( - __file__ as logged_server_setup_script, -) -from advlabdb.scripts.setup.shared import LOGS_DIR -from advlabdb.scripts.terminal_utils import run +from ..terminal_utils import run +from .logged_server_setup import __file__ as logged_server_setup_script +from .shared import LOGS_DIR def main(): diff --git a/advlabdb/scripts/setup/shared.py b/advlabdb/scripts/setup/shared.py index e223bca..e6e0d69 100644 --- a/advlabdb/scripts/setup/shared.py +++ b/advlabdb/scripts/setup/shared.py @@ -1,7 +1,7 @@ from pathlib import Path -from advlabdb import __file__ as advlabdb_init_path # Points to __init__.py -from advlabdb.scripts.terminal_utils import run +from ... import __file__ as advlabdb_init_path # Points to __init__.py +from ..terminal_utils import run LOCAL_BIN = Path("/home/admin/.local/bin/") LOGS_DIR = Path("/var/log/advlabdb") diff --git a/advlabdb/scripts/test/__init__.py b/advlabdb/scripts/test/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/advlabdb/scripts/test/test_database.py b/advlabdb/scripts/test/test_database.py index 9547eed..2708a5b 100644 --- a/advlabdb/scripts/test/test_database.py +++ b/advlabdb/scripts/test/test_database.py @@ -2,8 +2,8 @@ from datetime import date from flask_security import hash_password -from advlabdb import app, db, user_datastore -from advlabdb.models import * +from ... import app, db, user_datastore +from ...models import * def main(): diff --git a/docs/user_docs/server_setup.adoc b/docs/user_docs/server_setup.adoc index cad5d43..a51bae8 100644 --- a/docs/user_docs/server_setup.adoc +++ b/docs/user_docs/server_setup.adoc @@ -43,7 +43,8 @@ Run the following: [source,bash] ---- # Run server setup script -python3 ~/advlabdb/advlabdb/scripts/setup/server_setup.py +cd ~/advlabdb +python3 -m advlabdb.scripts.setup.init_db ---- Change server_name in advlabdb.conf