Changeset View
Changeset View
Standalone View
Standalone View
swh/deposit/api/deposit.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 typing import Any, Dict, Optional, Tuple | from typing import Any, Dict, Optional, Tuple | ||||
from rest_framework import status | from rest_framework import status | ||||
from ..config import EDIT_SE_IRI | from ..config import EDIT_SE_IRI | ||||
from ..errors import BAD_REQUEST, make_error_dict | |||||
from ..parsers import ( | from ..parsers import ( | ||||
SWHAtomEntryParser, | SWHAtomEntryParser, | ||||
SWHFileUploadTarParser, | SWHFileUploadTarParser, | ||||
SWHFileUploadZipParser, | SWHFileUploadZipParser, | ||||
SWHMultiPartParser, | SWHMultiPartParser, | ||||
) | ) | ||||
from .common import ACCEPT_ARCHIVE_CONTENT_TYPES, APIPost | from .common import ACCEPT_ARCHIVE_CONTENT_TYPES, APIPost | ||||
Show All 9 Lines | class APIPostDeposit(APIPost): | ||||
parser_classes = ( | parser_classes = ( | ||||
SWHMultiPartParser, | SWHMultiPartParser, | ||||
SWHFileUploadZipParser, | SWHFileUploadZipParser, | ||||
SWHFileUploadTarParser, | SWHFileUploadTarParser, | ||||
SWHAtomEntryParser, | SWHAtomEntryParser, | ||||
) | ) | ||||
def additional_checks( | |||||
self, | |||||
req, | |||||
headers: Dict[str, Any], | |||||
collection_name: str, | |||||
deposit_id: Optional[int] = None, | |||||
) -> Dict[str, Any]: | |||||
slug = headers["slug"] | |||||
if not slug: | |||||
msg = "Missing SLUG header in request" | |||||
verbose_description = "Provide in the SLUG header one identifier, for example the url pointing to the resource you are depositing." # noqa | |||||
return make_error_dict(BAD_REQUEST, msg, verbose_description) | |||||
return {} | |||||
vlorentz: why did you remove this function and duplicate its code? | |||||
Done Inline ActionsIt's not per say the same. This one is actually triggered too soon in the current workflow we want. So removing it and calling an equivalent (granted duplicated code [1]) code only when we actually want to check for the slug. [1] I can probably improve on the duplication part. ardumont: It's not per say the same.
This one is actually triggered too soon in the current workflow we… | |||||
Not Done Inline Actionsyeah, please deduplicate vlorentz: yeah, please deduplicate | |||||
Done Inline ActionsI'm realizing that it will need to happen also for other errors btw (in another diff) ardumont: I'm realizing that it will need to happen also for other errors btw (in another diff) | |||||
def process_post( | def process_post( | ||||
self, | self, | ||||
req, | req, | ||||
headers: Dict[str, Any], | headers: Dict[str, Any], | ||||
collection_name: str, | collection_name: str, | ||||
deposit_id: Optional[int] = None, | deposit_id: Optional[int] = None, | ||||
) -> Tuple[int, str, Dict[str, Any]]: | ) -> Tuple[int, str, Dict[str, Any]]: | ||||
"""Create a first deposit as: | """Create a first deposit as: | ||||
Show All 38 Lines | ) -> Tuple[int, str, Dict[str, Any]]: | ||||
identifier | identifier | ||||
- 400 (bad request) if the request's body is empty | - 400 (bad request) if the request's body is empty | ||||
- 415 (unsupported media type) if a wrong media type is | - 415 (unsupported media type) if a wrong media type is | ||||
provided | provided | ||||
""" | """ | ||||
assert deposit_id is None | assert deposit_id is None | ||||
if req.content_type in ACCEPT_ARCHIVE_CONTENT_TYPES: | if req.content_type in ACCEPT_ARCHIVE_CONTENT_TYPES: | ||||
data = self._binary_upload(req, headers, collection_name) | data = self._binary_upload( | ||||
req, headers, collection_name, check_slug_is_present=True | |||||
) | |||||
elif req.content_type.startswith("multipart/"): | elif req.content_type.startswith("multipart/"): | ||||
data = self._multipart_upload(req, headers, collection_name) | data = self._multipart_upload( | ||||
req, headers, collection_name, check_slug_is_present=True | |||||
) | |||||
else: | else: | ||||
data = self._atom_entry(req, headers, collection_name) | data = self._atom_entry( | ||||
req, headers, collection_name, check_slug_is_present=True | |||||
) | |||||
return status.HTTP_201_CREATED, EDIT_SE_IRI, data | return status.HTTP_201_CREATED, EDIT_SE_IRI, data |
why did you remove this function and duplicate its code?