- Fixed numerous issues with the cwa_db that were resulting in the db occasionally becoming locked for some users

- CWA Settings page checkbox labels are now clickable and the target format is unable to be selected to be ignored
- Users can now also set certain formats to be ignored by the auto importer
This commit is contained in:
crocodilestick
2024-11-15 11:47:20 +00:00
parent d6b2caa7f8
commit fc188873a4
5 changed files with 112 additions and 42 deletions
+1 -1
View File
@@ -25,7 +25,7 @@ DEV
cwa.db
# Dev files
dev_changelog.md
changelogs/
*.txz
# Distribution / packaging
+20 -7
View File
@@ -90,12 +90,13 @@ def set_cwa_settings():
"auto_convert"]
string_settings = ["auto_convert_target_format"]
for format in ignorable_formats:
string_settings.append(f"ignore_{format}")
string_settings.append(f"ignore_import_{format}")
string_settings.append(f"ignore_convert_{format}")
if request.method == 'POST':
cwa_db = CWA_DB()
if request.form['submit_button'] == "Submit":
result = {"cwa_ignored_formats":[]}
result = {"auto_convert_ignored_formats":[], "auto_import_ignored_formats":[]}
# set boolean_settings
for setting in boolean_settings:
value = request.form.get(setting)
@@ -107,21 +108,33 @@ def set_cwa_settings():
# set string settings
for setting in string_settings:
value = request.form.get(setting)
if setting[:7] == "ignore_":
if setting[:14] == "ignore_convert":
if value == None:
continue
else:
result["cwa_ignored_formats"].append(value)
result["auto_convert_ignored_formats"].append(value)
continue
elif setting == "auto_convert_target_format" and value == None:
value = cwa_db.cwa_settings['auto_convert_target_format']
elif setting[:13] == "ignore_import":
if value == None:
continue
else:
result["auto_import_ignored_formats"].append(value)
continue
elif setting == "auto_import_target_format" and value == None:
value = cwa_db.cwa_settings['auto_import_target_format']
result |= {setting:value}
# Prevent ignoring of target format
if result['auto_convert_target_format'] in result['auto_convert_ignored_formats']:
result['auto_convert_ignored_formats'].remove(result['auto_convert_target_format'])
if result['auto_convert_target_format'] in result['auto_import_ignored_formats']:
result['auto_import_ignored_formats'].remove(result['auto_convert_target_format'])
# DEBUGGING
with open("/config/post_request" ,"w") as f:
for key in result.keys():
if key == "cwa_ignored_formats":
if key == "auto_convert_ignored_formats" or key == "auto_import_ignored_formats":
f.write(f"{key} - {', '.join(result[key])}\n")
else:
f.write(f"{key} - {result[key]}\n")
@@ -76,14 +76,50 @@
font-size: inherit;
line-height: normal;
padding-left: 10px;
max-width: 90rem;">The formats selected here will be ignored by CWA's auto-conversion feature when it's active</p>
max-width: 90rem;">The formats selected here will be ignored by CWA's Auto-Conversion feature when it's active</p>
<div style="max-width: 90rem; padding-left: 30px;">
{% for format in ignorable_formats -%}
<label for="ignore_{{ format }}" style="width: 75px; padding-right: 6px;">
{% if format in cwa_settings['cwa_ignored_formats'] %}
<input type="checkbox" name="ignore_{{ format }}" value="{{ format }}" checked style="vertical-align: middle;">
<label for="ignore_convert_{{ format }}" style="width: 75px; padding-right: 6px;">
{% if format in cwa_settings['auto_convert_ignored_formats'] %}
{% if format == cwa_settings['auto_convert_target_format'] %}
<input type="checkbox" id="ignore_convert_{{ format }}" name="ignore_convert_{{ format }}" value="{{ format }}" disabled style="vertical-align: middle;">
{% else %}
<input type="checkbox" id="ignore_convert_{{ format }}" name="ignore_convert_{{ format }}" value="{{ format }}" checked style="vertical-align: middle;">
{% endif %}
{% else %}
<input type="checkbox" name="ignore_{{ format }}" value="{{ format }}" style="vertical-align: middle;">
{% if format == cwa_settings['auto_convert_target_format'] %}
<input type="checkbox" id="ignore_convert_{{ format }}" name="ignore_convert_{{ format }}" value="{{ format }}" disabled style="vertical-align: middle;">
{% else %}
<input type="checkbox" id="ignore_convert_{{ format }}" name="ignore_convert_{{ format }}" value="{{ format }}" style="vertical-align: middle;">
{% endif %}
{% endif %}
<span style="padding-left: 4px; vertical-align: middle;">{{ format }}</span>
</label>
{% endfor %}
</div>
<h3>CWA Auto-Import - Ignored Formats</h3>
<p style="color: whitesmoke;
font-style: italic;
font-size: inherit;
line-height: normal;
padding-left: 10px;
max-width: 90rem;">The formats selected here will be ignored by CWA's Auto-Import feature</p>
<div style="max-width: 90rem; padding-left: 30px;">
{% for format in ignorable_formats -%}
<label for="ignore_import_{{ format }}" style="width: 75px; padding-right: 6px;">
{% if format in cwa_settings['auto_import_ignored_formats'] %}
{% if format == cwa_settings['auto_convert_target_format'] %}
<input type="checkbox" id="ignore_import_{{ format }}" name="ignore_import_{{ format }}" value="{{ format }}" disabled style="vertical-align: middle;">
{% else %}
<input type="checkbox" id="ignore_import_{{ format }}" name="ignore_import_{{ format }}" value="{{ format }}" checked style="vertical-align: middle;">
{% endif %}
{% else %}
{% if format == cwa_settings['auto_convert_target_format'] %}
<input type="checkbox" id="ignore_import_{{ format }}" name="ignore_import_{{ format }}" value="{{ format }}" disabled style="vertical-align: middle;">
{% else %}
<input type="checkbox" id="ignore_import_{{ format }}" name="ignore_import_{{ format }}" value="{{ format }}" style="vertical-align: middle;">
{% endif %}
{% endif %}
<span style="padding-left: 4px; vertical-align: middle;">{{ format }}</span>
</label>
+48 -28
View File
@@ -26,7 +26,8 @@ class CWA_DB:
"cwa_update_notifications": 1,
"auto_convert": 1,
"auto_convert_target_format": "epub",
"cwa_ignored_formats":""}
"auto_convert_ignored_formats":"",
"auto_import_ignored_formats":""}
self.tables, self.schema = self.make_tables()
self.ensure_schema_match()
@@ -36,6 +37,7 @@ class CWA_DB:
self.cwa_settings = self.get_cwa_settings()
def temp_disable_split_library(self): # Temporary measure to disable split library functionality until it can be supported in V2.2.0
con = sqlite3.connect("/config/app.db")
cur = con.cursor()
@@ -63,6 +65,7 @@ class CWA_DB:
print("[cwa-db]: Connection with the CWA Enforcement DB Successful!")
return con, cur
def make_tables(self) -> None:
"""Creates the tables for the CWA DB if they don't already exist"""
schema = []
@@ -77,9 +80,11 @@ class CWA_DB:
tables[x] = tables[x] + ";"
for table in tables:
self.cur.execute(table)
self.con.commit()
return tables, schema
def ensure_schema_match(self) -> None:
self.cur.execute("SELECT * FROM cwa_settings")
cwa_setting_names = [header[0] for header in self.cur.description]
@@ -106,6 +111,7 @@ class CWA_DB:
self.con.commit()
print(f"[cwa_db] Deprecated setting found from previous version of CWA, deleting setting '{setting}' from cwa.db...")
def set_default_settings(self, force=False) -> None:
"""Sets default settings for new tables and keeps track if the user is using the default settings or not.\n\n
If the argument 'force' is set to True, the function instead sets all settings to their default values"""
@@ -113,39 +119,44 @@ class CWA_DB:
for setting in self.cwa_default_settings:
if type(self.cwa_default_settings[setting]) == int:
self.cur.execute(f"UPDATE cwa_settings SET {setting}={self.cwa_default_settings[setting]};")
self.con.commit()
else:
self.cur.execute(f'UPDATE cwa_settings SET {setting}="{self.cwa_default_settings[setting]}";')
print("[cwa-db] CWA Default Settings successfully applied.")
current_settings = self.cur.execute("SELECT * FROM cwa_settings")
setting_names = [header[0] for header in self.cur.description]
cwa_settings = [dict(zip(setting_names,row)) for row in self.cur.fetchall()][0]
if current_settings == []:
print("[cwa-db]: New DB detected, applying default CWA settings...")
self.con.commit()
print("[cwa-db] CWA Default Settings successfully applied!")
return
try:
self.cur.execute("SELECT * FROM cwa_settings")
setting_names = [header[0] for header in self.cur.description]
current_settings = [dict(zip(setting_names,row)) for row in self.cur.fetchall()][0]
except IndexError:
print("[cwa-db]: No existing CWA settings detected, applying default CWA settings...")
for setting in self.cwa_default_settings:
if type(self.cwa_default_settings[setting]) == int:
self.cur.execute(f"UPDATE cwa_settings SET {setting}={self.cwa_default_settings[setting]};")
else:
self.cur.execute(f'UPDATE cwa_settings SET {setting}="{self.cwa_default_settings[setting]}";')
else:
default_check = True
for setting in setting_names:
if setting == "default_settings":
continue
elif cwa_settings[setting] != self.cwa_default_settings[setting]:
default_check = False
self.cur.execute("UPDATE cwa_settings SET default_settings=0 WHERE default_settings=1;")
self.con.commit()
break
if default_check:
self.cur.execute("UPDATE cwa_settings SET default_settings=1 WHERE default_settings=0;")
self.con.commit()
print("[cwa-db] CWA Default Settings successfully applied!")
return
default_check = True
for setting in setting_names:
if setting == "default_settings":
continue
elif current_settings[setting] != self.cwa_default_settings[setting]:
default_check = False
self.cur.execute("UPDATE cwa_settings SET default_settings=0 WHERE default_settings=1;")
self.con.commit()
break
if default_check:
self.cur.execute("UPDATE cwa_settings SET default_settings=1 WHERE default_settings=0;")
self.con.commit()
if self.verbose:
print("[cwa-db] CWA Settings loaded successfully")
if self.verbose:
print("[cwa-db] CWA Settings loaded successfully")
def get_cwa_settings(self) -> dict[str:bool|str]:
"""Gets the current cwa_settings values from the table of the same name in cwa.db and returns them as a dict"""
@@ -156,14 +167,16 @@ class CWA_DB:
for header in headers:
if type(cwa_settings[header]) == int:
cwa_settings[header] = bool(cwa_settings[header])
cwa_settings['cwa_ignored_formats'] = cwa_settings['cwa_ignored_formats'].split(',')
cwa_settings['auto_convert_ignored_formats'] = cwa_settings['auto_convert_ignored_formats'].split(',')
cwa_settings['auto_import_ignored_formats'] = cwa_settings['auto_import_ignored_formats'].split(',')
return cwa_settings
def update_cwa_settings(self, result) -> None:
"""Sets settings using POST request from set_cwa_settings()"""
for setting in result.keys():
if setting == "cwa_ignored_formats":
if setting == "auto_convert_ignored_formats" or setting == "auto_import_ignored_formats":
result[setting] = ','.join(result[setting])
if type(result[setting]) == int:
@@ -173,21 +186,25 @@ class CWA_DB:
self.con.commit()
self.set_default_settings()
def enforce_add_entry_from_log(self, log_info: dict):
"""Adds an entry to the db from a change log file"""
self.cur.execute("INSERT INTO cwa_enforcement(timestamp, book_id, book_title, author, epub_path, trigger_type) VALUES (?, ?, ?, ?, ?, ?);", (log_info['timestamp'], log_info['book_id'], log_info['book_title'], log_info['author_name'], log_info['epub_path'], 'auto -log'))
self.con.commit()
def enforce_add_entry_from_dir(self, book_info: dict):
"""Adds an entry to the db when cover-enforcer is ran with a directory"""
self.cur.execute("INSERT INTO cwa_enforcement(timestamp, book_id, book_title, author, epub_path, trigger_type) VALUES (?, ?, ?, ?, ?, ?);", (book_info['timestamp'], book_info['book_id'], book_info['book_title'], book_info['author_name'], book_info['epub_path'], 'manual -dir'))
self.con.commit()
def enforce_add_entry_from_all(self, book_info: dict):
"""Adds an entry to the db when cover-enforcer is ran with the -all flag"""
self.cur.execute("INSERT INTO cwa_enforcement(timestamp, book_id, book_title, author, epub_path, trigger_type) VALUES (?, ?, ?, ?, ?, ?);", (book_info['timestamp'], book_info['book_id'], book_info['book_title'], book_info['author_name'], book_info['epub_path'], 'manual -all'))
self.con.commit()
def enforce_show(self, paths: bool, verbose: bool, web_ui=False):
results_no_path = self.cur.execute("SELECT timestamp, book_id, book_title, author, trigger_type FROM cwa_enforcement ORDER BY timestamp DESC;").fetchall()
results_with_path = self.cur.execute("SELECT timestamp, book_id, epub_path FROM cwa_enforcement ORDER BY timestamp DESC;").fetchall()
@@ -246,6 +263,7 @@ class CWA_DB:
break
return newest_ten, headers
def get_conversion_history(self, verbose: bool):
headers = ["Timestamp", "Filename", "Original Format", "Original Backed Up?"]
results = self.cur.execute("SELECT timestamp, filename, original_format, original_backed_up FROM cwa_conversions ORDER BY timestamp DESC;").fetchall()
@@ -262,11 +280,13 @@ class CWA_DB:
break
return newest_ten, headers
def import_add_entry(self, filename, original_backed_up):
timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
self.cur.execute("INSERT INTO cwa_import(timestamp, filename, original_backed_up) VALUES (?, ?, ?);", (timestamp, filename, original_backed_up))
self.con.commit()
def conversion_add_entry(self, filename, original_format, original_backed_up):
timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
self.cur.execute("INSERT INTO cwa_conversions(timestamp, filename, original_format, original_backed_up) VALUES (?, ?, ?, ?);", (timestamp, filename, original_format, original_backed_up))
+2 -1
View File
@@ -28,5 +28,6 @@ CREATE TABLE IF NOT EXISTS cwa_settings(
cwa_update_notifications SMALLINT DEFAULT 1 NOT NULL,
auto_convert SMALLINT DEFAULT 1 NOT NULL,
auto_convert_target_format TEXT DEFAULT "epub" NOT NULL,
cwa_ignored_formats TEXT DEFAULT "" NOT NULL
auto_convert_ignored_formats TEXT DEFAULT "" NOT NULL,
auto_import_ignored_formats TEXT DEFAULT "" NOT NULL
);