Changeset View
Changeset View
Standalone View
Standalone View
swh/deposit/api/common.py
Show First 20 Lines • Show All 740 Lines • ▼ Show 20 Lines | ) -> Receipt: | ||||
if metadata is None: | if metadata is None: | ||||
raise DepositError( | raise DepositError( | ||||
BAD_REQUEST, | BAD_REQUEST, | ||||
"Empty body request is not supported", | "Empty body request is not supported", | ||||
"Atom entry deposit is supposed to send for metadata. " | "Atom entry deposit is supposed to send for metadata. " | ||||
"If the body is empty, there is no metadata.", | "If the body is empty, there is no metadata.", | ||||
) | ) | ||||
create_origin = metadata.get("swh:deposit", {}).get("swh:create_origin") | self._set_deposit_origin_from_metadata(deposit, metadata, headers) | ||||
add_to_origin = metadata.get("swh:deposit", {}).get("swh:add_to_origin") | |||||
if create_origin and add_to_origin: | |||||
raise DepositError( | |||||
BAD_REQUEST, | |||||
"<swh:create_origin> and <swh:add_to_origin> are mutually exclusive, " | |||||
"as they respectively create a new origin and add to an existing " | |||||
"origin.", | |||||
) | |||||
if create_origin: | |||||
origin_url = create_origin["swh:origin"]["@url"] | |||||
check_client_origin(deposit.client, origin_url) | |||||
deposit.origin_url = origin_url | |||||
if add_to_origin: | |||||
origin_url = add_to_origin["swh:origin"]["@url"] | |||||
check_client_origin(deposit.client, origin_url) | |||||
deposit.parent = ( | |||||
Deposit.objects.filter( | |||||
client=deposit.client, | |||||
origin_url=origin_url, | |||||
status=DEPOSIT_STATUS_LOAD_SUCCESS, | |||||
) | |||||
.order_by("-id")[0:1] | |||||
.get() | |||||
) | |||||
if "atom:external_identifier" in metadata: | |||||
# Deprecated tag. | |||||
# When clients stopped using it, this should raise an error | |||||
# unconditionally | |||||
if deposit.origin_url: | |||||
raise DepositError( | |||||
BAD_REQUEST, | |||||
"<external_identifier> is deprecated, you should only use " | |||||
"<swh:create_origin> from now on.", | |||||
) | |||||
if deposit.parent: | |||||
raise DepositError( | |||||
BAD_REQUEST, "<external_identifier> is deprecated.", | |||||
) | |||||
if headers.slug and metadata["atom:external_identifier"] != headers.slug: | |||||
raise DepositError( | |||||
BAD_REQUEST, | |||||
"The 'external_identifier' tag is deprecated, " | |||||
"the Slug header should be used instead.", | |||||
) | |||||
# Determine if we are in the metadata-only deposit case | # Determine if we are in the metadata-only deposit case | ||||
try: | try: | ||||
swhid = parse_swh_reference(metadata) | swhid = parse_swh_reference(metadata) | ||||
except ValidationError as e: | except ValidationError as e: | ||||
raise DepositError( | raise DepositError( | ||||
PARSING_ERROR, "Invalid SWHID reference", str(e), | PARSING_ERROR, "Invalid SWHID reference", str(e), | ||||
) | ) | ||||
▲ Show 20 Lines • Show All 42 Lines • ▼ Show 20 Lines | ) -> Receipt: | ||||
return Receipt( | return Receipt( | ||||
deposit_id=deposit.id, | deposit_id=deposit.id, | ||||
deposit_date=deposit.reception_date, | deposit_date=deposit.reception_date, | ||||
status=deposit.status, | status=deposit.status, | ||||
archive=None, | archive=None, | ||||
) | ) | ||||
def _set_deposit_origin_from_metadata(self, deposit, metadata, headers): | |||||
create_origin = metadata.get("swh:deposit", {}).get("swh:create_origin") | |||||
add_to_origin = metadata.get("swh:deposit", {}).get("swh:add_to_origin") | |||||
if create_origin and add_to_origin: | |||||
raise DepositError( | |||||
BAD_REQUEST, | |||||
"<swh:create_origin> and <swh:add_to_origin> are mutually exclusive, " | |||||
"as they respectively create a new origin and add to an existing " | |||||
"origin.", | |||||
) | |||||
if create_origin: | |||||
origin_url = create_origin["swh:origin"]["@url"] | |||||
check_client_origin(deposit.client, origin_url) | |||||
deposit.origin_url = origin_url | |||||
if add_to_origin: | |||||
origin_url = add_to_origin["swh:origin"]["@url"] | |||||
check_client_origin(deposit.client, origin_url) | |||||
deposit.parent = ( | |||||
Deposit.objects.filter( | |||||
client=deposit.client, | |||||
origin_url=origin_url, | |||||
status=DEPOSIT_STATUS_LOAD_SUCCESS, | |||||
) | |||||
.order_by("-id")[0:1] | |||||
olasd: This would be a behavior change: when the parent deposit doesn't exist, `[0:1].get()` throws a… | |||||
Done Inline ActionsIt should raise an error, because the alternative is silently ignoring invalid input. But I agree it should not be that error. But it's outside the scope of this diff, I just moved that code. vlorentz: It should raise an error, because the alternative is silently ignoring invalid input. But I… | |||||
.get() | |||||
) | |||||
if "atom:external_identifier" in metadata: | |||||
# Deprecated tag. | |||||
# When clients stopped using it, this should raise an error | |||||
# unconditionally | |||||
if deposit.origin_url: | |||||
raise DepositError( | |||||
BAD_REQUEST, | |||||
"<external_identifier> is deprecated, you should only use " | |||||
"<swh:create_origin> from now on.", | |||||
) | |||||
if deposit.parent: | |||||
raise DepositError( | |||||
BAD_REQUEST, "<external_identifier> is deprecated.", | |||||
) | |||||
if headers.slug and metadata["atom:external_identifier"] != headers.slug: | |||||
raise DepositError( | |||||
BAD_REQUEST, | |||||
"The 'external_identifier' tag is deprecated, " | |||||
"the Slug header should be used instead.", | |||||
) | |||||
def _empty_post( | def _empty_post( | ||||
self, | self, | ||||
request: Request, | request: Request, | ||||
headers: ParsedRequestHeaders, | headers: ParsedRequestHeaders, | ||||
collection_name: str, | collection_name: str, | ||||
deposit: Deposit, | deposit: Deposit, | ||||
) -> Receipt: | ) -> Receipt: | ||||
"""Empty post to finalize a deposit. | """Empty post to finalize a deposit. | ||||
▲ Show 20 Lines • Show All 329 Lines • Show Last 20 Lines |
This would be a behavior change: when the parent deposit doesn't exist, [0:1].get() throws a Deposit.DoesNotExist exception, which I don't see any explicit handling for, while this would just set parent to None.
Not sure which behavior is best. Either way this edge case should probably raise a DepositError?