Changeset View
Changeset View
Standalone View
Standalone View
swh/deposit/api/private/__init__.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, List, Tuple | from typing import Any, Dict, List, Tuple | ||||
from rest_framework.permissions import AllowAny | from rest_framework.permissions import AllowAny | ||||
from swh.deposit import utils | from swh.deposit import utils | ||||
from swh.deposit.api.common import AuthenticatedAPIView | from swh.deposit.api.common import AuthenticatedAPIView | ||||
from swh.deposit.errors import NOT_FOUND, make_error_dict | from swh.deposit.errors import NOT_FOUND, DepositError | ||||
from ...config import METADATA_TYPE, APIConfig | from ...config import METADATA_TYPE, APIConfig | ||||
from ...models import Deposit, DepositRequest | from ...models import Deposit, DepositRequest | ||||
class DepositReadMixin: | class DepositReadMixin: | ||||
"""Deposit Read mixin | """Deposit Read mixin | ||||
▲ Show 20 Lines • Show All 55 Lines • ▼ Show 20 Lines | class APIPrivateView(APIConfig, AuthenticatedAPIView): | ||||
def checks(self, req, collection_name, deposit_id=None): | def checks(self, req, collection_name, deposit_id=None): | ||||
"""Override default checks implementation to allow empty collection. | """Override default checks implementation to allow empty collection. | ||||
""" | """ | ||||
if deposit_id: | if deposit_id: | ||||
try: | try: | ||||
Deposit.objects.get(pk=deposit_id) | Deposit.objects.get(pk=deposit_id) | ||||
except Deposit.DoesNotExist: | except Deposit.DoesNotExist: | ||||
return make_error_dict( | raise DepositError( | ||||
NOT_FOUND, "Deposit with id %s does not exist" % deposit_id | NOT_FOUND, "Deposit with id %s does not exist" % deposit_id | ||||
) | ) | ||||
headers = self._read_headers(req) | headers = self._read_headers(req) | ||||
checks = self.additional_checks(req, headers, collection_name, deposit_id) | self.additional_checks(req, headers, collection_name, deposit_id) | ||||
if "error" in checks: | |||||
return checks | |||||
return {"headers": headers} | return {"headers": headers} | ||||
def get( | def get( | ||||
self, request, collection_name=None, deposit_id=None, *args, **kwargs, | self, request, collection_name=None, deposit_id=None, *args, **kwargs, | ||||
): | ): | ||||
return super().get(request, collection_name, deposit_id) | return super().get(request, collection_name, deposit_id) | ||||
def put( | def put( | ||||
self, request, collection_name=None, deposit_id=None, *args, **kwargs, | self, request, collection_name=None, deposit_id=None, *args, **kwargs, | ||||
): | ): | ||||
return super().put(request, collection_name, deposit_id) | return super().put(request, collection_name, deposit_id) |