Skip to content

Commit

Permalink
Merge branch 'master' into feat/dpmeta-62-add-klass-codes-to-owner
Browse files Browse the repository at this point in the history
  • Loading branch information
jonolehagemo authored Mar 8, 2024
2 parents 42d338a + 26cd64f commit 0fc24c0
Show file tree
Hide file tree
Showing 10 changed files with 104 additions and 70 deletions.
8 changes: 4 additions & 4 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ beautifulsoup4 = ">=4.12.3"
cloudpathlib = { extras = ["gs"], version = ">=0.17.0" }
pyjwt = ">=2.8.0"
ssb-klass-python = ">=0.0.9"
ssb-dash-components = ">=0.4.1"
ssb-dash-components = ">=0.5.2"

[tool.poetry.group.dev.dependencies]
mypy = ">=0.950"
Expand Down
5 changes: 4 additions & 1 deletion src/datadoc/frontend/callbacks/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ def open_file(file_path: str | None = None) -> DataDocMetadata:

dataset = file_path or get_dataset_path()
logger.info("Opening dataset %s", dataset)
return DataDocMetadata(state.statistic_subject_mapping, dataset_path=str(dataset))
return DataDocMetadata(
state.statistic_subject_mapping,
dataset_path=str(dataset) if dataset else None,
)


