Skip to content

Commit

Permalink
wip #1276 upgrade markupsafe, remove before_app_first_request usage, …
Browse files Browse the repository at this point in the history
…MAX_CONTEXT_LENGTH update
  • Loading branch information
chrisjsimpson committed Jan 1, 2024
1 parent 7a6298e commit 801a427
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 51 deletions.
73 changes: 39 additions & 34 deletions subscribie/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion subscribie/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions subscribie/blueprints/admin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@
request,
session,
Response,
Markup,
escape,
)
from markupsafe import Markup, escape
import jinja2
import requests
from subscribie.utils import (
Expand Down
3 changes: 1 addition & 2 deletions subscribie/blueprints/pages/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion subscribie/blueprints/subscriber/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
g,
current_app,
request,
Markup,
)
from markupsafe import Markup
from subscribie.forms import (
PasswordLoginForm,
SubscriberForgotPasswordForm,
Expand Down
12 changes: 1 addition & 11 deletions subscribie/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 801a427

Please sign in to comment.