Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 503 db session refactoring #509

Merged
merged 39 commits into from
Nov 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
a77ff73
Change Term Repository to use a session directly.
jzohrab Oct 30, 2024
38282aa
Change Book Repository to use a session directly.
jzohrab Oct 30, 2024
ffd00c6
Add BookTagRepository.
jzohrab Oct 30, 2024
c7a3d44
Add BookRepository.
jzohrab Oct 30, 2024
d62e285
Remove unneeded method.
jzohrab Oct 30, 2024
80702f9
Add LanguageRepository.
jzohrab Oct 30, 2024
b553945
Remove business term Language object, use ID only.
jzohrab Oct 31, 2024
b9cf6eb
Use LanguageRepository, remove Language.find.
jzohrab Oct 31, 2024
751d3af
Add TermTagRepository.
jzohrab Oct 31, 2024
7481907
Remove unnecessary method.
jzohrab Oct 31, 2024
6358784
Remove unneeded method.
jzohrab Oct 31, 2024
51bebfd
Pass session to TermForm instead of specific repo.
jzohrab Oct 31, 2024
0592a50
Add TermRepository.
jzohrab Oct 31, 2024
4809404
Add UserSettingRepository, SystemSettingRepository.
jzohrab Nov 1, 2024
ac03d64
Move Language.delete to LanguageRepository.
jzohrab Nov 1, 2024
6651e59
Move settings forms to module.
jzohrab Nov 1, 2024
033ee1b
Move app-specific settings to new lute/settings/current.py module.
jzohrab Nov 1, 2024
1862acf
Use global settings for show_highlight.
jzohrab Nov 2, 2024
cb8dfb5
Simplify Japanese parser settings, break db dependency.
jzohrab Nov 2, 2024
8e9d42a
Don't lint after black.
jzohrab Nov 2, 2024
17acf69
Convert term import to service object.
jzohrab Nov 2, 2024
ff175e2
Convert term parent map to service object.
jzohrab Nov 2, 2024
8c03ac8
Convert read service to object.
jzohrab Nov 2, 2024
3e4dde8
Convert render service to object.
jzohrab Nov 2, 2024
f507dfa
Convert language service to object.
jzohrab Nov 2, 2024
5a7fda7
Convert book service to object.
jzohrab Nov 2, 2024
50a448c
Convert backup service to object.
jzohrab Nov 2, 2024
1e899fc
Convert theme service to object.
jzohrab Nov 2, 2024
f885f3a
Move BookStats model to models.
jzohrab Nov 2, 2024
6c6e01d
Use existing instance session.
jzohrab Nov 2, 2024
07fc581
Convert book stats to service.
jzohrab Nov 2, 2024
af63584
Pass session arg explicitly where needed.
jzohrab Nov 2, 2024
495b05c
Change lastbackup setting to 'user' setting.
jzohrab Nov 3, 2024
81d03f4
Move model repositories to separate file.
jzohrab Nov 3, 2024
4847fae
Break circular dependency.
jzohrab Nov 3, 2024
8475644
Improve startup of browser-based tests.
jzohrab Nov 3, 2024
96e1dd0
Reduce acceptance test console errors.
jzohrab Nov 3, 2024
8b7f04a
'Fix' playwright, acceptance sterr noise.
jzohrab Nov 3, 2024
892ff7e
Fix lint.
jzohrab Nov 3, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 29 additions & 19 deletions lute/app_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,19 @@
from lute.config.app_config import AppConfig
from lute.db import db
from lute.db.setup.main import setup_db
from lute.db.management import add_default_user_settings
from lute.db.data_cleanup import clean_data
import lute.backup.service as backupservice
from lute.backup.service import Service as BackupService
import lute.db.demo
import lute.utils.formutils

from lute.parse.registry import init_parser_plugins, supported_parsers

from lute.models.book import Book
from lute.models.language import Language
from lute.models.setting import BackupSettings, UserSetting
from lute.book.stats import mark_stale
from lute.settings.current import refresh_global_settings, current_settings
from lute.models.repositories import UserSettingRepository
from lute.book.stats import Service as StatsService

from lute.book.routes import bp as book_bp
from lute.bookmarks.routes import bp as bookmarks_bp
Expand Down Expand Up @@ -111,35 +113,40 @@ def inject_menu_bar_vars():
"""
Inject backup settings into the all templates for the menu bar.
"""
bs = BackupSettings.get_backup_settings()
us_repo = UserSettingRepository(db.session)
bs = us_repo.get_backup_settings()
have_languages = len(db.session.query(Language).all()) > 0
ret = {
"have_languages": have_languages,
"backup_enabled": bs.backup_enabled,
"backup_directory": bs.backup_dir,
"backup_last_display_date": bs.last_backup_display_date,
"backup_time_since": bs.time_since_last_backup,
"user_settings": json.dumps(UserSetting.all_settings()),
"user_settings": json.dumps(current_settings),
}
return ret

@app.route("/")
def index():
is_production = not lute.db.demo.contains_demo_data()
bkp_settings = BackupSettings.get_backup_settings()
is_production = not lute.db.demo.contains_demo_data(db.session)
us_repo = UserSettingRepository(db.session)
bkp_settings = us_repo.get_backup_settings()

