Changeset View
Changeset View
Standalone View
Standalone View
swh/deposit/api/common.py
# Copyright (C) 2017-2019 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 | ||||
import hashlib | import hashlib | ||||
from typing import Sequence, Type | from typing import Sequence, Type | ||||
from abc import ABCMeta, abstractmethod | from abc import ABCMeta, abstractmethod | ||||
from django.urls import reverse | from django.urls import reverse | ||||
from django.http import HttpResponse | from django.http import HttpResponse | ||||
from django.shortcuts import render | from django.shortcuts import render | ||||
from django.utils import timezone | from django.utils import timezone | ||||
from rest_framework import status | from rest_framework import status | ||||
from rest_framework.authentication import BaseAuthentication, BasicAuthentication | from rest_framework.authentication import BaseAuthentication, BasicAuthentication | ||||
from rest_framework.permissions import BasePermission, IsAuthenticated | from rest_framework.permissions import BasePermission, IsAuthenticated | ||||
from rest_framework.views import APIView | from rest_framework.views import APIView | ||||
from swh.model import hashutil | from swh.model import hashutil | ||||
from swh.scheduler.utils import create_oneshot_task_dict | from swh.scheduler.utils import create_oneshot_task_dict | ||||
from ..config import ( | from ..config import ( | ||||
SWHDefaultConfig, | APIConfig, | ||||
EDIT_SE_IRI, | EDIT_SE_IRI, | ||||
EM_IRI, | EM_IRI, | ||||
CONT_FILE_IRI, | CONT_FILE_IRI, | ||||
ARCHIVE_KEY, | ARCHIVE_KEY, | ||||
METADATA_KEY, | METADATA_KEY, | ||||
RAW_METADATA_KEY, | RAW_METADATA_KEY, | ||||
STATE_IRI, | STATE_IRI, | ||||
DEPOSIT_STATUS_DEPOSITED, | DEPOSIT_STATUS_DEPOSITED, | ||||
Show All 20 Lines | |||||
from ..models import Deposit, DepositRequest, DepositCollection, DepositClient | from ..models import Deposit, DepositRequest, DepositCollection, DepositClient | ||||
from ..parsers import parse_xml | from ..parsers import parse_xml | ||||
ACCEPT_PACKAGINGS = ["http://purl.org/net/sword/package/SimpleZip"] | ACCEPT_PACKAGINGS = ["http://purl.org/net/sword/package/SimpleZip"] | ||||
ACCEPT_ARCHIVE_CONTENT_TYPES = ["application/zip", "application/x-tar"] | ACCEPT_ARCHIVE_CONTENT_TYPES = ["application/zip", "application/x-tar"] | ||||
class SWHAPIView(APIView): | class AuthenticatedAPIView(APIView): | ||||
"""Mixin intended as a based API view to enforce the basic | """Mixin intended as a based API view to enforce the basic | ||||
authentication check | authentication check | ||||
""" | """ | ||||
authentication_classes: Sequence[Type[BaseAuthentication]] = (BasicAuthentication,) | authentication_classes: Sequence[Type[BaseAuthentication]] = (BasicAuthentication,) | ||||
permission_classes: Sequence[Type[BasePermission]] = (IsAuthenticated,) | permission_classes: Sequence[Type[BasePermission]] = (IsAuthenticated,) | ||||
class SWHBaseDeposit(SWHDefaultConfig, SWHAPIView, metaclass=ABCMeta): | class APIBase(APIConfig, AuthenticatedAPIView, metaclass=ABCMeta): | ||||
"""Base deposit request class sharing multiple common behaviors. | """Base deposit request class sharing multiple common behaviors. | ||||
""" | """ | ||||
def _read_headers(self, request): | def _read_headers(self, request): | ||||
"""Read and unify the necessary headers from the request (those are | """Read and unify the necessary headers from the request (those are | ||||
not stored in the same location or not properly formatted). | not stored in the same location or not properly formatted). | ||||
▲ Show 20 Lines • Show All 712 Lines • ▼ Show 20 Lines | class APIBase(APIConfig, AuthenticatedAPIView, metaclass=ABCMeta): | ||||
def put(self, request, *args, **kwargs): | def put(self, request, *args, **kwargs): | ||||
return self._basic_not_allowed_method(request, "PUT") | return self._basic_not_allowed_method(request, "PUT") | ||||
def delete(self, request, *args, **kwargs): | def delete(self, request, *args, **kwargs): | ||||
return self._basic_not_allowed_method(request, "DELETE") | return self._basic_not_allowed_method(request, "DELETE") | ||||
class SWHGetDepositAPI(SWHBaseDeposit, metaclass=ABCMeta): | class APIGet(APIBase, metaclass=ABCMeta): | ||||
"""Mixin for class to support GET method. | """Mixin for class to support GET method. | ||||
""" | """ | ||||
def get(self, request, collection_name, deposit_id, format=None): | def get(self, request, collection_name, deposit_id, format=None): | ||||
"""Endpoint to create/add resources to deposit. | """Endpoint to create/add resources to deposit. | ||||
Returns: | Returns: | ||||
Show All 20 Lines | def process_get(self, request, collection_name, deposit_id): | ||||
Returns: | Returns: | ||||
Tuple status, stream of content, content-type | Tuple status, stream of content, content-type | ||||
""" | """ | ||||
pass | pass | ||||
class SWHPostDepositAPI(SWHBaseDeposit, metaclass=ABCMeta): | class APIPost(APIBase, metaclass=ABCMeta): | ||||
"""Mixin for class to support DELETE method. | """Mixin for class to support DELETE method. | ||||
""" | """ | ||||
def post(self, request, collection_name, deposit_id=None, format=None): | def post(self, request, collection_name, deposit_id=None, format=None): | ||||
"""Endpoint to create/add resources to deposit. | """Endpoint to create/add resources to deposit. | ||||
Returns: | Returns: | ||||
Show All 37 Lines | def process_post(self, request, headers, collection_name, deposit_id=None): | ||||
- response status code (200, 201, etc...) | - response status code (200, 201, etc...) | ||||
- key iri (EM_IRI, EDIT_SE_IRI, etc...) | - key iri (EM_IRI, EDIT_SE_IRI, etc...) | ||||
- dictionary of the processing result | - dictionary of the processing result | ||||
""" | """ | ||||
pass | pass | ||||
class SWHPutDepositAPI(SWHBaseDeposit, metaclass=ABCMeta): | class APIPut(APIBase, metaclass=ABCMeta): | ||||
"""Mixin for class to support PUT method. | """Mixin for class to support PUT method. | ||||
""" | """ | ||||
def put(self, request, collection_name, deposit_id, format=None): | def put(self, request, collection_name, deposit_id, format=None): | ||||
"""Endpoint to update deposit resources. | """Endpoint to update deposit resources. | ||||
Returns: | Returns: | ||||
Show All 21 Lines | def process_put(self, request, headers, collection_name, deposit_id): | ||||
Returns | Returns | ||||
dictionary of the processing result | dictionary of the processing result | ||||
""" | """ | ||||
pass | pass | ||||
class SWHDeleteDepositAPI(SWHBaseDeposit, metaclass=ABCMeta): | class APIDelete(APIBase, metaclass=ABCMeta): | ||||
"""Mixin for class to support DELETE method. | """Mixin for class to support DELETE method. | ||||
""" | """ | ||||
def delete(self, request, collection_name, deposit_id): | def delete(self, request, collection_name, deposit_id): | ||||
"""Endpoint to delete some deposit's resources (archives, deposit). | """Endpoint to delete some deposit's resources (archives, deposit). | ||||
Returns: | Returns: | ||||
Show All 13 Lines | def delete(self, request, collection_name, deposit_id): | ||||
return HttpResponse(status=status.HTTP_204_NO_CONTENT) | return HttpResponse(status=status.HTTP_204_NO_CONTENT) | ||||
@abstractmethod | @abstractmethod | ||||
def process_delete(self, request, collection_name, deposit_id): | def process_delete(self, request, collection_name, deposit_id): | ||||
"""Routine to delete a resource. | """Routine to delete a resource. | ||||
This is mostly not allowed except for the | This is mostly not allowed except for the | ||||
EM_IRI (cf. .api.deposit_update.SWHUpdateArchiveDeposit) | EM_IRI (cf. .api.deposit_update.APIUpdateArchive) | ||||
""" | """ | ||||
pass | pass |