From 08819af6d1043eb489616c25592ab324e73627f9 Mon Sep 17 00:00:00 2001 From: ai-bot Date: Fri, 2 Oct 2026 12:23:23 +0000 Subject: [PATCH] database.py: list_previous_values for placeholders and carried values (section 20) --- database.py | 50 +++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 45 insertions(+), 5 deletions(-) diff --git a/database.py b/database.py index 8da0ddd..fa19ab1 100644 --- a/database.py +++ b/database.py @@ -106,10 +106,10 @@ SECURITY_STATUS_MIGRATION = ( ("SÃ…RBAR", "VULNERABLE"), ("KOMPROMITTERT", "COMPROMISED"), ) -# The newest saved check per key, used by the dashboard. Duplicated on purpose so -# the query stays readable without a window function. +# The newest saved check per key, used by the dashboard and the form. Duplicated +# on purpose so the queries stay readable without a window function. _LATEST_CHECK_QUERY = """ - SELECT hc.{column} + SELECT {expression} FROM key_checks AS kc JOIN health_checks AS hc ON hc.id = kc.health_check_id WHERE kc.key_id = k.id @@ -446,8 +446,8 @@ def list_key_status(database_path="data/helsesjekk.sqlite3"): rows = connection.execute( f""" SELECT k.id AS key_id, k.key_number, k.name, k.type, k.device, - ({_LATEST_CHECK_QUERY.format(column="checked_at")}) AS last_checked_at, - ({_LATEST_CHECK_QUERY.format(column="next_check_due")}) AS next_check_due + ({_LATEST_CHECK_QUERY.format(expression="hc.checked_at")}) AS last_checked_at, + ({_LATEST_CHECK_QUERY.format(expression="hc.next_check_due")}) AS next_check_due FROM keys AS k ORDER BY k.key_number """ @@ -457,6 +457,46 @@ def list_key_status(database_path="data/helsesjekk.sqlite3"): connection.close() +def list_previous_values(database_path="data/helsesjekk.sqlite3"): + """Return the most recently saved values, for pre-filling the form. + + ``keys`` carries the last recorded firmware version, address path, wallet + software, and software version for each key. ``security`` carries the last + recorded security status for each provider, whatever key recorded it. Both + are used for the placeholders and for carrying hidden fields forward. + """ + connection = sqlite3.connect(database_path, timeout=5) + connection.row_factory = sqlite3.Row + try: + keys = [ + dict(row) + for row in connection.execute( + f""" + SELECT k.id AS key_id, + ({_LATEST_CHECK_QUERY.format(expression="kc.firmware_version")}) AS firmware_version, + ({_LATEST_CHECK_QUERY.format(expression="kc.address_path")}) AS address_path, + ({_LATEST_CHECK_QUERY.format(expression="hc.wallet_software")}) AS wallet_software, + ({_LATEST_CHECK_QUERY.format(expression="hc.software_version")}) AS software_version + FROM keys AS k + ORDER BY k.key_number + """ + ).fetchall() + ] + security = {} + for row in connection.execute( + """ + SELECT acs.provider, acs.status + FROM ai_security_checks AS acs + JOIN health_checks AS hc ON hc.id = acs.health_check_id + ORDER BY hc.checked_at DESC, hc.id DESC, acs.id DESC + """ + ): + security.setdefault(row["provider"], row["status"]) + return {"keys": keys, "security": security} + finally: + connection.close() + + def list_export_rows(database_path="data/helsesjekk.sqlite3"): """Return one row per key check for the report export, oldest first.