Skip to content

Commit

Permalink
Pass session arg explicitly where needed.
Browse files Browse the repository at this point in the history
- datatables methods
- lute/db data cleanup, demo, management
- stats
  • Loading branch information
jzohrab committed Nov 3, 2024
1 parent 07fc581 commit af63584
Show file tree
Hide file tree
Showing 25 changed files with 124 additions and 130 deletions.
20 changes: 11 additions & 9 deletions lute/app_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,15 @@ def inject_menu_bar_vars():

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

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)

bs = BackupService(db.session)
should_run_auto_backup = bs.should_run_auto_backup(bkp_settings)
Expand All @@ -154,7 +156,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 @@ -176,8 +178,8 @@ def refresh_all_stats():

@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 @@ -188,8 +190,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 @@ -310,7 +312,7 @@ def _pragmas_on_connect(dbapi_con, con_record): # pylint: disable=unused-argume
db.create_all()
load_settings(db.session, app_config.default_user_backup_path)
# 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
5 changes: 1 addition & 4 deletions lute/book/datatables.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@
Show books in datatables.
"""

from lute.db import db
from lute.utils.data_tables import DataTablesSqliteQuery, supported_parser_type_criteria


def get_data_tables_list(parameters, is_archived):
def get_data_tables_list(parameters, is_archived, session):
"Book json data for datatables."
archived = "true" if is_archived else "false"

Expand Down Expand Up @@ -70,7 +69,5 @@ def get_data_tables_list(parameters, is_archived):
if language_id != 0:
base_sql += f" and LgID = {language_id}"

session = db.session
connection = session.connection()

return DataTablesSqliteQuery.get_data(base_sql, parameters, connection)
10 changes: 6 additions & 4 deletions lute/book/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def datatables_source(is_archived):
# (currently unused)
parameters = DataTablesFlaskParamParser.parse_params(request.form)
_load_term_custom_filters(request.form, parameters)
data = get_data_tables_list(parameters, is_archived)
data = get_data_tables_list(parameters, is_archived, db.session)
return jsonify(data)


Expand All @@ -59,8 +59,10 @@ def datatables_active_source():
@bp.route("/archived", methods=["GET"])
def archived():
"List archived books."
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)

return render_template(
"book/index.html",
Expand Down Expand Up @@ -108,7 +110,7 @@ def new():
b = _book_from_url(import_url)

form = NewBookForm(obj=b)
form.language_id.choices = lute.utils.formutils.language_choices()
form.language_id.choices = lute.utils.formutils.language_choices(db.session)
repo = Repository(db.session)

if form.validate_on_submit():
Expand Down
5 changes: 1 addition & 4 deletions lute/bookmarks/datatables.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@
Show bookmarks in datatables.
"""

from lute.db import db
from lute.utils.data_tables import DataTablesSqliteQuery


def get_data_tables_list(parameters, book_id):
def get_data_tables_list(parameters, book_id, session):
"Bookmark json data for datatables."

base_sql = f"""
Expand All @@ -16,7 +15,5 @@ def get_data_tables_list(parameters, book_id):
WHERE tx.TxBkID = { book_id }
"""

session = db.session
connection = session.connection()

return DataTablesSqliteQuery.get_data(base_sql, parameters, connection)
2 changes: 1 addition & 1 deletion lute/bookmarks/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
def datatables_bookmarks(bookid):
"Get datatables json for bookmarks."
parameters = DataTablesFlaskParamParser.parse_params(request.form)
data = get_data_tables_list(parameters, bookid)
data = get_data_tables_list(parameters, bookid, db.session)
return jsonify(data)