have_books = len(db.session.query(Book).all()) > 0
have_languages = len(db.session.query(Language).all()) > 0
language_choices = lute.utils.formutils.language_choices("(all languages)")
current_language_id = lute.utils.formutils.valid_current_language_id()
language_choices = lute.utils.formutils.language_choices(
db.session, "(all languages)"
)
current_language_id = lute.utils.formutils.valid_current_language_id(db.session)

should_run_auto_backup = backupservice.should_run_auto_backup(bkp_settings)
bs = BackupService(db.session)
should_run_auto_backup = bs.should_run_auto_backup(bkp_settings)
# Only back up if we have books, otherwise the backup is
# kicked off when the user empties the demo database.
if is_production and have_books and should_run_auto_backup:
return redirect("/backup/backup", 302)

warning_msg = backupservice.backup_warning(bkp_settings)
warning_msg = bs.backup_warning(bkp_settings)
backup_show_warning = (
bkp_settings.backup_warn
and bkp_settings.backup_enabled
Expand All @@ -152,7 +159,7 @@ def index():
hide_homelink=True,
dbname=app_config.dbname,
datapath=app_config.datapath,
tutorial_book_id=lute.db.demo.tutorial_book_id(),
tutorial_book_id=lute.db.demo.tutorial_book_id(db.session),
have_books=have_books,
have_languages=have_languages,
language_choices=language_choices,
Expand All @@ -167,14 +174,15 @@ def index():
@app.route("/refresh_all_stats")
def refresh_all_stats():
books_to_update = db.session.query(Book).filter(Book.archived == 0).all()
svc = StatsService(db.session)
for book in books_to_update:
mark_stale(book)
svc.mark_stale(book)
return redirect("/", 302)

@app.route("/wipe_database")
def wipe_db():
if lute.db.demo.contains_demo_data():
lute.db.demo.delete_demo_data()
if lute.db.demo.contains_demo_data(db.session):
lute.db.demo.delete_demo_data(db.session)
msg = """
The database has been wiped clean. Have fun! <br /><br />
<i>(Lute has automatically enabled backups --
Expand All @@ -185,8 +193,8 @@ def wipe_db():

@app.route("/remove_demo_flag")
def remove_demo():
if lute.db.demo.contains_demo_data():
lute.db.demo.remove_flag()
if lute.db.demo.contains_demo_data(db.session):
lute.db.demo.remove_flag(db.session)
msg = """
Demo mode deactivated. Have fun! <br /><br />
<i>(Lute has automatically enabled backups --
Expand Down Expand Up @@ -305,9 +313,10 @@ def _pragmas_on_connect(dbapi_con, con_record): # pylint: disable=unused-argume

with app.app_context():
db.create_all()
UserSetting.load()
add_default_user_settings(db.session, app_config.default_user_backup_path)
refresh_global_settings(db.session)
# TODO valid parsers: do parser check, mark valid as active, invalid as inactive.
clean_data()
clean_data(db.session)
app.db = db

_add_base_routes(app, app_config)
Expand Down Expand Up @@ -392,6 +401,7 @@ def null_print(s): # pylint: disable=unused-argument
outfunc("Initializing app.")
app = _create_app(app_config, extra_config)

# Plugins are loaded after the app, as they may use settings etc.
_init_parser_plugins(app_config.plugin_datapath, outfunc)

return app
28 changes: 19 additions & 9 deletions lute/backup/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,28 @@
send_file,
flash,
)
from lute.models.setting import BackupSettings
from lute.backup.service import create_backup, skip_this_backup, list_backups
from lute.db import db
from lute.models.repositories import UserSettingRepository
from lute.backup.service import Service


bp = Blueprint("backup", __name__, url_prefix="/backup")


def _get_settings():
"Get backup settings."
repo = UserSettingRepository(db.session)
return repo.get_backup_settings()


@bp.route("/index")
def index():
"""
List all backups.
"""
settings = BackupSettings.get_backup_settings()
backups = list_backups(settings.backup_dir)
settings = _get_settings()
service = Service(db.session)
backups = service.list_backups(settings.backup_dir)
backups.sort(reverse=True)

return render_template(
Expand All @@ -40,7 +48,7 @@ def index():
@bp.route("/download/<filename>")
def download_backup(filename):
"Download the given backup file."
settings = BackupSettings.get_backup_settings()
settings = _get_settings()
fullpath = os.path.join(settings.backup_dir, filename)
return send_file(fullpath, as_attachment=True)

Expand All @@ -56,7 +64,7 @@ def backup():
if "type" in request.args:
backuptype = "manual"

settings = BackupSettings.get_backup_settings()
settings = _get_settings()
return render_template(
"backup/backup.html", backup_folder=settings.backup_dir, backuptype=backuptype
)
Expand All @@ -73,10 +81,11 @@ def do_backup():
backuptype = prms["type"]

c = current_app.env_config
settings = BackupSettings.get_backup_settings()
settings = _get_settings()
service = Service(db.session)
is_manual = backuptype.lower() == "manual"
try:
f = create_backup(c, settings, is_manual=is_manual)
f = service.create_backup(c, settings, is_manual=is_manual)
flash(f"Backup created: {f}", "notice")
return jsonify(f)
except Exception as e: # pylint: disable=broad-exception-caught
Expand All @@ -87,5 +96,6 @@ def do_backup():
@bp.route("/skip_this_backup", methods=["GET"])
def handle_skip_this_backup():
"Update last backup date so backup not attempted again."
skip_this_backup()
service = Service(db.session)
service.skip_this_backup()
return redirect("/", 302)
Loading
Loading