Skip to content

Commit

Permalink
Merge pull request #336 from statisticsnorway/dpmeta-108-set-default-…
Browse files Browse the repository at this point in the history
…value-spatial-coverage-description

Set default value for spatial coverage description
  • Loading branch information
JanhSander authored May 6, 2024
2 parents 831a1ba + 425c1c2 commit 7dafc1e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
20 changes: 14 additions & 6 deletions src/datadoc/backend/datadoc_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
from datadoc.enums import Assessment
from datadoc.enums import DataSetState
from datadoc.enums import DataSetStatus
from datadoc.enums import LanguageStringType
from datadoc.enums import LanguageStringTypeItem
from datadoc.frontend.fields.display_dataset import (
OBLIGATORY_DATASET_METADATA_IDENTIFIERS,
)
Expand Down Expand Up @@ -81,7 +83,6 @@ def _open_path(path: str) -> pathlib.Path | CloudPath:
if path.startswith(GSPath.cloud_prefix):
client = GSClient(credentials=AuthClient.fetch_google_credentials())
return GSPath(path, client=client)

return pathlib.Path(path)

def _set_variable_uuid(self) -> None:
Expand All @@ -97,7 +98,6 @@ def extract_metadata_from_files(self) -> None:
"""
if self.metadata_document is not None and self.metadata_document.exists():
self.extract_metadata_from_existing_document(self.metadata_document)

if (
self.dataset_path is not None
and self.dataset == model.Dataset()
Expand All @@ -112,9 +112,7 @@ def extract_metadata_from_files(self) -> None:
v.variable_role = model.VariableRole.MEASURE
if v.direct_person_identifying is None:
v.direct_person_identifying = False

self._set_variable_uuid()

if not self.dataset.id:
self.dataset.id = uuid.uuid4()
self.variables_lookup = {v.short_name: v for v in self.variables}
Expand Down Expand Up @@ -169,11 +167,9 @@ def extract_metadata_from_dataset(
"""
self.ds_schema: DatasetParser = DatasetParser.for_file(dataset)
dapla_dataset_path_info = DaplaDatasetPathInfo(dataset)

subject_field = self._statistic_subject_mapping.get_secondary_subject(
dapla_dataset_path_info.statistic_short_name,
)

self.dataset = model.Dataset(
short_name=dapla_dataset_path_info.dataset_short_name,
dataset_state=dapla_dataset_path_info.dataset_state,
Expand All @@ -187,6 +183,7 @@ def extract_metadata_from_dataset(
file_path=str(self.dataset_path),
metadata_created_by=user_info.get_user_info_for_current_platform().short_email,
subject_field=subject_field,
spatial_coverage_description=self.set_default_spatial_coverage_description(),
)
self.variables = self.ds_schema.get_fields()

Expand Down Expand Up @@ -263,3 +260,14 @@ def percent_complete(self) -> int:
],
)
return calculate_percentage(num_set_fields, num_all_fields)

def set_default_spatial_coverage_description(self) -> LanguageStringType:
"""Returns the default value 'Norge'."""
return LanguageStringType(
[
LanguageStringTypeItem(
languageCode="nb",
languageText="Norge",
),
],
)
6 changes: 6 additions & 0 deletions tests/backend/test_datadoc_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -432,3 +432,9 @@ def test_existing_metadata_variables_valid_id(
post_write_id: list = [v["id"] for v in json.load(f)["datadoc"]["variables"]]

assert pre_open_id == post_write_id


def test_default_spatial_coverage_description(metadata: DataDocMetadata):
metadata.extract_metadata_from_dataset(TEST_PARQUET_FILEPATH)
language_strings = metadata.dataset.spatial_coverage_description
assert language_strings.root[0].languageText == "Norge" # type: ignore [union-attr, index]

0 comments on commit 7dafc1e

Please sign in to comment.