diff --git a/advlabdb/__init__.py b/advlabdb/__init__.py index 382d859..34999d3 100644 --- a/advlabdb/__init__.py +++ b/advlabdb/__init__.py @@ -8,7 +8,7 @@ from flask_sqlalchemy import SQLAlchemy from dotenv import load_dotenv -from .independent_funs import parse_bool +from .advlabdb_independent_funs import parse_bool def set_from_env(app, var): diff --git a/advlabdb/adminModelViews.py b/advlabdb/adminModelViews.py index 46fccd5..bb8de37 100644 --- a/advlabdb/adminModelViews.py +++ b/advlabdb/adminModelViews.py @@ -31,6 +31,7 @@ from wtforms.validators import URL, DataRequired, Email, NumberRange, Optional from wtforms.widgets import NumberInput from . import adminSpace, app, assistantSpace, db, user_datastore +from .advlabdb_independent_funs import flashRandomPassword, randomPassword from .configUtils import getConfig from .customClasses import ( CustomIdEndpointLinkRowAction, @@ -38,15 +39,13 @@ from .customClasses import ( SecureAdminModelView, ) from .database_import import importFromFile -from .dependent_funs import ( - flashRandomPassword, +from .exceptions import DataBaseException, ModelViewException +from .model_dependent_funs import ( initActiveSemesterMenuLinks, setUserActiveSemester, sortedSemestersStartingWithNewest, userActiveSemester, ) -from .exceptions import DataBaseException, ModelViewException -from .independent_funs import randomPassword from .models import ( MAX_MARK, MAX_YEAR, diff --git a/advlabdb/independent_funs.py b/advlabdb/advlabdb_independent_funs.py similarity index 70% rename from advlabdb/independent_funs.py rename to advlabdb/advlabdb_independent_funs.py index 5fc8576..66a3a9c 100644 --- a/advlabdb/independent_funs.py +++ b/advlabdb/advlabdb_independent_funs.py @@ -1,6 +1,18 @@ +# Functions not dependent on advlabdb + from random import choice from string import ascii_letters, digits +from flask import flash + + +def randomPassword(): + return "".join(choice(ascii_letters + digits) for i in range(15)) + + +def flashRandomPassword(password): + flash(f"Random password: {password}", category="warning") + def parse_bool(str): str_lower = str.lower() @@ -10,7 +22,3 @@ def parse_bool(str): return True else: raise ValueError(f'Can not parse a bool from "{str}"') - - -def randomPassword(): - return "".join(choice(ascii_letters + digits) for i in range(15)) diff --git a/advlabdb/assistantModelViews.py b/advlabdb/assistantModelViews.py index 3d5fa92..1b6af43 100644 --- a/advlabdb/assistantModelViews.py +++ b/advlabdb/assistantModelViews.py @@ -8,14 +8,10 @@ from wtforms.fields import DateField from wtforms.validators import NumberRange, Optional from . import assistantSpace, db +from .advlabdb_independent_funs import flashRandomPassword, randomPassword from .customClasses import SecureAssistantBaseView, SecureAssistantModelView -from .dependent_funs import ( - flashRandomPassword, - initActiveSemesterMenuLinks, - userActiveSemester, -) from .exceptions import DataBaseException, ModelViewException -from .independent_funs import randomPassword +from .model_dependent_funs import initActiveSemesterMenuLinks, userActiveSemester from .models import ( MAX_MARK, MIN_MARK, diff --git a/advlabdb/customClasses.py b/advlabdb/customClasses.py index b3404b5..2ffb36c 100644 --- a/advlabdb/customClasses.py +++ b/advlabdb/customClasses.py @@ -8,8 +8,9 @@ from flask_security import current_user from sqlalchemy import and_, select from . import db -from .dependent_funs import get_count, reportBadAttempt, userActiveSemester from .exceptions import DataBaseException, ModelViewException +from .model_dependent_funs import userActiveSemester +from .model_independent_funs import get_count, reportBadAttempt from .models import ( Assistant, ExperimentMark, diff --git a/advlabdb/dependent_funs.py b/advlabdb/model_dependent_funs.py similarity index 95% rename from advlabdb/dependent_funs.py rename to advlabdb/model_dependent_funs.py index a41a6dd..68ab657 100644 --- a/advlabdb/dependent_funs.py +++ b/advlabdb/model_dependent_funs.py @@ -1,16 +1,14 @@ +# Functions dependent on advlabdb.models + from flask import flash, url_for from flask_admin.menu import MenuLink from flask_security import current_user -from sqlalchemy import func, select +from sqlalchemy import select from . import app, db from .models import Semester -def flashRandomPassword(password): - flash(f"Random password: {password}", category="warning") - - def sortedSemestersStartingWithNewest(limit=0): # Inserting an older semester is not allowed! # Therefore, the id is enough. diff --git a/advlabdb/model_independent_funs.py b/advlabdb/model_independent_funs.py new file mode 100644 index 0000000..a229f29 --- /dev/null +++ b/advlabdb/model_independent_funs.py @@ -0,0 +1,17 @@ +# Functions not dependent on advlabdb.models + +from sqlalchemy import func, select + +from . import db + + +def reportBadAttempt(message): + print("BAD ATTEMPT:", message) # TODO: Log + + +def get_count(table): + return db.session.scalar(select(func.count()).select_from(table)) + + +def get_first(table): + return db.session.execute(table.limit(1)).scalars().first() diff --git a/advlabdb/routes.py b/advlabdb/routes.py index 7a15a4f..aeaaad9 100644 --- a/advlabdb/routes.py +++ b/advlabdb/routes.py @@ -2,7 +2,7 @@ from flask import flash, redirect, render_template, request, url_for from flask_security import auth_required, current_user, roles_accepted, roles_required from . import app -from .dependent_funs import setUserActiveSemester, userActiveSemester +from .model_dependent_funs import setUserActiveSemester, userActiveSemester @app.context_processor diff --git a/advlabdb/scripts/maintain/reset_admin_password.py b/advlabdb/scripts/maintain/reset_admin_password.py index 18d3bd2..51be09a 100644 --- a/advlabdb/scripts/maintain/reset_admin_password.py +++ b/advlabdb/scripts/maintain/reset_admin_password.py @@ -2,7 +2,7 @@ from flask_security import admin_change_password from sqlalchemy import select from ... import app, db -from ...independent_funs import randomPassword +from ...advlabdb_independent_funs import randomPassword from ...models import Admin, User from ..terminal_utils import box, spaced_hl, validating_input diff --git a/advlabdb/scripts/setup/init_database.py b/advlabdb/scripts/setup/init_database.py index 9a5629d..5319cc0 100644 --- a/advlabdb/scripts/setup/init_database.py +++ b/advlabdb/scripts/setup/init_database.py @@ -2,7 +2,7 @@ from email_validator import validate_email from flask_security import hash_password from ... import app, db, user_datastore -from ...independent_funs import randomPassword +from ...advlabdb_independent_funs import randomPassword from ...models import MAX_YEAR, MIN_YEAR, Admin, Semester from ..terminal_utils import box, confirm, validating_input