Changeset View
Changeset View
Standalone View
Standalone View
swh/deposit/api/common.py
Show First 20 Lines • Show All 863 Lines • ▼ Show 20 Lines | ) -> Dict[str, Any]: | ||||
Returns: | Returns: | ||||
dict with 'error' detailing the problem. | dict with 'error' detailing the problem. | ||||
""" | """ | ||||
return {} | return {} | ||||
def checks( | def checks( | ||||
self, request: Request, collection_name: str, deposit_id: Optional[int] = None | self, request: Request, collection_name: str, deposit_id: Optional[int] = None | ||||
) -> Dict[str, Any]: | ) -> ParsedRequestHeaders: | ||||
try: | try: | ||||
self._collection = DepositCollection.objects.get(name=collection_name) | self._collection = DepositCollection.objects.get(name=collection_name) | ||||
except DepositCollection.DoesNotExist: | except DepositCollection.DoesNotExist: | ||||
raise DepositError(NOT_FOUND, f"Unknown collection name {collection_name}") | raise DepositError(NOT_FOUND, f"Unknown collection name {collection_name}") | ||||
assert self._collection is not None | assert self._collection is not None | ||||
username = request.user.username | username = request.user.username | ||||
if username: # unauthenticated request can have the username empty | if username: # unauthenticated request can have the username empty | ||||
Show All 26 Lines | ) -> ParsedRequestHeaders: | ||||
assert deposit is not None | assert deposit is not None | ||||
self.restrict_access(request, headers, deposit) | self.restrict_access(request, headers, deposit) | ||||
if headers.on_behalf_of: | if headers.on_behalf_of: | ||||
raise DepositError(MEDIATION_NOT_ALLOWED, "Mediation is not supported.") | raise DepositError(MEDIATION_NOT_ALLOWED, "Mediation is not supported.") | ||||
self.additional_checks(request, headers, collection_name, deposit_id) | self.additional_checks(request, headers, collection_name, deposit_id) | ||||
return {"headers": headers} | return headers | ||||
def restrict_access( | def restrict_access( | ||||
self, request: Request, headers: ParsedRequestHeaders, deposit: Deposit | self, request: Request, headers: ParsedRequestHeaders, deposit: Deposit | ||||
) -> None: | ) -> None: | ||||
"""Allow modifications on deposit with status 'partial' only, reject the rest. | """Allow modifications on deposit with status 'partial' only, reject the rest. | ||||
""" | """ | ||||
if request.method != "GET" and deposit.status != DEPOSIT_STATUS_PARTIAL: | if request.method != "GET" and deposit.status != DEPOSIT_STATUS_PARTIAL: | ||||
▲ Show 20 Lines • Show All 87 Lines • ▼ Show 20 Lines | ) -> HttpResponse: | ||||
"""Endpoint to create/add resources to deposit. | """Endpoint to create/add resources to deposit. | ||||
Returns: | Returns: | ||||
204 response when no error during routine occurred. | 204 response when no error during routine occurred. | ||||
400 if the deposit does not belong to the collection | 400 if the deposit does not belong to the collection | ||||
404 if the deposit or the collection does not exist | 404 if the deposit or the collection does not exist | ||||
""" | """ | ||||
checks = self.checks(request, collection_name, deposit_id) | headers = self.checks(request, collection_name, deposit_id) | ||||
headers = checks["headers"] | |||||
status, iri_key, data = self.process_post( | status, iri_key, data = self.process_post( | ||||
request, headers, collection_name, deposit_id | request, headers, collection_name, deposit_id | ||||
) | ) | ||||
return self._make_deposit_receipt( | return self._make_deposit_receipt( | ||||
request, collection_name, status, iri_key, data, | request, collection_name, status, iri_key, data, | ||||
) | ) | ||||
▲ Show 20 Lines • Show All 58 Lines • ▼ Show 20 Lines | ) -> HttpResponse: | ||||
"""Endpoint to update deposit resources. | """Endpoint to update deposit resources. | ||||
Returns: | Returns: | ||||
204 response when no error during routine occurred. | 204 response when no error during routine occurred. | ||||
400 if the deposit does not belong to the collection | 400 if the deposit does not belong to the collection | ||||
404 if the deposit or the collection does not exist | 404 if the deposit or the collection does not exist | ||||
""" | """ | ||||
checks = self.checks(request, collection_name, deposit_id) | headers = self.checks(request, collection_name, deposit_id) | ||||
headers = checks["headers"] | |||||
self.process_put(request, headers, collection_name, deposit_id) | self.process_put(request, headers, collection_name, deposit_id) | ||||
return HttpResponse(status=status.HTTP_204_NO_CONTENT) | return HttpResponse(status=status.HTTP_204_NO_CONTENT) | ||||
@abstractmethod | @abstractmethod | ||||
def process_put( | def process_put( | ||||
self, | self, | ||||
request: Request, | request: Request, | ||||
▲ Show 20 Lines • Show All 46 Lines • Show Last 20 Lines |