Skip to content

Commit

Permalink
- Fixed numerous issues with the cwa_db that were resulting in the db…
Browse files Browse the repository at this point in the history
… 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
  • Loading branch information
crocodilestick committed Nov 15, 2024
1 parent d6b2caa commit fc18887
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 42 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ DEV
cwa.db

# Dev files
dev_changelog.md
changelogs/
*.txz

# Distribution / packaging
Expand Down
27 changes: 20 additions & 7 deletions root/app/calibre-web/cps/cwa_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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")
Expand Down
46 changes: 41 additions & 5 deletions root/app/calibre-web/cps/templates/cwa_settings.html
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,50 @@ <h3>CWA Auto-Convert - Ignored Formats</h3>
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>
Expand Down
76 changes: 48 additions & 28 deletions scripts/cwa_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ def __init__(self, verbose=False):
"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()
Expand All @@ -36,6 +37,7 @@ def __init__(self, verbose=False):

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()
Expand Down Expand Up @@ -63,6 +65,7 @@ def connect_to_db(self) -> tuple[sqlite3.Connection, sqlite3.Cursor] | None:
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 = []
Expand All @@ -77,9 +80,11 @@ def make_tables(self) -> None:
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]
Expand All @@ -106,46 +111,52 @@ def ensure_schema_match(self) -> None:
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"""
if force:
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"""
Expand All @@ -156,14 +167,16 @@ def get_cwa_settings(self) -> dict[str:bool|str]:
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:
Expand All @@ -173,21 +186,25 @@ def update_cwa_settings(self, result) -> None:
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()
Expand Down Expand Up @@ -246,6 +263,7 @@ def get_import_history(self, verbose: bool):
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()
Expand All @@ -262,11 +280,13 @@ def get_conversion_history(self, verbose: bool):
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))
Expand Down
3 changes: 2 additions & 1 deletion scripts/cwa_schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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
);

0 comments on commit fc18887

Please sign in to comment.