Changeset View
Changeset View
Standalone View
Standalone View
swh/deposit/api/state.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 | ||||
from ..models import DEPOSIT_STATUS_DETAIL, Deposit | from .common import APIBase, get_deposit_by_id | ||||
from .common import APIBase | |||||
from .converters import convert_status_detail | from .converters import convert_status_detail | ||||
class StateAPI(APIBase): | class StateAPI(APIBase): | ||||
"""Deposit status. | """Deposit status. | ||||
What's known as 'State-IRI' in the sword specification. | What's known as 'State-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), | |||||
) | |||||
status_detail = convert_status_detail(deposit.status_detail) | status_detail = convert_status_detail(deposit.status_detail) | ||||
if not status_detail: | if not status_detail: | ||||
status_detail = DEPOSIT_STATUS_DETAIL[deposit.status] | status_detail = DEPOSIT_STATUS_DETAIL[deposit.status] | ||||
context = { | context = { | ||||
"deposit_id": deposit.id, | "deposit_id": deposit.id, | ||||
"status_detail": status_detail, | "status_detail": status_detail, | ||||
Show All 17 Lines |