Changeset View
Changeset View
Standalone View
Standalone View
swh/deposit/api/content.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 django.http import HttpResponse | from django.http import HttpResponse | ||||
from django.shortcuts import render | from django.shortcuts import render | ||||
from rest_framework import status | from rest_framework import status | ||||
from ..errors import NOT_FOUND, make_error_response | from ..models import DEPOSIT_STATUS_DETAIL, DepositRequest | ||||
from ..models import DEPOSIT_STATUS_DETAIL, Deposit, DepositRequest | from .common import APIBase, get_deposit_by_id | ||||
from .common import APIBase | |||||
class ContentAPI(APIBase): | class ContentAPI(APIBase): | ||||
"""Deposit request class defining api endpoints for sword deposit. | """Deposit request class defining api endpoints for sword deposit. | ||||
What's known as 'Cont-IRI' and 'File-IRI' in the sword specification. | What's known as 'Cont-IRI' and 'File-IRI' in the sword specification. | ||||
HTTP verbs supported: GET | HTTP verbs supported: GET | ||||
""" | """ | ||||
def get(self, req, collection_name: str, deposit_id: int) -> HttpResponse: | def get(self, req, collection_name: str, deposit_id: int) -> HttpResponse: | ||||
self.checks(req, collection_name, deposit_id) | self.checks(req, collection_name, deposit_id) | ||||
try: | deposit = get_deposit_by_id(deposit_id, collection_name) | ||||
deposit = Deposit.objects.get(pk=deposit_id) | |||||
if deposit.collection.name != collection_name: | |||||
raise Deposit.DoesNotExist | |||||
except Deposit.DoesNotExist: | |||||
return make_error_response( | |||||
req, | |||||
NOT_FOUND, | |||||
"deposit %s does not belong to collection %s" | |||||
% (deposit_id, collection_name), | |||||
) | |||||
requests = DepositRequest.objects.filter(deposit=deposit) | requests = DepositRequest.objects.filter(deposit=deposit) | ||||
context = { | context = { | ||||
"deposit_id": deposit.id, | "deposit_id": deposit.id, | ||||
"status": deposit.status, | "status": deposit.status, | ||||
"status_detail": DEPOSIT_STATUS_DETAIL[deposit.status], | "status_detail": DEPOSIT_STATUS_DETAIL[deposit.status], | ||||
"requests": requests, | "requests": requests, | ||||
} | } | ||||
return render( | return render( | ||||
req, | req, | ||||
"deposit/content.xml", | "deposit/content.xml", | ||||
context=context, | context=context, | ||||
content_type="application/xml", | content_type="application/xml", | ||||
status=status.HTTP_200_OK, | status=status.HTTP_200_OK, | ||||
) | ) |