Skip to content

Commit

Permalink
Filter displayed variables by short name (#272)
Browse files Browse the repository at this point in the history
* Implement filtering which variables are displayed

* Add test for variables filtering

* Add ellipsis to placeholder text
  • Loading branch information
mmwinther authored Apr 8, 2024
1 parent 6551edf commit c103299
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 34 deletions.
46 changes: 14 additions & 32 deletions src/datadoc/frontend/callbacks/register_callbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand Down Expand Up @@ -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"),
Expand All @@ -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(
Expand Down
44 changes: 44 additions & 0 deletions src/datadoc/frontend/callbacks/variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions src/datadoc/frontend/components/variables_tab.py
Original file line number Diff line number Diff line change
Expand Up @@ -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="",
Expand Down
22 changes: 22 additions & 0 deletions tests/frontend/callbacks/test_variables_callbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
)

0 comments on commit c103299

Please sign in to comment.