Changeset View
Changeset View
Standalone View
Standalone View
swh/deposit/api/checks.py
# Copyright (C) 2017-2020 The Software Heritage developers | # Copyright (C) 2017-2020 The Software Heritage developers | ||||
# See the AUTHORS file at the top-level directory of this distribution | # See the AUTHORS file at the top-level directory of this distribution | ||||
# License: GNU General Public License version 3, or any later version | # License: GNU General Public License version 3, or any later version | ||||
# See top-level LICENSE file for more information | # See top-level LICENSE file for more information | ||||
"""Functional Metadata checks: | |||||
Mandatory fields: | |||||
- 'author' | |||||
- 'name' or 'title' | |||||
""" | |||||
from typing import Dict, Optional, Tuple | from typing import Dict, Optional, Tuple | ||||
MANDATORY_FIELDS_MISSING = "Mandatory fields are missing" | MANDATORY_FIELDS_MISSING = "Mandatory fields are missing" | ||||
ALTERNATE_FIELDS_MISSING = "Mandatory alternate fields are missing" | ALTERNATE_FIELDS_MISSING = "Mandatory alternate fields are missing" | ||||
def check_metadata(metadata: Dict) -> Tuple[bool, Optional[Dict]]: | def check_metadata(metadata: Dict) -> Tuple[bool, Optional[Dict]]: | ||||
"""Check metadata for mandatory field presence. | """Check metadata for mandatory field presence. | ||||
Args: | Args: | ||||
metadata: Metadata dictionary to check for mandatory fields | metadata: Metadata dictionary to check for mandatory fields | ||||
Returns: | Returns: | ||||
tuple (status, error_detail): True, None if metadata are | tuple (status, error_detail): True, None if metadata are | ||||
ok (False, <detailed-error>) otherwise. | ok (False, <detailed-error>) otherwise. | ||||
""" | """ | ||||
# following fields are mandatory | |||||
required_fields = { | required_fields = { | ||||
"author": False, | "author": False, | ||||
} | } | ||||
# at least one value per couple below is mandatory | |||||
alternate_fields = { | alternate_fields = { | ||||
("name", "title"): False, # alternate field, at least one | ("name", "title"): False, | ||||
# of them must be present | |||||
} | } | ||||
for field, value in metadata.items(): | for field, value in metadata.items(): | ||||
for name in required_fields: | for name in required_fields: | ||||
if name in field: | if name in field: | ||||
required_fields[name] = True | required_fields[name] = True | ||||
for possible_names in alternate_fields: | for possible_names in alternate_fields: | ||||
Show All 18 Lines |