diff --git a/subscribie/__init__.py b/subscribie/__init__.py index e52d239b9..6049cfa3b 100644 --- a/subscribie/__init__.py +++ b/subscribie/__init__.py @@ -34,13 +34,12 @@ configure_uploads, UploadSet, IMAGES, - patch_request_class, ) # noqa: E501 import importlib import urllib from pathlib import Path import sqlalchemy -from flask_migrate import Migrate +from flask_migrate import Migrate, upgrade import click from jinja2 import Template from .models import PaymentProvider, Person, Company, Module, Plan, PriceList @@ -56,17 +55,16 @@ def seed_db(): def create_app(test_config=None): app = Flask(__name__, instance_relative_config=True) - babel = Babel(app) LANGUAGES = ["en", "de", "es", "fr", "hr"] load_dotenv(verbose=True) PERMANENT_SESSION_LIFETIME = int(os.environ.pop("PERMANENT_SESSION_LIFETIME", 1800)) app.config.update(os.environ) app.config["PERMANENT_SESSION_LIFETIME"] = PERMANENT_SESSION_LIFETIME + app.config["MAX_CONTENT_LENGTH"] = int(os.getenv("MAX_CONTENT_LENGTH", 52428800)) if test_config is not None: app.config.update(test_config) - @babel.localeselector def get_locale(): language_code = session.get("language_code", None) if language_code is not None: @@ -79,6 +77,8 @@ def get_locale(): log.info(f"language_code best match set to: {language_code}") return request.accept_languages.best_match(LANGUAGES) + Babel(app, locale_selector=get_locale) + @app.before_request def start_session(): session.permanent = True @@ -88,38 +88,8 @@ def start_session(): session["sid"] = urllib.parse.quote_plus(b64encode(os.urandom(10))) log.info(f"Starting with sid {session['sid']}") - @app.before_first_request - def register_modules(): - """Import any custom modules""" - # Set custom modules path - sys.path.append(app.config["MODULES_PATH"]) - modules = Module.query.all() - log.info(f"sys.path contains: {sys.path}") - for module in modules: - # Assume standard python module - try: - log.info(f"Attempting to importing module: {module.name}") - importlib.import_module(module.name) - except ModuleNotFoundError: - log.debug(f"Error: Could not import module: {module.name}") - # Register modules as blueprint (if it is one) - try: - importedModule = importlib.import_module(module.name) - if isinstance(getattr(importedModule, module.name), Blueprint): - # Load any config the Blueprint declares - blueprint = getattr(importedModule, module.name) - blueprintConfig = "".join([blueprint.root_path, "/", "config.py"]) - app.config.from_pyfile(blueprintConfig, silent=True) - # Register the Blueprint - app.register_blueprint(getattr(importedModule, module.name)) - log.info(f"Imported {module.name} as flask Blueprint") - - except (ModuleNotFoundError, AttributeError): - log.error(f"Error: Could not import module as blueprint: {module.name}") - CORS(app) images = UploadSet("images", IMAGES) - patch_request_class(app, int(app.config.get("MAX_CONTENT_LENGTH", 2 * 1024 * 1024))) configure_uploads(app, images) from . import auth @@ -155,6 +125,41 @@ def register_modules(): database.init_app(app) Migrate(app, database) + """Migrate database when app first boots""" + log.info("Migrating database") + upgrade( + directory=Path( + current_app.config["SUBSCRIBIE_REPO_DIRECTORY"] + "/migrations" + ) + ) + + """Import any custom modules""" + # Set custom modules path + sys.path.append(app.config["MODULES_PATH"]) + modules = Module.query.all() + log.info(f"sys.path contains: {sys.path}") + for module in modules: + # Assume standard python module + try: + log.info(f"Attempting to importing module: {module.name}") + importlib.import_module(module.name) + except ModuleNotFoundError: + log.debug(f"Error: Could not import module: {module.name}") + # Register modules as blueprint (if it is one) + try: + importedModule = importlib.import_module(module.name) + if isinstance(getattr(importedModule, module.name), Blueprint): + # Load any config the Blueprint declares + blueprint = getattr(importedModule, module.name) + blueprintConfig = "".join([blueprint.root_path, "/", "config.py"]) + app.config.from_pyfile(blueprintConfig, silent=True) + # Register the Blueprint + app.register_blueprint(getattr(importedModule, module.name)) + log.info(f"Imported {module.name} as flask Blueprint") + + except (ModuleNotFoundError, AttributeError): + log.error(f"Error: Could not import module as blueprint: {module.name}") + try: payment_provider = PaymentProvider.query.first() if payment_provider is None: diff --git a/subscribie/auth.py b/subscribie/auth.py index f4d2989fb..b6f0adf63 100644 --- a/subscribie/auth.py +++ b/subscribie/auth.py @@ -12,8 +12,8 @@ url_for, current_app, render_template_string, - Markup, ) +from markupsafe import Markup from subscribie.email import EmailMessageQueue from subscribie.utils import get_stripe_secret_key, get_stripe_connect_account from base64 import urlsafe_b64encode diff --git a/subscribie/blueprints/admin/__init__.py b/subscribie/blueprints/admin/__init__.py index 0126be6f3..428d48733 100644 --- a/subscribie/blueprints/admin/__init__.py +++ b/subscribie/blueprints/admin/__init__.py @@ -15,9 +15,8 @@ request, session, Response, - Markup, - escape, ) +from markupsafe import Markup, escape import jinja2 import requests from subscribie.utils import ( diff --git a/subscribie/blueprints/pages/__init__.py b/subscribie/blueprints/pages/__init__.py index e1701f728..59ae94f3e 100644 --- a/subscribie/blueprints/pages/__init__.py +++ b/subscribie/blueprints/pages/__init__.py @@ -7,9 +7,8 @@ current_app, url_for, flash, - Markup, - escape, ) +from markupsafe import Markup, escape from subscribie.auth import login_required from subscribie.models import database, Page from pathlib import Path diff --git a/subscribie/blueprints/subscriber/__init__.py b/subscribie/blueprints/subscriber/__init__.py index 12f433ca5..2512faf9f 100644 --- a/subscribie/blueprints/subscriber/__init__.py +++ b/subscribie/blueprints/subscriber/__init__.py @@ -14,8 +14,8 @@ g, current_app, request, - Markup, ) +from markupsafe import Markup from subscribie.forms import ( PasswordLoginForm, SubscriberForgotPasswordForm, diff --git a/subscribie/views.py b/subscribie/views.py index d56c1c5e0..db2a44121 100644 --- a/subscribie/views.py +++ b/subscribie/views.py @@ -14,10 +14,9 @@ current_app, g, send_from_directory, - Markup, ) +from markupsafe import Markup from .models import Company, Plan, Integration, Page, Category, Setting, PaymentProvider -from flask_migrate import upgrade from subscribie.blueprints.style import inject_custom_style from subscribie.database import database from subscribie.signals import register_signal_handlers @@ -45,15 +44,6 @@ register_signal_handlers() -@bp.before_app_first_request -def migrate_database(): - """Migrate database when app first boots""" - log.info("Migrating database") - upgrade( - directory=Path(current_app.config["SUBSCRIBIE_REPO_DIRECTORY"] + "/migrations") - ) - - @bp.before_app_request def on_each_request(): # Detect country code if present in the request from proxy