diff --git a/src/datadoc/frontend/components/builders.py b/src/datadoc/frontend/components/builders.py index b27eaf42..93961823 100644 --- a/src/datadoc/frontend/components/builders.py +++ b/src/datadoc/frontend/components/builders.py @@ -15,7 +15,7 @@ from datadoc.frontend.fields.display_base import DATASET_METADATA_INPUT from datadoc.frontend.fields.display_base import VARIABLES_METADATA_INPUT from datadoc.frontend.fields.display_base import DatasetFieldTypes -from datadoc.frontend.fields.display_base import VariablesFieldTypes +from datadoc.frontend.fields.display_base import MetdataFieldTypes if TYPE_CHECKING: from datadoc_model import model @@ -113,7 +113,7 @@ def build_ssb_button(text: str, icon_class: str, button_id: str) -> dbc.Button: def build_input_field_section( - metadata_fields: list[VariablesFieldTypes], + metadata_fields: list[MetdataFieldTypes], variable: model.Variable, language: str, ) -> dbc.Form: diff --git a/src/datadoc/frontend/fields/display_base.py b/src/datadoc/frontend/fields/display_base.py index c756239f..b236d64c 100644 --- a/src/datadoc/frontend/fields/display_base.py +++ b/src/datadoc/frontend/fields/display_base.py @@ -38,13 +38,6 @@ DATASET_METADATA_DATE_INPUT = "dataset-metadata-date-input" -INPUT_KWARGS = { - "debounce": True, - "style": {"width": "100%"}, - "className": "ssb-input", -} - - def get_enum_options_for_language( enum: Enum, language: SupportedLanguages, @@ -59,15 +52,6 @@ def get_enum_options_for_language( ] -def input_kwargs_factory() -> dict[str, t.Any]: - """Initialize the field extra_kwargs. - - We aren't allowed to directly assign a mutable type like a dict to - a dataclass field. - """ - return INPUT_KWARGS - - def empty_kwargs_factory() -> dict[str, t.Any]: """Initialize the field extra_kwargs. @@ -144,29 +128,6 @@ class DisplayMetadata: multiple_language_support: bool = False -@dataclass -class DisplayDatasetMetadata(DisplayMetadata): - """Controls for how a given metadata field should be displayed. - - Specific to dataset fields. - """ - - extra_kwargs: dict[str, Any] = field(default_factory=input_kwargs_factory) - component: type[Component] = dcc.Input - value_getter: Callable[[BaseModel, str], Any] = get_metadata_and_stringify - - -@dataclass -class DisplayDatasetMetadataDropdown(DisplayDatasetMetadata): - """Include the possible options which a user may choose from.""" - - # fmt: off - options_getter: Callable[[SupportedLanguages], list[dict[str, str]]] = lambda _: [] # noqa: E731 - # fmt: on - extra_kwargs: dict[str, Any] = field(default_factory=empty_kwargs_factory) - component: type[Component] = dcc.Dropdown - - @dataclass class MetadataInputField(DisplayMetadata): """Controls how a input field should be displayed.""" @@ -254,67 +215,10 @@ def render( @dataclass -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=empty_kwargs_factory) - value_getter: Callable[[BaseModel, str], Any] = get_metadata_and_stringify - type: str = "text" - - 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=True, - type=self.type, - disabled=not self.editable, - value=value, - className="input-component", - ) - - -@dataclass -class VariablesDropdownField(DisplayMetadata): - """Control how a Dropdown should be displayed.""" - - extra_kwargs: dict[str, Any] = field(default_factory=empty_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 - - 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, - className="dropdown-component", - ) - - -@dataclass -class VariablesCheckboxField(DisplayMetadata): +class MetadataCheckboxField(DisplayMetadata): """Controls for how a checkbox metadata field should be displayed.""" - extra_kwargs: dict[str, Any] = field(default_factory=input_kwargs_factory) + extra_kwargs: dict[str, Any] = field(default_factory=empty_kwargs_factory) value_getter: Callable[[BaseModel, str], Any] = get_standard_metadata def render( @@ -335,11 +239,34 @@ def render( ) -VariablesFieldTypes = ( +MetdataFieldTypes = ( MetadataInputField | MetadataDropdownField - | VariablesCheckboxField + | MetadataCheckboxField | MetadataPeriodField ) DatasetFieldTypes = MetadataInputField | MetadataDropdownField | MetadataPeriodField + + +@dataclass +class DisplayDatasetMetadata(DisplayMetadata): + """Controls for how a given metadata field should be displayed. + + Specific to dataset fields. + """ + + extra_kwargs: dict[str, Any] = field(default_factory=empty_kwargs_factory) + component: type[Component] = dcc.Input + value_getter: Callable[[BaseModel, str], Any] = get_metadata_and_stringify + + +@dataclass +class DisplayDatasetMetadataDropdown(DisplayDatasetMetadata): + """Include the possible options which a user may choose from.""" + + # fmt: off + options_getter: Callable[[SupportedLanguages], list[dict[str, str]]] = lambda _: [] # noqa: E731 + # fmt: on + extra_kwargs: dict[str, Any] = field(default_factory=empty_kwargs_factory) + component: type[Component] = dcc.Dropdown diff --git a/src/datadoc/frontend/fields/display_dataset.py b/src/datadoc/frontend/fields/display_dataset.py index 2329fd84..9e8bfeaa 100644 --- a/src/datadoc/frontend/fields/display_dataset.py +++ b/src/datadoc/frontend/fields/display_dataset.py @@ -10,7 +10,6 @@ from datadoc import enums from datadoc import state from datadoc.frontend.fields.display_base import DATASET_METADATA_DATE_INPUT -from datadoc.frontend.fields.display_base import INPUT_KWARGS from datadoc.frontend.fields.display_base import DatasetFieldTypes from datadoc.frontend.fields.display_base import DisplayDatasetMetadataDropdown from datadoc.frontend.fields.display_base import MetadataDropdownField @@ -171,7 +170,7 @@ class DatasetIdentifiers(str, Enum): identifier=DatasetIdentifiers.VERSION.value, display_name="Versjon", description="Versjon", - extra_kwargs=dict(type="number", min=1, **INPUT_KWARGS), + extra_kwargs={"type": "number", "min": 1}, obligatory=True, ), DatasetIdentifiers.VERSION_DESCRIPTION: MetadataInputField( diff --git a/src/datadoc/frontend/fields/display_variables.py b/src/datadoc/frontend/fields/display_variables.py index e2c67cf4..9a6ca50b 100644 --- a/src/datadoc/frontend/fields/display_variables.py +++ b/src/datadoc/frontend/fields/display_variables.py @@ -7,11 +7,11 @@ from datadoc import enums from datadoc.frontend.fields.display_base import VARIABLES_METADATA_DATE_INPUT +from datadoc.frontend.fields.display_base import MetadataCheckboxField from datadoc.frontend.fields.display_base import MetadataDropdownField from datadoc.frontend.fields.display_base import MetadataInputField from datadoc.frontend.fields.display_base import MetadataPeriodField -from datadoc.frontend.fields.display_base import VariablesCheckboxField -from datadoc.frontend.fields.display_base import VariablesFieldTypes +from datadoc.frontend.fields.display_base import MetdataFieldTypes from datadoc.frontend.fields.display_base import get_enum_options_for_language from datadoc.frontend.fields.display_base import get_multi_language_metadata @@ -41,7 +41,7 @@ class VariableIdentifiers(str, Enum): DISPLAY_VARIABLES: dict[ VariableIdentifiers, - VariablesFieldTypes, + MetdataFieldTypes, ] = { VariableIdentifiers.SHORT_NAME: MetadataInputField( identifier=VariableIdentifiers.SHORT_NAME.value, @@ -86,7 +86,7 @@ class VariableIdentifiers(str, Enum): obligatory=True, type="url", ), - VariableIdentifiers.DIRECT_PERSON_IDENTIFYING: VariablesCheckboxField( + VariableIdentifiers.DIRECT_PERSON_IDENTIFYING: MetadataCheckboxField( identifier=VariableIdentifiers.DIRECT_PERSON_IDENTIFYING.value, display_name="Direkte personidentifiserende informasjon", description="Direkte personidentifiserende informasjon (DPI)",