def open_dataset_handling(
Expand Down
16 changes: 11 additions & 5 deletions src/datadoc/frontend/callbacks/register_callbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from datadoc.frontend.callbacks.dataset import accept_dataset_metadata_input
from datadoc.frontend.callbacks.dataset import change_language_dataset_metadata
from datadoc.frontend.callbacks.dataset import open_dataset_handling
from datadoc.frontend.callbacks.utils import update_global_language_state
from datadoc.frontend.callbacks.variables import (
accept_variable_datatable_metadata_input,
)
Expand Down Expand Up @@ -217,14 +218,19 @@ def register_new_variables_tab_callbacks(app: Dash) -> None:
prevent_initial_call=True,
)
def callback_populate_new_variables_workspace(
language: str, # noqa: ARG001
language: str,
) -> list:
"""Create variable workspace with accordions for variables."""
update_global_language_state(SupportedLanguages(language))
logger.info("Populating new variables workspace")
return [
build_ssb_accordion(
variable,
{"type": "variables-accordion", "id": variable},
variable,
variable.short_name,
{
"type": "variables-accordion",
"id": f"{variable.short_name}-{language}",
},
variable.short_name,
children=[
build_edit_section(
OBLIGATORY_VARIABLES_METADATA,
Expand All @@ -240,7 +246,7 @@ def callback_populate_new_variables_workspace(
),
],
)
for variable in list(state.metadata.variables_lookup.keys())
for variable in list(state.metadata.variables)
]

@app.callback(
Expand Down
5 changes: 3 additions & 2 deletions src/datadoc/frontend/callbacks/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@

def update_global_language_state(language: SupportedLanguages) -> None:
"""Update global language state."""
logger.debug("Updating language: %s", language.name)
state.current_metadata_language = language
if state.current_metadata_language != language:
logger.debug("Updating language: %s", language.name)
state.current_metadata_language = language


def get_language_strings_enum(
Expand Down
6 changes: 6 additions & 0 deletions src/datadoc/frontend/callbacks/variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,12 @@ def accept_variable_metadata_input(
Returns an error message if an exception was raised, otherwise returns None.
"""
logger.debug(
"Updating %s, %s with %s",
metadata_field,
variable_short_name,
value,
)
try:
if metadata_field in MULTIPLE_LANGUAGE_VARIABLES_METADATA:
new_value = handle_multi_language_metadata(
Expand Down
19 changes: 9 additions & 10 deletions src/datadoc/frontend/components/resources_test_new_variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
from dash import html

if TYPE_CHECKING:
from datadoc.frontend.fields.display_new_variables import (
DisplayNewVariablesMetadata,
)
from datadoc_model import model

from datadoc.frontend.fields.display_new_variables import VariablesInputField

logger = logging.getLogger(__name__)

Expand All @@ -23,11 +23,9 @@
VARIABLES_METADATA_INPUT = "variables-metadata-input"


# section of Inputs: Input, checkbox or dropdown -
# It is hard to separate between DisplayNewVariablesMetatdata and NewVariablesMetadataDropddown - with NewVariablesMetadataDropddown in type hint it's temporary solved
def build_input_field_section(
metadata_inputs: list[DisplayNewVariablesMetadata],
variable_short_name: str,
metadata_inputs: list[VariablesInputField],
variable: model.Variable,
language: str,
) -> dbc.Form:
"""Create input fields."""
Expand All @@ -36,10 +34,11 @@ def build_input_field_section(
i.render(
{
"type": VARIABLES_METADATA_INPUT,
"variable_short_name": variable_short_name,
"variable_short_name": variable.short_name,
"id": i.identifier,
},
language,
variable,
)
for i in metadata_inputs
],
Expand All @@ -52,15 +51,15 @@ def build_input_field_section(
def build_edit_section(
metadata_inputs: list,
title: str,
variable_short_name: str,
variable: model.Variable,
language: str,
) -> html.Section:
"""Create input section."""
return html.Section(
id={"type": "edit-section", "title": title},
children=[
ssb.Title(title, size=3, className="input-section-title"),
build_input_field_section(metadata_inputs, variable_short_name, language),
build_input_field_section(metadata_inputs, variable, language),
],
className="input-section",
)
Expand Down
63 changes: 41 additions & 22 deletions src/datadoc/frontend/fields/display_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from collections.abc import Callable

from dash.development.base_component import Component
from datadoc_model import model
from datadoc_model.model import LanguageStringType
from pydantic import BaseModel

Expand Down Expand Up @@ -56,15 +57,15 @@ def new_input_kwargs_factory() -> dict[str, t.Any]:

def get_standard_metadata(metadata: BaseModel, identifier: str) -> MetadataInputTypes:
"""Get a metadata value from the model."""
value = metadata.model_dump()[identifier]
if value is None:
return None
return str(value)
return getattr(metadata, identifier)


def get_metadata_and_stringify(metadata: BaseModel, identifier: str) -> str:
def get_metadata_and_stringify(metadata: BaseModel, identifier: str) -> str | None:
"""Get a metadata value from the model and cast to string."""
return str(get_standard_metadata(metadata, identifier))
value = get_standard_metadata(metadata, identifier)
if value is None:
return None
return str(value)


def get_multi_language_metadata(metadata: BaseModel, identifier: str) -> str | None:
Expand Down Expand Up @@ -117,7 +118,7 @@ class DisplayDatasetMetadata(DisplayMetadata):

extra_kwargs: dict[str, Any] = field(default_factory=input_kwargs_factory)
component: type[Component] = dcc.Input
value_getter: Callable[[BaseModel, str], Any] = get_standard_metadata
value_getter: Callable[[BaseModel, str], Any] = get_metadata_and_stringify


@dataclass
Expand All @@ -131,63 +132,81 @@ class DisplayDatasetMetadataDropdown(DisplayDatasetMetadata):
component: type[Component] = dcc.Dropdown


# New design for variables - Input , dropdown, checkbox
# Language for Input and Checkbox is not used, but trigger error if not handled when render
@dataclass
class DisplayNewVariablesMetadata(DisplayMetadata):
class VariablesInputField(DisplayMetadata):
"""Controls for how a given metadata field should be displayed.
Specific to variable fields.
"""

extra_kwargs: dict[str, Any] = field(default_factory=new_input_kwargs_factory)
value_getter: Callable[[BaseModel, str], Any] = get_standard_metadata
value_getter: Callable[[BaseModel, str], Any] = get_metadata_and_stringify
type: str = "text"

def render(self, variable_id: dict, language: str) -> ssb.Input:
"""Build Input component with props."""
self.language = language
def render(
self,
variable_id: dict,
language: str, # noqa: ARG002
variable: model.Variable,
) -> ssb.Input:
"""Build Input component."""
value = self.value_getter(variable, self.identifier)
return ssb.Input(
label=self.display_name,
id=variable_id,
debounce=self.type != "date",
type=self.type,
disabled=not self.editable,
value=value,
)


@dataclass
class DisplayNewVariablesMetadataDropdown(DisplayNewVariablesMetadata):
class VariablesDropdownField(DisplayMetadata):
"""Control how a Dropdown should be displayed."""

extra_kwargs: dict[str, Any] = field(default_factory=new_input_kwargs_factory)
value_getter: Callable[[BaseModel, str], Any] = get_metadata_and_stringify
# fmt: off
options_getter: Callable[[SupportedLanguages], list[dict[str, str]]] = lambda _: [] # noqa: E731
# fmt: on
extra_kwargs: dict[str, Any] = field(default_factory=new_input_kwargs_factory)

def render(self, variable_id: dict, language: str) -> ssb.Dropdown:
"""Build Dropdown component with props."""
def render(
self,
variable_id: dict,
language: str,
variable: model.Variable,
) -> ssb.Dropdown:
"""Build Dropdown component."""
value = self.value_getter(variable, self.identifier)
return ssb.Dropdown(
header=self.display_name,
id=variable_id,
items=self.options_getter(SupportedLanguages(language)),
value=value,
)


@dataclass
class DisplayNewVariablesMetadataCheckbox(DisplayNewVariablesMetadata):
class VariablesCheckboxField(DisplayMetadata):
"""Controls for how a checkbox metadata field should be displayed."""

extra_kwargs: dict[str, Any] = field(default_factory=input_kwargs_factory)
value_getter: Callable[[BaseModel, str], Any] = get_standard_metadata

def render(self, variable_id: dict, language: str) -> dbc.Checkbox:
"""Build Dropdown component with props."""
self.language = language
def render(
self,
variable_id: dict,
language: str, # noqa: ARG002
variable: model.Variable,
) -> dbc.Checkbox:
"""Build Checkbox component."""
value = self.value_getter(variable, self.identifier)
return dbc.Checkbox(
label=self.display_name,
id=variable_id,
disabled=not self.editable,
label_class_name="ssb-checkbox checkbox-label",
class_name="ssb-checkbox",
value=value,
)
Loading

0 comments on commit 0fc24c0

Please sign in to comment.