diff --git a/swh/deposit/api/collection.py b/swh/deposit/api/collection.py --- a/swh/deposit/api/collection.py +++ b/swh/deposit/api/collection.py @@ -92,7 +92,7 @@ """ assert deposit is None - deposit = self._deposit_create(collection_name, external_id=headers.slug) + deposit = self._deposit_create(req, collection_name, external_id=headers.slug) if req.content_type in ACCEPT_ARCHIVE_CONTENT_TYPES: receipt = self._binary_upload( @@ -110,17 +110,20 @@ return status.HTTP_201_CREATED, EDIT_IRI, receipt def _deposit_create( - self, collection_name: str, external_id: Optional[str] + self, request, collection_name: str, external_id: Optional[str] ) -> Deposit: collection = get_collection_by_name(collection_name) + client = self.get_client(request) deposit_parent: Optional[Deposit] = None + assert client + if external_id: try: # find a deposit parent (same external id, status load to success) deposit_parent = ( Deposit.objects.filter( - client=self._client, + client=client, external_id=external_id, status=DEPOSIT_STATUS_LOAD_SUCCESS, ) @@ -134,6 +137,6 @@ return Deposit( collection=collection, external_id=external_id or "", - client=self._client, + client=client, parent=deposit_parent, ) diff --git a/swh/deposit/api/common.py b/swh/deposit/api/common.py --- a/swh/deposit/api/common.py +++ b/swh/deposit/api/common.py @@ -152,6 +152,8 @@ """ + _client: Optional[DepositClient] = None + def _read_headers(self, request: Request) -> ParsedRequestHeaders: """Read and unify the necessary headers from the request (those are not stored in the same location or not properly formatted). @@ -858,6 +860,24 @@ """ return {} + def get_client(self, request) -> Optional[DepositClient]: + """Returns a DepositClient if request.user.username is not None""" + username = request.user.username + if username is None: + return None + + if self._client is None: + try: + self._client = DepositClient.objects.get( # type: ignore + username=username + ) + except DepositClient.DoesNotExist: + raise DepositError(NOT_FOUND, f"Unknown client name {username}") + + assert self._client.username == username + + return self._client + def checks( self, request: Request, collection_name: str, deposit: Optional[Deposit] = None ) -> ParsedRequestHeaders: @@ -867,22 +887,16 @@ assert collection_name == deposit.collection.name collection = deposit.collection - username = request.user.username - if username: # unauthenticated request can have the username empty - try: - self._client: DepositClient = DepositClient.objects.get( # type: ignore - username=username - ) - except DepositClient.DoesNotExist: - raise DepositError(NOT_FOUND, f"Unknown client name {username}") - + client = self.get_client(request) + if client: # unauthenticated request can have the username empty collection_id = collection.id - collections = self._client.collections + collections = client.collections assert collections is not None if collection_id not in collections: raise DepositError( FORBIDDEN, - f"Client {username} cannot access collection {collection_name}", + f"Client {client.username} cannot access collection " + f"{collection_name}", ) headers = self._read_headers(request)