Changeset View
Changeset View
Standalone View
Standalone View
swh/deposit/api/private/deposit_read.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 | ||||
from contextlib import contextmanager | from contextlib import contextmanager | ||||
import os | import os | ||||
import shutil | import shutil | ||||
import tempfile | import tempfile | ||||
from typing import Any, Dict, Tuple | from typing import Any, Dict, Tuple | ||||
from rest_framework import status | from rest_framework import status | ||||
from swh.core import tarball | from swh.core import tarball | ||||
from swh.deposit.api import __version__ | |||||
from swh.deposit.utils import normalize_date | from swh.deposit.utils import normalize_date | ||||
from swh.model import identifiers | from swh.model import identifiers | ||||
from swh.model.model import MetadataAuthorityType | |||||
from . import APIPrivateView, DepositReadMixin | from . import APIPrivateView, DepositReadMixin | ||||
from ...config import ARCHIVE_TYPE, SWH_PERSON | from ...config import ARCHIVE_TYPE, SWH_PERSON | ||||
from ...models import Deposit | from ...models import Deposit | ||||
from ..common import APIGet | from ..common import APIGet | ||||
@contextmanager | @contextmanager | ||||
▲ Show 20 Lines • Show All 73 Lines • ▼ Show 20 Lines | ) -> Tuple[int, Any, str]: | ||||
) | ) | ||||
class APIReadMetadata(APIPrivateView, APIGet, DepositReadMixin): | class APIReadMetadata(APIPrivateView, APIGet, DepositReadMixin): | ||||
"""Class in charge of aggregating metadata on a deposit. | """Class in charge of aggregating metadata on a deposit. | ||||
""" | """ | ||||
def __init__(self): | |||||
super().__init__() | |||||
self.provider = self.config["provider"] | |||||
self.tool = { | |||||
"name": "swh-deposit", | |||||
"version": __version__, | |||||
"configuration": {"sword_version": "2"}, | |||||
} | |||||
def _normalize_dates(self, deposit, metadata): | def _normalize_dates(self, deposit, metadata): | ||||
"""Normalize the date to use as a tuple of author date, committer date | """Normalize the date to use as a tuple of author date, committer date | ||||
from the incoming metadata. | from the incoming metadata. | ||||
Args: | Args: | ||||
deposit (Deposit): Deposit model representation | deposit (Deposit): Deposit model representation | ||||
metadata (Dict): Metadata dict representation | metadata (Dict): Metadata dict representation | ||||
Show All 26 Lines | def metadata_read(self, deposit): | ||||
Returns: | Returns: | ||||
Dictionary of data representing the deposit to inject in swh. | Dictionary of data representing the deposit to inject in swh. | ||||
""" | """ | ||||
metadata = self._metadata_get(deposit) | metadata = self._metadata_get(deposit) | ||||
# Read information metadata | # Read information metadata | ||||
data = {"origin": {"type": "deposit", "url": deposit.origin_url,}} | data = {"origin": {"type": "deposit", "url": deposit.origin_url,}} | ||||
# metadata provider | |||||
self.provider["provider_name"] = deposit.client.last_name | |||||
self.provider["provider_url"] = deposit.client.provider_url | |||||
author_date, commit_date = self._normalize_dates(deposit, metadata) | author_date, commit_date = self._normalize_dates(deposit, metadata) | ||||
if deposit.parent: | if deposit.parent: | ||||
swh_persistent_id = deposit.parent.swh_id | swh_persistent_id = deposit.parent.swh_id | ||||
swhid = identifiers.parse_swhid(swh_persistent_id) | swhid = identifiers.parse_swhid(swh_persistent_id) | ||||
parent_revision = swhid.object_id | parent_revision = swhid.object_id | ||||
parents = [parent_revision] | parents = [parent_revision] | ||||
else: | else: | ||||
parents = [] | parents = [] | ||||
data["origin_metadata"] = { | data["origin_metadata"] = { | ||||
"provider": self.provider, | # metadata provider | ||||
"provider": { | |||||
"provider_name": deposit.client.last_name, | |||||
"provider_url": deposit.client.provider_url, | |||||
"provider_type": MetadataAuthorityType.DEPOSIT_CLIENT.value, | |||||
"metadata": {}, | |||||
}, | |||||
"tool": self.tool, | "tool": self.tool, | ||||
"metadata": metadata, | "metadata": metadata, | ||||
} | } | ||||
data["deposit"] = { | data["deposit"] = { | ||||
"id": deposit.id, | "id": deposit.id, | ||||
"client": deposit.client.username, | "client": deposit.client.username, | ||||
"collection": deposit.collection.name, | "collection": deposit.collection.name, | ||||
"author": SWH_PERSON, | "author": SWH_PERSON, | ||||
Show All 14 Lines |