From 6bc6acf0e19ceb2e663eb2ced5c2d92601cc0342 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Sun, 15 Sep 2024 12:35:19 +0200 Subject: [PATCH] Continued: - instances.delete() now has an optional parameter 'column' which also needs validation on type and non-empty string - some functions in same module didn't log optional/extra parameter 'column' --- fba/models/instances.py | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/fba/models/instances.py b/fba/models/instances.py index 2fe116d..45d2fb6 100644 --- a/fba/models/instances.py +++ b/fba/models/instances.py @@ -543,7 +543,8 @@ def set_software(domain: str, software: str) -> None: logger.debug("EXIT!") def valid(value: str, column: str) -> bool: - logger.debug("value='%s' - CALLED!", value) + logger.debug("value='%s',column='%s' - CALLED!", value, column) + if not isinstance(value, str): raise ValueError(f"Parameter value[]='{type(value)}' is not of type 'str'") elif value == "": @@ -563,11 +564,21 @@ def valid(value: str, column: str) -> bool: logger.debug("is_valid='%s' - EXIT!", is_valid) return is_valid -def delete(domain: str) -> None: - logger.debug("domain='%s' - CALLED!", domain) - domain_helper.raise_on(domain) +def delete(domain: str, column: str = "domain") -> None: + logger.debug("domain='%s, column='%s'' - CALLED!", domain, column) + + if not isinstance(domain, str): + raise ValueError(f"Parameter domain[]='{type(domain)}' is not of type 'str'") + elif domain == "": + raise ValueError("Parameter 'domain' is empty") + elif not isinstance(column, str): + raise ValueError(f"Parameter column[]='{type(column)}' is not of type 'str'") + elif column == "": + raise ValueError("Parameter 'column' is empty") + elif column in ["domain", "origin"]: + domain_helper.raise_on(domain) - database.cursor.execute(f"DELETE FROM instances WHERE domain = ? LIMIT 1", [domain]) + database.cursor.execute(f"DELETE FROM instances WHERE {column} = ? LIMIT 1", [domain]) logger.debug("Invoking commit() ...") database.connection.commit() @@ -575,7 +586,7 @@ def delete(domain: str) -> None: logger.debug("EXIT!") def translate_idnas(rows: list, column: str) -> None: - logger.debug("rows[]='%s' - CALLED!", type(rows)) + logger.debug("rows[]='%s',column='%s' - CALLED!", type(rows), column) if not isinstance(rows, list): raise ValueError("rows[]='{type(rows)}' is not of type 'list'") @@ -617,7 +628,7 @@ def translate_idnas(rows: list, column: str) -> None: logger.info("punycode row[%s]='%s' to '%s'", column, row[column], punycode) if is_registered(punycode, True): logger.warning("Deleting row[%s]='%s' as punycode='%s' already exist", column, row[column], punycode) - database.cursor.execute(f"DELETE FROM instances WHERE {column} = ? LIMIT 1", [row[column]]) + delete(row[column], column) else: logger.debug("Updating row[%s]='%s' to punycode='%s' ...", column, row[column], punycode) database.cursor.execute(f"UPDATE instances SET {column} = ? WHERE {column} = ? LIMIT 1", [punycode, row[column]]) -- 2.39.5