Skip to content

Commit

Permalink
#1262 add set_app_default_settings
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisjsimpson committed Oct 22, 2023
1 parent 11a742e commit 6a4b420
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 10 deletions.
8 changes: 6 additions & 2 deletions subscribie/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
import click
from jinja2 import Template
from .models import PaymentProvider, Person, Company, Module, Plan, PriceList
from .bootstrap_app import migrate_database
from .bootstrap_app import migrate_database, set_app_default_settings

load_dotenv(verbose=True)

Expand All @@ -67,10 +67,14 @@ def create_app(test_config=None):
with app.app_context():
database.init_app(app)
# Initialize flask_migrate using Migrate
# Note: Migrate configures the flask_migrate addon- it does not
# *perform* a migration
Migrate(app, database)
# Trigger a programatic database migration during application boot

# Perform a programatic database migration during application boot
# Note: flask_migrate calls database migrations 'upgrades'.
migrate_database()
set_app_default_settings()

if test_config is not None:
app.config.update(test_config)
Expand Down
8 changes: 0 additions & 8 deletions subscribie/blueprints/admin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1202,17 +1202,9 @@ def connect_tawk_manually():
@login_required
def add_custom_code():
setting = Setting.query.first()
if setting is None:
setting = Setting()
database.session.add(setting)

if request.method == "POST":
custom_code = request.form.get("code", None)
if custom_code is not None:
setting = Setting.query.first()
if setting is None:
setting = Setting()
database.session.add(setting)
setting.custom_code = custom_code
database.session.commit()
flash("Custom code added")
Expand Down
23 changes: 23 additions & 0 deletions subscribie/bootstrap_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
from .logger import logger # noqa: F401
import logging
from flask import current_app
from subscribie.database import database
from subscribie.models import Setting

log = logging.getLogger(__name__)

Expand All @@ -13,3 +15,24 @@ def migrate_database():
upgrade(
directory=Path(current_app.config["SUBSCRIBIE_REPO_DIRECTORY"] + "/migrations")
)


def set_app_default_settings():
"""Pre-populate the Settings model with the column insertion defaults.
If the Settings model is empty, pre-populate the database with the column
insertion defaults (see model.py -> class Settings).
Note this does not relate to .env settings. See README.md
The Settings stored in the Settings database model are for
user controllable settings which may be changed at runtime
without an application restart.
Issue #1262
https://github.com/Subscribie/subscribie/issues/1262
"""
setting = Setting.query.first()
if setting is None:
setting = Setting()
database.session.add(setting)
database.session.commit()

0 comments on commit 6a4b420

Please sign in to comment.