Changeset View
Changeset View
Standalone View
Standalone View
swh/web/api/views/origin_save.py
# Copyright (C) 2018-2021 The Software Heritage developers | # Copyright (C) 2018-2022 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 Affero General Public License version 3, or any later version | # License: GNU Affero 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 os | import os | |||||||||
from typing import Optional, cast | ||||||||||
from rest_framework.request import Request | ||||||||||
from swh.web.api.apidoc import api_doc, format_docstring | from swh.web.api.apidoc import api_doc, format_docstring | |||||||||
from swh.web.api.apiurls import api_route | from swh.web.api.apiurls import api_route | |||||||||
from swh.web.auth.utils import ( | from swh.web.auth.utils import ( | |||||||||
API_SAVE_ORIGIN_PERMISSION, | API_SAVE_ORIGIN_PERMISSION, | |||||||||
SWH_AMBASSADOR_PERMISSION, | SWH_AMBASSADOR_PERMISSION, | |||||||||
privileged_user, | privileged_user, | |||||||||
) | ) | |||||||||
from swh.web.common.origin_save import ( | from swh.web.common.origin_save import ( | |||||||||
create_save_origin_request, | create_save_origin_request, | |||||||||
get_savable_visit_types, | get_savable_visit_types, | |||||||||
get_save_origin_requests, | get_save_origin_requests, | |||||||||
) | ) | |||||||||
def _savable_visit_types(): | def _savable_visit_types() -> str: | |||||||||
docstring = "" | docstring = "" | |||||||||
if os.environ.get("DJANGO_SETTINGS_MODULE") != "swh.web.settings.tests": | if os.environ.get("DJANGO_SETTINGS_MODULE") != "swh.web.settings.tests": | |||||||||
visit_types = sorted(get_savable_visit_types()) | visit_types = sorted(get_savable_visit_types()) | |||||||||
docstring = "" | docstring = "" | |||||||||
for visit_type in visit_types[:-1]: | for visit_type in visit_types[:-1]: | |||||||||
docstring += f"**{visit_type}**, " | docstring += f"**{visit_type}**, " | |||||||||
docstring += f"and **{visit_types[-1]}**" | docstring += f"and **{visit_types[-1]}**" | |||||||||
return docstring | return docstring | |||||||||
@api_route( | @api_route( | |||||||||
r"/origin/save/(?P<visit_type>.+)/url/(?P<origin_url>.+)/", | r"/origin/save/(?P<visit_type>.+)/url/(?P<origin_url>.+)/", | |||||||||
"api-1-save-origin", | "api-1-save-origin", | |||||||||
methods=["GET", "POST"], | methods=["GET", "POST"], | |||||||||
throttle_scope="swh_save_origin", | throttle_scope="swh_save_origin", | |||||||||
never_cache=True, | never_cache=True, | |||||||||
) | ) | |||||||||
@api_doc("/origin/save/") | @api_doc("/origin/save/") | |||||||||
@format_docstring(visit_types=_savable_visit_types()) | @format_docstring(visit_types=_savable_visit_types()) | |||||||||
def api_save_origin(request, visit_type, origin_url): | def api_save_origin(request: Request, visit_type: str, origin_url: str): | |||||||||
""" | """ | |||||||||
.. http:get:: /api/1/origin/save/(visit_type)/url/(origin_url)/ | .. http:get:: /api/1/origin/save/(visit_type)/url/(origin_url)/ | |||||||||
.. http:post:: /api/1/origin/save/(visit_type)/url/(origin_url)/ | .. http:post:: /api/1/origin/save/(visit_type)/url/(origin_url)/ | |||||||||
Request the saving of a software origin into the archive | Request the saving of a software origin into the archive | |||||||||
or check the status of previously created save requests. | or check the status of previously created save requests. | |||||||||
That endpoint enables to create a saving task for a software origin | That endpoint enables to create a saving task for a software origin | |||||||||
▲ Show 20 Lines • Show All 56 Lines • ▼ Show 20 Lines | def api_save_origin(request: Request, visit_type: str, origin_url: str): | |||||||||
if request.method == "POST": | if request.method == "POST": | |||||||||
sor = create_save_origin_request( | sor = create_save_origin_request( | |||||||||
visit_type, | visit_type, | |||||||||
origin_url, | origin_url, | |||||||||
privileged_user( | privileged_user( | |||||||||
request, | request, | |||||||||
permissions=[SWH_AMBASSADOR_PERMISSION, API_SAVE_ORIGIN_PERMISSION], | permissions=[SWH_AMBASSADOR_PERMISSION, API_SAVE_ORIGIN_PERMISSION], | |||||||||
), | ), | |||||||||
user_id=request.user.id, | user_id=cast(Optional[int], request.user.id), | |||||||||
vlorentzUnsubmitted Not Done Inline Actions
vlorentz: | ||||||||||
**data, | **data, | |||||||||
) | ) | |||||||||
del sor["id"] | del sor["id"] | |||||||||
else: | ||||||||||
sor = get_save_origin_requests(visit_type, origin_url) | ||||||||||
for s in sor: | ||||||||||
del s["id"] | ||||||||||
return sor | return sor | |||||||||
else: | ||||||||||
sors = get_save_origin_requests(visit_type, origin_url) | ||||||||||
for sor in sors: | ||||||||||
del sor["id"] | ||||||||||
return sors |