Expand Down
13 changes: 6 additions & 7 deletions lute/db/data_cleanup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@
These cleanup routines will be called by the app_factory.
"""

from lute.db import db
from lute.models.book import Text


def _set_texts_word_count():
def _set_texts_word_count(session):
"""
texts.TxWordCount should be set for all texts.
Expand All @@ -18,7 +17,7 @@ def _set_texts_word_count():
Ref https://github.com/jzohrab/lute-v3/issues/95
"""
calc_counts = db.session.query(Text).filter(Text.word_count.is_(None)).all()
calc_counts = session.query(Text).filter(Text.word_count.is_(None)).all()

# Don't recalc with invalid parsers!!!!
recalc = [t for t in calc_counts if t.book.language.is_supported]
Expand All @@ -31,10 +30,10 @@ def _set_texts_word_count():
pt = t.book.language.get_parsed_tokens(t.text)
words = [w for w in pt if w.is_word]
t.word_count = len(words)
db.session.add(t)
db.session.commit()
session.add(t)
session.commit()


def clean_data():
def clean_data(session):
"Clean all data as required."
_set_texts_word_count()
_set_texts_word_count(session)
57 changes: 28 additions & 29 deletions lute/db/demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
from lute.book.model import Repository
from lute.book.stats import Service as StatsService
from lute.models.setting import SystemSettingRepository
from lute.db import db
import lute.db.management


Expand All @@ -39,100 +38,100 @@ def _demo_languages():
]


def contains_demo_data():
def contains_demo_data(session):
"""
True if IsDemoData setting is present.
"""
repo = SystemSettingRepository(db.session)
repo = SystemSettingRepository(session)
ss = repo.get_value("IsDemoData")
if ss is None:
return False
return True


def remove_flag():
def remove_flag(session):
"""
Remove IsDemoData setting.
"""
if not contains_demo_data():
if not contains_demo_data(session):
raise RuntimeError("Can't delete non-demo data.")

repo = SystemSettingRepository(db.session)
repo = SystemSettingRepository(session)
repo.delete_key("IsDemoData")
db.session.commit()
session.commit()


def tutorial_book_id():
def tutorial_book_id(session):
"""
Return the book id of the tutorial.
"""
if not contains_demo_data():
if not contains_demo_data(session):
return None
sql = """select BkID from books
inner join languages on LgID = BkLgID
where LgName = 'English' and BkTitle = 'Tutorial'
"""
r = db.session.execute(text(sql)).first()
r = session.execute(text(sql)).first()
if r is None:
return None
return int(r[0])


def delete_demo_data():
def delete_demo_data(session):
"""
If this is a demo, wipe everything.
"""
if not contains_demo_data():
if not contains_demo_data(session):
raise RuntimeError("Can't delete non-demo data.")
remove_flag()
lute.db.management.delete_all_data()
remove_flag(session)
lute.db.management.delete_all_data(session)


# Loading demo data.


def load_demo_languages():
def load_demo_languages(session):
"""
Load selected predefined languages. Assume everything is supported.
This method will also be called during acceptance tests, so it's public.
"""
demo_langs = _demo_languages()
service = Service(db.session)
service = Service(session)
langs = [service.get_language_def(langname)["language"] for langname in demo_langs]
supported = [lang for lang in langs if lang.is_supported]
for lang in supported:
db.session.add(lang)
db.session.commit()
session.add(lang)
session.commit()


def load_demo_stories():
def load_demo_stories(session):
"Load the stories."
demo_langs = _demo_languages()
service = Service(db.session)
service = Service(session)
langdefs = [service.get_language_def(langname) for langname in demo_langs]
langdefs = [d for d in langdefs if d["language"].is_supported]

r = Repository(db.session)
r = Repository(session)
for d in langdefs:
for b in d["books"]:
r.add(b)
r.commit()

repo = SystemSettingRepository(db.session)
repo = SystemSettingRepository(session)
repo.set_value("IsDemoData", True)
db.session.commit()
session.commit()

svc = StatsService(db.session)
svc = StatsService(session)
svc.refresh_stats()


def load_demo_data():
def load_demo_data(session):
"""
Load the data.
"""
load_demo_languages()
load_demo_stories()
repo = SystemSettingRepository(db.session)
load_demo_languages(session)
load_demo_stories(session)
repo = SystemSettingRepository(session)
repo.set_value("IsDemoData", True)
db.session.commit()
session.commit()
9 changes: 4 additions & 5 deletions lute/db/management.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@

from sqlalchemy import text
from flask import current_app
from lute.db import db
from lute.settings.current import load


def delete_all_data():
def delete_all_data(session):
"""
DANGEROUS! Delete everything, restore user settings, clear sys settings.
Expand All @@ -24,6 +23,6 @@ def delete_all_data():
"delete from settings",
]
for s in statements:
db.session.execute(text(s))
db.session.commit()
load(db.session, current_app.env_config.default_user_backup_path)
session.execute(text(s))
session.commit()
load(session, current_app.env_config.default_user_backup_path)
12 changes: 6 additions & 6 deletions lute/dev_api/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,25 +38,25 @@ def _ensure_is_test_db():
@bp.route("/wipe_db", methods=["GET"])
def wipe_db():
"Clean it all."
lute.db.management.delete_all_data()
lute.db.management.delete_all_data(db.session)
flash("db wiped")
return redirect("/", 302)


@bp.route("/load_demo", methods=["GET"])
def load_demo():
"Clean out everything, and load the demo."
lute.db.management.delete_all_data()
lute.db.demo.load_demo_data()
lute.db.management.delete_all_data(db.session)
lute.db.demo.load_demo_data(db.session)
flash("demo loaded")
return redirect("/", 302)


@bp.route("/load_demo_languages", methods=["GET"])
def load_demo_languages():
"Clean out everything, and load the demo langs with dummy dictionaries."
lute.db.management.delete_all_data()
lute.db.demo.load_demo_languages()
lute.db.management.delete_all_data(db.session)
lute.db.demo.load_demo_languages(db.session)
langs = db.session.query(Language).all()
for lang in langs:
d = lang.dictionaries[0]
Expand All @@ -70,7 +70,7 @@ def load_demo_languages():
@bp.route("/load_demo_stories", methods=["GET"])
def load_demo_stories():
"Stories only. No db wipe."
lute.db.demo.load_demo_stories()
lute.db.demo.load_demo_stories(db.session)
flash("stories loaded")
return redirect("/", 302)

Expand Down
Loading

0 comments on commit af63584

Please sign in to comment.