diff --git a/CHANGELOG.md b/CHANGELOG.md index fb71c8b905bed071cee16167142fab0a0eae215d..58ebc532bdb172cf17be6b40955719290df5af9e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] ## ### Added ### +- `create_entity_link` function to create html links to entities; useful for logging ### Changed ### diff --git a/src/caosadvancedtools/crawler.py b/src/caosadvancedtools/crawler.py index 085cd8d27f261644b38061d26fb10e37ac5465fd..7b81b81ee9a717c226de7f3f3c0e5f28e3e6e789 100644 --- a/src/caosadvancedtools/crawler.py +++ b/src/caosadvancedtools/crawler.py @@ -58,6 +58,7 @@ from .guard import RETRIEVE, ProhibitedException from .guard import global_guard as guard from .serverside.helper import send_mail as main_send_mail from .suppressKnown import SuppressKnown +from .utils import create_entity_link logger = logging.getLogger(__name__) @@ -103,15 +104,8 @@ def apply_list_of_updates(to_be_updated, update_flags={}, info = "UPDATE: updating the following entities\n" - baseurl = db.configuration.get_config()["Connection"]["url"] - - def make_clickable(txt, id): - return "<a href='{}/Entity/{}'>{}</a>".format(baseurl, id, txt) - for el in to_be_updated: - info += str("\t" + make_clickable(el.name, el.id) - if el.name is not None - else "\t" + make_clickable(str(el.id), el.id)) + info += str("\t" + create_entity_link(el)) info += "\n" logger.info(info) diff --git a/src/caosadvancedtools/utils.py b/src/caosadvancedtools/utils.py index 2504f56976e2f6122f3e3468db1c7ae807bbb8cd..4d6a4b36bcb5dbdcd5bfe9357c53d4e9aa3501ca 100644 --- a/src/caosadvancedtools/utils.py +++ b/src/caosadvancedtools/utils.py @@ -55,6 +55,26 @@ def replace_path_prefix(path, old_prefix, new_prefix): return os.path.join(new_prefix, path) +def create_entity_link(entity: db.Entity, base_url: str = ""): + """ + creates a string that contains the code for an html link to the provided entity. + + The text of the link is the entity name if one exists and the id otherwise. + + Args: + entity (db.Entity): the entity object to which the link will point + base_url (str): optional, by default, the url starts with '/Entity' and thus is relative. + You can provide a base url that will be prefixed. + Returns: + str: the string containing the html code + + """ + return "<a href='{}/Entity/{}'>{}</a>".format( + base_url, + entity.id, + entity.name if entity.name is not None else entity.id) + + def string_to_person(person): """ Creates a Person Record from a string. diff --git a/unittests/test_utils.py b/unittests/test_utils.py index 7369931799b00eba5a835458a6fad474de1d9039..468e9200de723c65c75e21912b5b3940d758821c 100644 --- a/unittests/test_utils.py +++ b/unittests/test_utils.py @@ -26,7 +26,7 @@ from tempfile import NamedTemporaryFile import caosdb as db from caosadvancedtools.utils import (check_win_path, get_referenced_files, - string_to_person) + string_to_person, create_entity_link) from caosdb import RecordType, configure_connection, get_config from caosdb.connection.mockup import MockUpResponse, MockUpServerConnection from caosdb.exceptions import TransactionError @@ -140,3 +140,8 @@ class PathTest(unittest.TestCase): assert check_win_path(r"C:\hallo") assert check_win_path(r"\hallo") assert not check_win_path("/hallo") + + +class EntityLinkTest(unittest.TestCase): + def test_link(self): + assert "<a href='/Entity/1'>a</a>" == create_entity_link(db.Entity(id=1, name='a'))