diff --git a/src/datadoc/frontend/callbacks/register_callbacks.py b/src/datadoc/frontend/callbacks/register_callbacks.py index 3283ed9c..a38918e8 100644 --- a/src/datadoc/frontend/callbacks/register_callbacks.py +++ b/src/datadoc/frontend/callbacks/register_callbacks.py @@ -25,9 +25,8 @@ from datadoc.frontend.callbacks.utils import update_global_language_state 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.components.builders import build_dataset_edit_section -from datadoc.frontend.components.builders import build_edit_section -from datadoc.frontend.components.builders import build_ssb_accordion from datadoc.frontend.components.dataset_tab import SECTION_WRAPPER_ID from datadoc.frontend.components.variables_tab import ACCORDION_WRAPPER_ID from datadoc.frontend.components.variables_tab import VARIABLES_INFORMATION_ID @@ -39,8 +38,6 @@ from datadoc.frontend.fields.display_dataset import OBLIGATORY_EDITABLE_DATASET_METADATA from datadoc.frontend.fields.display_dataset import OPTIONAL_DATASET_METADATA from datadoc.frontend.fields.display_dataset import DatasetIdentifiers -from datadoc.frontend.fields.display_variables import OBLIGATORY_VARIABLES_METADATA -from datadoc.frontend.fields.display_variables import OPTIONAL_VARIABLES_METADATA from datadoc.frontend.fields.display_variables import VariableIdentifiers if TYPE_CHECKING: @@ -143,39 +140,24 @@ def callback_populate_variables_info_section( @app.callback( Output(ACCORDION_WRAPPER_ID, "children"), Input("language-dropdown", "value"), + Input("search-variables", "value"), prevent_initial_call=True, ) def callback_populate_variables_workspace( language: str, + search_query: str, ) -> list: - """Create variable workspace with accordions for variables.""" + """Create variable workspace with accordions for variables. + + Allows for filtering which variables are displayed via the search box. + """ update_global_language_state(SupportedLanguages(language)) - logger.info("Populating new variables workspace") - return [ - build_ssb_accordion( - variable.short_name, - { - "type": "variables-accordion", - "id": f"{variable.short_name}-{language}", # Insert language into the ID to invalidate browser caches - }, - variable.short_name, - children=[ - build_edit_section( - OBLIGATORY_VARIABLES_METADATA, - "Obligatorisk", - variable, - state.current_metadata_language.value, - ), - build_edit_section( - OPTIONAL_VARIABLES_METADATA, - "Anbefalt", - variable, - state.current_metadata_language.value, - ), - ], - ) - for variable in list(state.metadata.variables) - ] + logger.debug("Populating variables workspace. Search query: %s", search_query) + return populate_variables_workspace( + state.metadata.variables, + state.current_metadata_language, + search_query, + ) @app.callback( Output(SECTION_WRAPPER_ID, "children"), @@ -189,7 +171,7 @@ def callback_populate_dataset_workspace( ) -> list: """Create dataset workspace with sections.""" update_global_language_state(SupportedLanguages(language)) - logger.info("Populating new dataset workspace") + logger.debug("Populating dataset workspace") if n_clicks: return [ build_dataset_edit_section( diff --git a/src/datadoc/frontend/callbacks/variables.py b/src/datadoc/frontend/callbacks/variables.py index b37696cf..8ab12bfd 100644 --- a/src/datadoc/frontend/callbacks/variables.py +++ b/src/datadoc/frontend/callbacks/variables.py @@ -11,17 +11,61 @@ from datadoc.frontend.callbacks.utils import MetadataInputTypes from datadoc.frontend.callbacks.utils import find_existing_language_string from datadoc.frontend.callbacks.utils import parse_and_validate_dates +from datadoc.frontend.components.builders import build_edit_section +from datadoc.frontend.components.builders import build_ssb_accordion from datadoc.frontend.fields.display_variables import ( MULTIPLE_LANGUAGE_VARIABLES_METADATA, ) +from datadoc.frontend.fields.display_variables import OBLIGATORY_VARIABLES_METADATA +from datadoc.frontend.fields.display_variables import OPTIONAL_VARIABLES_METADATA from datadoc.frontend.fields.display_variables import VariableIdentifiers if TYPE_CHECKING: + from datadoc_model import model from datadoc_model.model import LanguageStringType + from datadoc.enums import SupportedLanguages + logger = logging.getLogger(__name__) +def populate_variables_workspace( + variables: list[model.Variable], + language: SupportedLanguages, + search_query: str, +) -> list: + """Create variable workspace with accordions for variables. + + Allows for filtering which variables are displayed via the search box. + """ + return [ + build_ssb_accordion( + variable.short_name or "", + { + "type": "variables-accordion", + "id": f"{variable.short_name}-{language.value}", # Insert language into the ID to invalidate browser caches + }, + variable.short_name or "", + children=[ + build_edit_section( + OBLIGATORY_VARIABLES_METADATA, + "Obligatorisk", + variable, + language.value, + ), + build_edit_section( + OPTIONAL_VARIABLES_METADATA, + "Anbefalt", + variable, + language.value, + ), + ], + ) + for variable in variables + if search_query in (variable.short_name or "") + ] + + def handle_multi_language_metadata( metadata_field: str, new_value: MetadataInputTypes | LanguageStringType, diff --git a/src/datadoc/frontend/components/variables_tab.py b/src/datadoc/frontend/components/variables_tab.py index 85644d51..b5a500b9 100644 --- a/src/datadoc/frontend/components/variables_tab.py +++ b/src/datadoc/frontend/components/variables_tab.py @@ -36,8 +36,8 @@ def build_variables_tab() -> dbc.Tab: ssb.Input( label="Søk i variabler", searchField=True, - disabled=True, - placeholder="Kommer...", + disabled=False, + placeholder="Variabel kortnavn...", id="search-variables", n_submit=0, value="", diff --git a/tests/frontend/callbacks/test_variables_callbacks.py b/tests/frontend/callbacks/test_variables_callbacks.py index 1fc7959d..7f3cdfe3 100644 --- a/tests/frontend/callbacks/test_variables_callbacks.py +++ b/tests/frontend/callbacks/test_variables_callbacks.py @@ -16,6 +16,7 @@ from datadoc.enums import SupportedLanguages 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.fields.display_variables import VariableIdentifiers if TYPE_CHECKING: @@ -223,3 +224,24 @@ def test_accept_variable_metadata_date_input( assert metadata.variables[0].contains_data_until == arrow.get( contains_data_until, ).to("utc") + + +@pytest.mark.parametrize( + ("search_query", "expected_length"), + [("", 8), ("a", 4), ("pers_id", 1)], +) +def test_populate_variables_workspace_filter_variables( + search_query: str, + expected_length: int, + metadata: DataDocMetadata, +): + assert ( + len( + populate_variables_workspace( + metadata.variables, + SupportedLanguages.NORSK_BOKMÅL, + search_query, + ), + ) + == expected_length + )