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

Changes to owner #206

Merged
merged 12 commits into from
Mar 11, 2024
16 changes: 8 additions & 8 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 @@ -29,7 +29,7 @@ dash = ">=2.15.0"
pydantic = "==2.5.2"
dash-bootstrap-components = ">=1.1.0"
pandas = ">=1.4.2"
ssb-datadoc-model = "==4.2.0"
ssb-datadoc-model = "==4.3.1"
dapla-toolbelt = ">=1.3.3"
gunicorn = ">=21.2.0"
flask-healthz = ">=0.0.3"
Expand Down
8 changes: 6 additions & 2 deletions src/datadoc/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@

from datadoc import config
from datadoc import state
from datadoc.backend.code_list import CodeList
from datadoc.backend.datadoc_metadata import DataDocMetadata
from datadoc.backend.statistic_subject_mapping import StatisticSubjectMapping
from datadoc.backend.unit_types import UnitTypes
from datadoc.enums import SupportedLanguages
from datadoc.frontend.callbacks.register_callbacks import register_callbacks
from datadoc.frontend.callbacks.register_callbacks import (
Expand Down Expand Up @@ -139,10 +139,14 @@ def collect_data_from_external_sources() -> None:
config.get_statistical_subject_source_url(),
)

state.unit_types = UnitTypes(
state.unit_types = CodeList(
config.get_unit_code(),
)

state.organisational_units = CodeList(
config.get_organisational_unit_code(),
)


def main(dataset_path: str | None = None) -> None:
"""Entrypoint when running as a script."""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,37 +16,40 @@


@dataclass
class UnitType:
"""Data structure for the a unit type."""
class CodeListItem:
"""Data structure for the a code list item."""
JanhSander marked this conversation as resolved.
Show resolved Hide resolved

titles: dict[str, str]
unit_code: str
mmwinther marked this conversation as resolved.
Show resolved Hide resolved

def get_title(self, language: SupportedLanguages) -> str:
"""Get the title in the given language."""
try:
return self.titles[
(
# Adjust to language codes in the UnitTypes structure.
"nb"
if language
in [
SupportedLanguages.NORSK_BOKMÅL,
SupportedLanguages.NORSK_NYNORSK,
]
else "en"
)
]
return self.titles[language]
except KeyError:
logger.exception(
"Could not find title for subject %s and language: %s",
self,
language.name,
)
return ""
try:
return self.titles[
(
# Adjust to language codes in the unit_type codelist.
JanhSander marked this conversation as resolved.
Show resolved Hide resolved
"nb"
if language
in [
SupportedLanguages.NORSK_BOKMÅL,
SupportedLanguages.NORSK_NYNORSK,
]
else "en"
)
]
except KeyError:
logger.exception(
"Could not find title for subject %s and language: %s",
self,
language.name,
)
return ""


class UnitTypes(GetExternalSource):
class CodeList(GetExternalSource):
"""Class for retrieving classifications from Klass."""

def __init__(self, classification_id: int | None) -> None:
Expand All @@ -58,13 +61,9 @@ def __init__(self, classification_id: int | None) -> None:
SupportedLanguages.NORSK_BOKMÅL.value,
SupportedLanguages.ENGLISH.value,
]

self._classifications: list[UnitType] = []

self._classifications: list[CodeListItem] = []
self.classification_id = classification_id

self.classifications_dataframes: dict[str, pd.DataFrame] | None = None

super().__init__()

def _fetch_data_from_external_source(
Expand All @@ -85,7 +84,6 @@ def _fetch_data_from_external_source(
.get_codes()
.data
)

except Exception:
logger.exception(
"Exception while getting classifications from Klass",
Expand Down Expand Up @@ -113,7 +111,7 @@ def _extract_titles(
def _create_unit_types_from_dataframe(
JanhSander marked this conversation as resolved.
Show resolved Hide resolved
self,
classifications_dataframes: dict[SupportedLanguages, pd.DataFrame],
) -> list[UnitType]:
) -> list[CodeListItem]:
"""Method that finds the name column in the dataframe, and returns all values in a list."""
classification_names = self._extract_titles(classifications_dataframes)
classification_codes: list
Expand All @@ -128,7 +126,7 @@ def _create_unit_types_from_dataframe(
unit_types = []
for a, b in zip(classification_names, classification_codes):
unit_types.append(
UnitType(a, b),
CodeListItem(a, b),
)
return unit_types

Expand All @@ -151,7 +149,7 @@ def _get_classification_dataframe_if_loaded(self) -> bool:
return False

@property
def classifications(self) -> list[UnitType]:
def classifications(self) -> list[CodeListItem]:
"""Getter for primary subjects."""
self._get_classification_dataframe_if_loaded()
logger.debug("Got %s classifications subjects", len(self._classifications))
Expand Down
1 change: 0 additions & 1 deletion src/datadoc/backend/external_sources/external_sources.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ def __init__(self) -> None:
Initializes the future object.
"""
self.future: concurrent.futures.Future[T | None] | None = None

executor = concurrent.futures.ThreadPoolExecutor(max_workers=1)
self.future = executor.submit(
self._fetch_data_from_external_source,
Expand Down
5 changes: 5 additions & 0 deletions src/datadoc/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,8 @@ def get_oidc_token() -> str | None:
def get_unit_code() -> int | None:
"""The code for the Unit Type code list in Klass."""
return int(_get_config_item("DATADOC_UNIT_CODE") or 702)


def get_organisational_unit_code() -> int | None:
"""The code for the organisational units code list in Klass."""
return int(_get_config_item("DATADOC_ORGANISATIONAL_UNIT_CODE") or 83)
20 changes: 17 additions & 3 deletions src/datadoc/frontend/fields/display_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,19 @@ def get_unit_type_options(
]


def get_owner_options(
language: SupportedLanguages,
) -> list[dict[str, str]]:
"""Collect the owner options for the given language."""
return [
{
"label": f"{option.unit_code} - {option.get_title(language)}",
"value": option.unit_code,
}
for option in state.organisational_units.classifications
]


class DatasetIdentifiers(str, Enum):
"""As defined here: https://statistics-norway.atlassian.net/l/c/aoSfEWJU."""

Expand Down Expand Up @@ -225,13 +238,14 @@ class DatasetIdentifiers(str, Enum):
editable=False,
value_getter=get_metadata_and_stringify,
),
DatasetIdentifiers.OWNER: DisplayDatasetMetadata(
DatasetIdentifiers.OWNER: DisplayDatasetMetadataDropdown(
identifier=DatasetIdentifiers.OWNER.value,
display_name="Eier",
description="Maskingenerert seksjonstilhørighet til den som oppretter metadata om datasettet, men kan korrigeres manuelt",
obligatory=True,
editable=False,
multiple_language_support=True,
editable=True,
multiple_language_support=False,
options_getter=get_owner_options,
),
DatasetIdentifiers.FILE_PATH: DisplayDatasetMetadata(
identifier=DatasetIdentifiers.FILE_PATH.value,
Expand Down
6 changes: 4 additions & 2 deletions src/datadoc/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
from typing import TYPE_CHECKING

if TYPE_CHECKING:
from datadoc.backend.code_list import CodeList
from datadoc.backend.datadoc_metadata import DataDocMetadata
from datadoc.backend.statistic_subject_mapping import StatisticSubjectMapping
from datadoc.backend.unit_types import UnitTypes
from datadoc.enums import SupportedLanguages


Expand All @@ -26,4 +26,6 @@

statistic_subject_mapping: StatisticSubjectMapping

unit_types: UnitTypes
unit_types: CodeList

organisational_units: CodeList
Loading