Changeset View
Changeset View
Standalone View
Standalone View
swh/deposit/api/common.py
Show All 21 Lines | ||||||||||||
from rest_framework.request import Request | from rest_framework.request import Request | |||||||||||
from rest_framework.views import APIView | from rest_framework.views import APIView | |||||||||||
from swh.deposit.api.checks import check_metadata | from swh.deposit.api.checks import check_metadata | |||||||||||
from swh.deposit.api.converters import convert_status_detail | from swh.deposit.api.converters import convert_status_detail | |||||||||||
from swh.deposit.models import Deposit | from swh.deposit.models import Deposit | |||||||||||
from swh.deposit.utils import compute_metadata_context | from swh.deposit.utils import compute_metadata_context | |||||||||||
from swh.model import hashutil | from swh.model import hashutil | |||||||||||
from swh.model.identifiers import ExtendedSWHID, QualifiedSWHID, ValidationError | from swh.model.identifiers import ( | |||||||||||
ExtendedObjectType, | ||||||||||||
ExtendedSWHID, | ||||||||||||
QualifiedSWHID, | ||||||||||||
ValidationError, | ||||||||||||
) | ||||||||||||
from swh.model.model import ( | from swh.model.model import ( | |||||||||||
MetadataAuthority, | MetadataAuthority, | |||||||||||
MetadataAuthorityType, | MetadataAuthorityType, | |||||||||||
MetadataFetcher, | MetadataFetcher, | |||||||||||
Origin, | Origin, | |||||||||||
RawExtrinsicMetadata, | RawExtrinsicMetadata, | |||||||||||
) | ) | |||||||||||
from swh.scheduler.utils import create_oneshot_task_dict | from swh.scheduler.utils import create_oneshot_task_dict | |||||||||||
▲ Show 20 Lines • Show All 637 Lines • ▼ Show 20 Lines | ) -> Tuple[ExtendedSWHID, Deposit, DepositRequest]: | |||||||||||
metadata_context = {} | metadata_context = {} | |||||||||||
else: | else: | |||||||||||
metadata_context = compute_metadata_context(swhid_reference) | metadata_context = compute_metadata_context(swhid_reference) | |||||||||||
if deposit_origin: # metadata deposit update on completed deposit | if deposit_origin: # metadata deposit update on completed deposit | |||||||||||
metadata_context["origin"] = deposit_origin | metadata_context["origin"] = deposit_origin | |||||||||||
target_swhid = extended_swhid_from_qualified(swhid_reference) | target_swhid = extended_swhid_from_qualified(swhid_reference) | |||||||||||
# store that metadata to the metadata storage | self._check_swhid_in_archive(target_swhid) | |||||||||||
metadata_object = RawExtrinsicMetadata( | metadata_object = RawExtrinsicMetadata( | |||||||||||
target=target_swhid, # core swhid or origin | target=target_swhid, # core swhid or origin | |||||||||||
discovery_date=deposit_request.date, | discovery_date=deposit_request.date, | |||||||||||
authority=metadata_authority, | authority=metadata_authority, | |||||||||||
fetcher=metadata_fetcher, | fetcher=metadata_fetcher, | |||||||||||
format="sword-v2-atom-codemeta", | format="sword-v2-atom-codemeta", | |||||||||||
metadata=raw_metadata, | metadata=raw_metadata, | |||||||||||
**metadata_context, | **metadata_context, | |||||||||||
) | ) | |||||||||||
# write to metadata storage | # write to metadata storage | |||||||||||
self.storage_metadata.metadata_authority_add([metadata_authority]) | self.storage_metadata.metadata_authority_add([metadata_authority]) | |||||||||||
self.storage_metadata.metadata_fetcher_add([metadata_fetcher]) | self.storage_metadata.metadata_fetcher_add([metadata_fetcher]) | |||||||||||
self.storage_metadata.raw_extrinsic_metadata_add([metadata_object]) | self.storage_metadata.raw_extrinsic_metadata_add([metadata_object]) | |||||||||||
return (target_swhid, deposit, deposit_request) | return (target_swhid, deposit, deposit_request) | |||||||||||
def _check_swhid_in_archive(self, target_swhid: ExtendedSWHID) -> None: | ||||||||||||
"""Check the target object already exists in the archive, | ||||||||||||
and raises a BAD_REQUEST if it does not.""" | ||||||||||||
if target_swhid.object_type in (ExtendedObjectType.CONTENT,): | ||||||||||||
if list( | ||||||||||||
self.storage.content_missing_per_sha1_git([target_swhid.object_id]) | ||||||||||||
): | ||||||||||||
raise DepositError( | ||||||||||||
BAD_REQUEST, | ||||||||||||
f"Cannot load metadata on {target_swhid}, this content " | ||||||||||||
ardumont: Why such formulation?
just a plain `yet` is enough (same goes for the next occurrence ;) | ||||||||||||
Done Inline ActionsBecause "does not exist in the archive yet" may sound like it is about to be loaded vlorentz: Because "does not exist in the archive yet" may sound like it is about to be loaded | ||||||||||||
Not Done Inline Actions
then ^ is even clearer and without any ambiguity. ardumont: then ^ is even clearer and without any ambiguity. | ||||||||||||
f"object does not exist in the archive (yet?).", | ||||||||||||
) | ||||||||||||
elif target_swhid.object_type in ( | ||||||||||||
ExtendedObjectType.DIRECTORY, | ||||||||||||
ExtendedObjectType.REVISION, | ||||||||||||
ExtendedObjectType.RELEASE, | ||||||||||||
ExtendedObjectType.SNAPSHOT, | ||||||||||||
): | ||||||||||||
target_type_name = target_swhid.object_type.name.lower() | ||||||||||||
method = getattr(self.storage, target_type_name + "_missing") | ||||||||||||
if list(method([target_swhid.object_id])): | ||||||||||||
raise DepositError( | ||||||||||||
BAD_REQUEST, | ||||||||||||
f"Cannot load metadata on {target_swhid}, this {target_type_name} " | ||||||||||||
f"object does not exist in the archive (yet?).", | ||||||||||||
) | ||||||||||||
elif target_swhid.object_type in (ExtendedObjectType.ORIGIN,): | ||||||||||||
if None in list(self.storage.origin_get_by_sha1([target_swhid.object_id])): | ||||||||||||
raise DepositError( | ||||||||||||
BAD_REQUEST, | ||||||||||||
"Cannot load metadata on origin, it is not (yet?) known to the " | ||||||||||||
"archive.", | ||||||||||||
) | ||||||||||||
else: | ||||||||||||
# This should not happen, because target_swhid is generated from either | ||||||||||||
# a core swhid or an origin URL. | ||||||||||||
# Let's just check it again so the "switch" is exhaustive. | ||||||||||||
Not Done Inline ActionsMissing test case. ardumont: Missing test case. | ||||||||||||
Done Inline ActionsActually, that code is unreachable, because it's an ExtendedSWHID converted from a QualifiedSWHID, so swh:1:ori and swh:1:emd are caught ahead of that. vlorentz: Actually, that code is unreachable, because it's an ExtendedSWHID converted from a… | ||||||||||||
Not Done Inline Actionsthen remove it? ardumont: then remove it? | ||||||||||||
Done Inline ActionsI'll make it a 500 error vlorentz: I'll make it a 500 error | ||||||||||||
Not Done Inline Actionsok then (i gather in another diff ;) ardumont: ok then (i gather in another diff ;) | ||||||||||||
raise ValueError( | ||||||||||||
f"_check_swhid_in_archive expected core SWHID or origin SWHID, " | ||||||||||||
f"but got {target_swhid}." | ||||||||||||
) | ||||||||||||
def _atom_entry( | def _atom_entry( | |||||||||||
self, | self, | |||||||||||
request: Request, | request: Request, | |||||||||||
headers: ParsedRequestHeaders, | headers: ParsedRequestHeaders, | |||||||||||
collection_name: str, | collection_name: str, | |||||||||||
deposit: Deposit, | deposit: Deposit, | |||||||||||
replace_metadata: bool = False, | replace_metadata: bool = False, | |||||||||||
replace_archives: bool = False, | replace_archives: bool = False, | |||||||||||
▲ Show 20 Lines • Show All 489 Lines • Show Last 20 Lines |
Why such formulation?
just a plain yet is enough (same goes for the next occurrence ;)