Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chore/dpmeta 137 remove unused methods and classes #257

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/datadoc/frontend/components/builders.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
127 changes: 27 additions & 100 deletions src/datadoc/frontend/fields/display_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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.

Expand Down Expand Up @@ -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."""
Expand Down Expand Up @@ -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(
Expand All @@ -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
3 changes: 1 addition & 2 deletions src/datadoc/frontend/fields/display_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down
8 changes: 4 additions & 4 deletions src/datadoc/frontend/fields/display_variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -41,7 +41,7 @@ class VariableIdentifiers(str, Enum):

DISPLAY_VARIABLES: dict[
VariableIdentifiers,
VariablesFieldTypes,
MetdataFieldTypes,
] = {
VariableIdentifiers.SHORT_NAME: MetadataInputField(
identifier=VariableIdentifiers.SHORT_NAME.value,
Expand Down Expand Up @@ -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)",
Expand Down