From fa66260aee9df76e5eccf90278d7881c6bcdec48 Mon Sep 17 00:00:00 2001 From: Cecilie Seim <68303562+tilen1976@users.noreply.github.com> Date: Mon, 29 Apr 2024 14:54:18 +0200 Subject: [PATCH] Inherit dataset value - multilanguage (#326) * add population description - issue inherits both ways * Method for multilanguage updates only one value in fieldset * add TODO test * Test multilanguage can inherit and can be changed for each variable * update test multilanguage * replace method with too many branches with dict * Update test names --- src/datadoc/frontend/callbacks/dataset.py | 3 + src/datadoc/frontend/callbacks/variables.py | 32 ++++++++ .../callbacks/test_variables_callbacks.py | 79 ++++++++++++++++++- 3 files changed, 113 insertions(+), 1 deletion(-) diff --git a/src/datadoc/frontend/callbacks/dataset.py b/src/datadoc/frontend/callbacks/dataset.py index 692821c8..832ba396 100644 --- a/src/datadoc/frontend/callbacks/dataset.py +++ b/src/datadoc/frontend/callbacks/dataset.py @@ -16,6 +16,7 @@ from datadoc.frontend.callbacks.utils import find_existing_language_string from datadoc.frontend.callbacks.utils import get_dataset_path from datadoc.frontend.callbacks.utils import parse_and_validate_dates +from datadoc.frontend.callbacks.variables import set_variables_value_multilanguage from datadoc.frontend.callbacks.variables import ( set_variables_values_inherited_from_dataset, ) @@ -117,6 +118,7 @@ def process_keyword(value: str) -> list[str]: return [item.strip() for item in value.split(",")] +# legge inn population update her? def process_special_cases( value: MetadataInputTypes | LanguageStringType, metadata_identifier: str, @@ -147,6 +149,7 @@ def process_special_cases( metadata_identifier, language, ) + set_variables_value_multilanguage(value, metadata_identifier, language) elif metadata_identifier in DROPDOWN_DATASET_METADATA_IDENTIFIERS and value == "": updated_value = None else: diff --git a/src/datadoc/frontend/callbacks/variables.py b/src/datadoc/frontend/callbacks/variables.py index 6637f312..60148719 100644 --- a/src/datadoc/frontend/callbacks/variables.py +++ b/src/datadoc/frontend/callbacks/variables.py @@ -247,6 +247,38 @@ def variable_identifier( return metadata_identifiers.get(dataset_identifier) +def variable_identifier_multilanguage( + dataset_identifier: str, +) -> str | None: + """Pair corresponding identifiers for multilanguage fields.""" + metadata_identifiers = { + "population_description": VariableIdentifiers.POPULATION_DESCRIPTION, + } + return metadata_identifiers.get(dataset_identifier) + + +def set_variables_value_multilanguage( + value: MetadataInputTypes | LanguageStringType, + metadata_identifier: str, + language: str, +) -> None: + """Set variable multilanguage value based on dataset value.""" + variable = variable_identifier_multilanguage(metadata_identifier) + if value is not None and variable is not None: + for val in state.metadata.variables: + update_value = handle_multi_language_metadata( + variable, + value, + val.short_name, + language, + ) + setattr( + state.metadata.variables_lookup[val.short_name], + variable, + update_value, + ) + + def set_variables_values_inherited_from_dataset( value: MetadataInputTypes | LanguageStringType, metadata_identifier: str, diff --git a/tests/frontend/callbacks/test_variables_callbacks.py b/tests/frontend/callbacks/test_variables_callbacks.py index 8a633c7e..1705548a 100644 --- a/tests/frontend/callbacks/test_variables_callbacks.py +++ b/tests/frontend/callbacks/test_variables_callbacks.py @@ -8,6 +8,7 @@ import arrow import pytest +from datadoc_model.model import LanguageStringTypeItem from pydantic_core import Url from datadoc import enums @@ -15,10 +16,12 @@ from datadoc.frontend.callbacks.variables import accept_variable_metadata_date_input from datadoc.frontend.callbacks.variables import accept_variable_metadata_input from datadoc.frontend.callbacks.variables import populate_variables_workspace +from datadoc.frontend.callbacks.variables import set_variables_value_multilanguage from datadoc.frontend.callbacks.variables import ( set_variables_values_inherited_from_dataset, ) from datadoc.frontend.fields.display_base import get_metadata_and_stringify +from datadoc.frontend.fields.display_base import get_standard_metadata from datadoc.frontend.fields.display_dataset import DatasetIdentifiers from datadoc.frontend.fields.display_variables import DISPLAY_VARIABLES from datadoc.frontend.fields.display_variables import VariableIdentifiers @@ -396,7 +399,7 @@ def test_variables_values_inherit_dataset_values( ), ], ) -def test_variables_values_can_be_changed_after_update_from_dataset_value( +def test_variables_values_can_be_changed_after_inherit_dataset_value( dataset_value, dataset_identifier, variable_identifier, @@ -438,3 +441,77 @@ def test_variables_values_can_be_changed_after_update_from_dataset_value( ) == update_value ) + + +def test_variables_values_multilanguage_inherit_dataset_values( + metadata: DataDocMetadata, +): + state.metadata = metadata + dataset_population_description = "Personer bosatt i Norge" + dataset_population_description_language_item = [ + LanguageStringTypeItem( + languageCode="nb", + languageText="Personer bosatt i Norge", + ), + ] + metadata_identifier = DatasetIdentifiers.POPULATION_DESCRIPTION + language = "nb" + setattr( + state.metadata.dataset, + metadata_identifier, + dataset_population_description_language_item, + ) + set_variables_value_multilanguage( + dataset_population_description, + metadata_identifier, + language, + ) + for val in state.metadata.variables: + assert metadata.dataset.population_description == get_standard_metadata( + metadata.variables_lookup[val.short_name], + VariableIdentifiers.POPULATION_DESCRIPTION.value, + ) + + +def test_variables_values_multilanguage_can_be_changed_after_inherit_dataset_value( + metadata: DataDocMetadata, +): + state.metadata = metadata + dataset_population_description = "Persons in Norway" + dataset_population_description_language_item = [ + LanguageStringTypeItem(languageCode="en", languageText="Persons in Norway"), + ] + dataset_identifier = DatasetIdentifiers.POPULATION_DESCRIPTION + variables_identifier = VariableIdentifiers.POPULATION_DESCRIPTION + language = "en" + setattr( + state.metadata.dataset, + dataset_identifier, + dataset_population_description_language_item, + ) + set_variables_value_multilanguage( + dataset_population_description, + dataset_identifier, + language, + ) + for val in state.metadata.variables: + assert metadata.dataset.population_description == get_standard_metadata( + metadata.variables_lookup[val.short_name], + variables_identifier, + ) + variables_language_item = [ + LanguageStringTypeItem(languageCode="en", languageText="Persons in Sweden"), + ] + setattr( + state.metadata.variables_lookup["pers_id"], + variables_identifier, + variables_language_item, + ) + assert metadata.dataset.population_description != get_standard_metadata( + metadata.variables_lookup["pers_id"], + variables_identifier, + ) + assert metadata.dataset.population_description == get_standard_metadata( + metadata.variables_lookup["sivilstand"], + variables_identifier, + )