Page MenuHomeSoftware Heritage
Paste P1180

(An Untitled Masterwork)
ActivePublic

Authored by anlambert on Sep 28 2021, 4:48 PM.
diff --git a/swh/web/api/views/origin_save.py b/swh/web/api/views/origin_save.py
index a2d3366f..6cba7e4e 100644
--- a/swh/web/api/views/origin_save.py
+++ b/swh/web/api/views/origin_save.py
@@ -11,6 +11,7 @@ from swh.web.common.origin_save import (
get_savable_visit_types,
get_save_origin_requests,
)
+from swh.web.config import get_config
def _savable_visit_types():
@@ -22,90 +23,92 @@ def _savable_visit_types():
return docstring
-@api_route(
- r"/origin/save/(?P<visit_type>.+)/url/(?P<origin_url>.+)/",
- "api-1-save-origin",
- methods=["GET", "POST"],
- throttle_scope="swh_save_origin",
- never_cache=True,
-)
-@api_doc("/origin/save/")
-@format_docstring(visit_types=_savable_visit_types())
-def api_save_origin(request, visit_type, origin_url):
- """
- .. http:get:: /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
- or check the status of previously created save requests.
-
- That endpoint enables to create a saving task for a software origin
- through a POST request.
-
- Depending of the provided origin url, the save request can either be:
-
- * immediately **accepted**, for well known code hosting providers
- like for instance GitHub or GitLab
- * **rejected**, in case the url is blacklisted by Software Heritage
- * **put in pending state** until a manual check is done in order to
- determine if it can be loaded or not
-
- Once a saving request has been accepted, its associated saving task
- status can then be checked through a GET request on the same url.
- Returned status can either be:
-
- * **not created**: no saving task has been created
- * **not yet scheduled**: saving task has been created but its
- execution has not yet been scheduled
- * **scheduled**: the task execution has been scheduled
- * **succeeded**: the saving task has been successfully executed
- * **failed**: the saving task has been executed but it failed
-
- When issuing a POST request an object will be returned while a GET
- request will return an array of objects (as multiple save requests
- might have been submitted for the same origin).
-
- :param string visit_type: the type of visit to perform
- (currently the supported types are {visit_types})
- :param string origin_url: the url of the origin to save
-
- {common_headers}
-
- :>json string origin_url: the url of the origin to save
- :>json string visit_type: the type of visit to perform
- :>json string save_request_date: the date (in iso format) the save
- request was issued
- :>json string save_request_status: the status of the save request,
- either **accepted**, **rejected** or **pending**
- :>json string save_task_status: the status of the origin saving task,
- either **not created**, **not yet scheduled**, **scheduled**,
- **succeeded** or **failed**
- :>json string visit_date: the date (in iso format) of the visit if a visit
- occurred, null otherwise.
- :>json string visit_status: the status of the visit, either **full**,
- **partial**, **not_found** or **failed** if a visit occurred, null
- otherwise.
-
- :statuscode 200: no error
- :statuscode 400: an invalid visit type or origin url has been provided
- :statuscode 403: the provided origin url is blacklisted
- :statuscode 404: no save requests have been found for a given origin
-
- """
-
- data = request.data or {}
- if request.method == "POST":
- sor = create_save_origin_request(
- visit_type,
- origin_url,
- privileged_user(request),
- user_id=request.user.id,
- **data,
- )
- del sor["id"]
- else:
- sor = get_save_origin_requests(visit_type, origin_url)
- for s in sor:
- del s["id"]
-
- return sor
+if get_config()["scheduler_available"]:
+
+ @api_route(
+ r"/origin/save/(?P<visit_type>.+)/url/(?P<origin_url>.+)/",
+ "api-1-save-origin",
+ methods=["GET", "POST"],
+ throttle_scope="swh_save_origin",
+ never_cache=True,
+ )
+ @api_doc("/origin/save/")
+ @format_docstring(visit_types=_savable_visit_types())
+ def api_save_origin(request, visit_type, origin_url):
+ """
+ .. http:get:: /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
+ or check the status of previously created save requests.
+
+ That endpoint enables to create a saving task for a software origin
+ through a POST request.
+
+ Depending of the provided origin url, the save request can either be:
+
+ * immediately **accepted**, for well known code hosting providers
+ like for instance GitHub or GitLab
+ * **rejected**, in case the url is blacklisted by Software Heritage
+ * **put in pending state** until a manual check is done in order to
+ determine if it can be loaded or not
+
+ Once a saving request has been accepted, its associated saving task
+ status can then be checked through a GET request on the same url.
+ Returned status can either be:
+
+ * **not created**: no saving task has been created
+ * **not yet scheduled**: saving task has been created but its
+ execution has not yet been scheduled
+ * **scheduled**: the task execution has been scheduled
+ * **succeeded**: the saving task has been successfully executed
+ * **failed**: the saving task has been executed but it failed
+
+ When issuing a POST request an object will be returned while a GET
+ request will return an array of objects (as multiple save requests
+ might have been submitted for the same origin).
+
+ :param string visit_type: the type of visit to perform
+ (currently the supported types are {visit_types})
+ :param string origin_url: the url of the origin to save
+
+ {common_headers}
+
+ :>json string origin_url: the url of the origin to save
+ :>json string visit_type: the type of visit to perform
+ :>json string save_request_date: the date (in iso format) the save
+ request was issued
+ :>json string save_request_status: the status of the save request,
+ either **accepted**, **rejected** or **pending**
+ :>json string save_task_status: the status of the origin saving task,
+ either **not created**, **not yet scheduled**, **scheduled**,
+ **succeeded** or **failed**
+ :>json string visit_date: the date (in iso format) of the visit if a visit
+ occurred, null otherwise.
+ :>json string visit_status: the status of the visit, either **full**,
+ **partial**, **not_found** or **failed** if a visit occurred, null
+ otherwise.
+
+ :statuscode 200: no error
+ :statuscode 400: an invalid visit type or origin url has been provided
+ :statuscode 403: the provided origin url is blacklisted
+ :statuscode 404: no save requests have been found for a given origin
+
+ """
+
+ data = request.data or {}
+ if request.method == "POST":
+ sor = create_save_origin_request(
+ visit_type,
+ origin_url,
+ privileged_user(request),
+ user_id=request.user.id,
+ **data,
+ )
+ del sor["id"]
+ else:
+ sor = get_save_origin_requests(visit_type, origin_url)
+ for s in sor:
+ del s["id"]
+
+ return sor
diff --git a/swh/web/common/utils.py b/swh/web/common/utils.py
index 34d3a65a..421e41c4 100644
--- a/swh/web/common/utils.py
+++ b/swh/web/common/utils.py
@@ -285,6 +285,7 @@ def context_processor(request):
),
"swh_web_version": get_distribution("swh.web").version,
"iframe_mode": False,
+ "scheduler_available": config["scheduler_available"],
}
diff --git a/swh/web/config.py b/swh/web/config.py
index 70e68f51..872abef4 100644
--- a/swh/web/config.py
+++ b/swh/web/config.py
@@ -89,6 +89,7 @@ DEFAULT_CONFIG = {
),
"vault": ("dict", {"cls": "remote", "args": {"url": "http://127.0.0.1:5005/",}}),
"scheduler": ("dict", {"cls": "remote", "url": "http://127.0.0.1:5008/"}),
+ "scheduler_available": ("bool", False),
"development_db": ("string", os.path.join(SETTINGS_DIR, "db.sqlite3")),
"test_db": ("string", os.path.join(SETTINGS_DIR, "testdb.sqlite3")),
"production_db": ("dict", {"name": "swh-web"}),
diff --git a/swh/web/templates/layout.html b/swh/web/templates/layout.html
index d9b52a33..f1532856 100644
--- a/swh/web/templates/layout.html
+++ b/swh/web/templates/layout.html
@@ -202,12 +202,14 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
<p>Downloads</p>
</a>
</li>
- <li class="nav-item swh-origin-save-item" title="Request the saving of a software origin into the archive">
- <a href="{% url 'origin-save' %}" class="nav-link swh-origin-save-link">
- <i style="color: #e20026;" class="nav-icon mdi mdi-24px mdi-camera"></i>
- <p>Save code now</p>
- </a>
- </li>
+ {% if scheduler_available %}
+ <li class="nav-item swh-origin-save-item" title="Request the saving of a software origin into the archive">
+ <a href="{% url 'origin-save' %}" class="nav-link swh-origin-save-link">
+ <i style="color: #e20026;" class="nav-icon mdi mdi-24px mdi-camera"></i>
+ <p>Save code now</p>
+ </a>
+ </li>
+ {% endif %}
<li class="nav-item swh-help-item" title="How to browse the archive ?">
{% if user.is_authenticated and user.is_staff or "swh.ambassador" in user.get_all_permissions %}
<a href="#" class="nav-link swh-help-link" onclick="swh.guided_tour.guidedTourButtonClick(event)">
@@ -220,12 +222,14 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
</li>
{% if user.is_authenticated and user.is_staff %}
<li class="nav-header">Administration</li>
- <li class="nav-item swh-origin-save-admin-item" title="Save code now administration">
- <a href="{% url 'admin-origin-save' %}" class="nav-link swh-origin-save-admin-link">
- <i style="color: #fecd1b;" class="nav-icon mdi mdi-24px mdi-camera"></i>
- <p>Save code now</p>
- </a>
- </li>
+ {% if scheduler_available %}
+ <li class="nav-item swh-origin-save-admin-item" title="Save code now administration">
+ <a href="{% url 'admin-origin-save' %}" class="nav-link swh-origin-save-admin-link">
+ <i style="color: #fecd1b;" class="nav-icon mdi mdi-24px mdi-camera"></i>
+ <p>Save code now</p>
+ </a>
+ </li>
+ {% endif %}
<li class="nav-item swh-deposit-admin-item" title="Deposit administration">
<a href="{% url 'admin-deposit' %}" class="nav-link swh-deposit-admin-link">
<i style="color: #fecd1b;" class="nav-icon mdi mdi-24px mdi-folder-upload"></i>