From 7d3fa9f2fb32a5b2e0b37d573b5badfb2a52a71c Mon Sep 17 00:00:00 2001 From: Mark Jordan Date: Tue, 2 Jul 2024 08:07:40 -0700 Subject: [PATCH 1/4] WIP on #337. --- WorkbenchConfig.py | 1 + workbench_utils.py | 12 +++++++++--- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/WorkbenchConfig.py b/WorkbenchConfig.py index e30e7d8..26344f2 100644 --- a/WorkbenchConfig.py +++ b/WorkbenchConfig.py @@ -198,6 +198,7 @@ def get_default_config(self): "paged_content_from_directories": False, "delete_media_with_nodes": True, "allow_adding_terms": False, + "columns_with_term_names": [], "nodes_only": False, "log_response_time": False, "adaptive_pause_threshold": 2, diff --git a/workbench_utils.py b/workbench_utils.py index b6f6197..31c0686 100644 --- a/workbench_utils.py +++ b/workbench_utils.py @@ -6542,7 +6542,7 @@ def prepare_term_id(config, vocab_ids, field_name, term): """ term = str(term) term = term.strip() - if value_is_numeric(term): + if value_is_numeric(term) and field_name not in config["columns_with_term_names"]: return term entity_reference_view_endpoints = get_entity_reference_view_endpoints(config) if not entity_reference_view_endpoints and vocab_ids is False: @@ -8043,7 +8043,10 @@ def validate_taxonomy_reference_value( ) # Check to see if field_value is a member of the field's vocabularies. First, check whether field_value is a term ID. - if value_is_numeric(field_value): + if ( + value_is_numeric(field_value) + and csv_field_name not in config["columns_with_term_names"] + ): field_value = field_value.strip() term_in_vocabs = False for vocab_id in this_fields_vocabularies: @@ -8125,7 +8128,10 @@ def validate_taxonomy_reference_value( new_terms_to_add = [] for vocabulary in this_fields_vocabularies: tid = find_term_in_vocab(config, vocabulary, field_value) - if value_is_numeric(tid) is False: + if ( + value_is_numeric(tid) is False + or csv_field_name in config["columns_with_term_names"] + ): # Single taxonomy fields. if len(this_fields_vocabularies) == 1: if config["allow_adding_terms"] is True: From 2c21a4f79c4524c772562b150d766611c0b756f3 Mon Sep 17 00:00:00 2001 From: Mark Jordan Date: Tue, 2 Jul 2024 08:51:05 -0700 Subject: [PATCH 2/4] Fixed tests as part of WIP on #337. --- tests/field_tests.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/tests/field_tests.py b/tests/field_tests.py index dd8df5b..81df146 100644 --- a/tests/field_tests.py +++ b/tests/field_tests.py @@ -3189,7 +3189,12 @@ class TestEntityRefererenceField(unittest.TestCase): def setUp(self): self.maxDiff = None - self.config = {"task": "create", "subdelimiter": "|", "id_field": "id"} + self.config = { + "task": "create", + "subdelimiter": "|", + "id_field": "id", + "columns_with_term_names": [], + } def test_create_with_entity_reference_field(self): existing_node = { @@ -4392,7 +4397,12 @@ class TestTypedRelationField(unittest.TestCase): def setUp(self): self.maxDiff = None - self.config = {"task": "create", "subdelimiter": "|", "id_field": "id"} + self.config = { + "task": "create", + "subdelimiter": "|", + "id_field": "id", + "columns_with_term_names": [], + } self.existing_node = { "type": [{"target_id": "islandora_object", "target_type": "node_type"}], From cdf4f863ec0cd32016b028b8ae03b23aa6875b3f Mon Sep 17 00:00:00 2001 From: Mark Jordan Date: Tue, 2 Jul 2024 09:41:27 -0700 Subject: [PATCH 3/4] WIP on #752. --- WorkbenchConfig.py | 1 + workbench_utils.py | 51 +++++++++++++++++++++++++++++++++++++--------- 2 files changed, 42 insertions(+), 10 deletions(-) diff --git a/WorkbenchConfig.py b/WorkbenchConfig.py index 26344f2..241bea6 100644 --- a/WorkbenchConfig.py +++ b/WorkbenchConfig.py @@ -199,6 +199,7 @@ def get_default_config(self): "delete_media_with_nodes": True, "allow_adding_terms": False, "columns_with_term_names": [], + "protected_vocabularies": [], "nodes_only": False, "log_response_time": False, "adaptive_pause_threshold": 2, diff --git a/workbench_utils.py b/workbench_utils.py index 31c0686..977020e 100644 --- a/workbench_utils.py +++ b/workbench_utils.py @@ -6208,6 +6208,11 @@ def create_term(config, vocab_id, term_name, term_csv_row=None): string|boolean The term ID, or False term was not created. """ + if vocab_id in config["protected_vocabularies"]: + logging.warning( + f'Term "{term_name}" is not in its designated vocabulary ({vocab_id}) and will not be added since the vocabulary is registered in the "protected_vocabularies" config setting.' + ) + return False # Check to see if term exists; if so, return its ID, if not, proceed to create it. tid = find_term_in_vocab(config, vocab_id, term_name) if value_is_numeric(tid): @@ -8150,11 +8155,24 @@ def validate_taxonomy_reference_value( + field_value.strip() + '") that is ' ) - message_2 = ( - 'not in the referenced vocabulary ("' - + this_fields_vocabularies[0] - + '"). That term will be created.' - ) + + if ( + this_fields_vocabularies[0] + in config["protected_vocabularies"] + ): + message_2 = ( + 'not in the referenced vocabulary ("' + + this_fields_vocabularies[0] + + '"). The term will not be created since "' + + this_fields_vocabularies[0] + + '" is registered in the "protected_vocabularies" config setting.' + ) + else: + message_2 = ( + 'not in the referenced vocabulary ("' + + this_fields_vocabularies[0] + + '"). That term will be created.' + ) if config["validate_terms_exist"] is True: logging.warning(message + message_2) else: @@ -8223,11 +8241,24 @@ def validate_taxonomy_reference_value( + namespaced_term_name.strip() + '") that is ' ) - message_2 = ( - 'not in the referenced vocabulary ("' - + namespace_vocab_id - + '"). That term will be created.' - ) + + if ( + namespace_vocab_id + in config["protected_vocabularies"] + ): + message_2 = ( + 'not in the referenced vocabulary ("' + + namespace_vocab_id + + '"). The term will not be created since "' + + namespace_vocab_id + + '" is registered in the "protected_vocabularies" config setting.' + ) + else: + message_2 = ( + 'not in the referenced vocabulary ("' + + namespace_vocab_id + + '"). That term will be created.' + ) if config["validate_terms_exist"] is True: logging.warning(message + message_2) new_terms_to_add.append(split_field_value) From 1a35c45d6268cdcea0ee8b1a73c770a221881e7c Mon Sep 17 00:00:00 2001 From: Mark Jordan Date: Thu, 4 Jul 2024 08:14:31 -0700 Subject: [PATCH 4/4] WIP on #337. --- workbench_utils.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/workbench_utils.py b/workbench_utils.py index 977020e..875571b 100644 --- a/workbench_utils.py +++ b/workbench_utils.py @@ -6236,7 +6236,7 @@ def create_term(config, vocab_id, term_name, term_csv_row=None): if config["allow_adding_terms"] is False: logging.warning( - 'To create new taxonomy terms, you must add "allow_adding_terms: true" to your configuration file.' + f'Term "{term_name}" does not exist in the vocabulary "{vocab_id}". To create new taxonomy terms, you must add "allow_adding_terms: true" to your configuration file.' ) return False @@ -8133,10 +8133,7 @@ def validate_taxonomy_reference_value( new_terms_to_add = [] for vocabulary in this_fields_vocabularies: tid = find_term_in_vocab(config, vocabulary, field_value) - if ( - value_is_numeric(tid) is False - or csv_field_name in config["columns_with_term_names"] - ): + if value_is_numeric(tid) is False: # Single taxonomy fields. if len(this_fields_vocabularies) == 1: if config["allow_adding_terms"] is True: