diff --git a/swh/web/admin/urls.py b/swh/web/admin/urls.py --- a/swh/web/admin/urls.py +++ b/swh/web/admin/urls.py @@ -7,10 +7,13 @@ from django.contrib.auth.views import LoginView from django.shortcuts import redirect -import swh.web.admin.add_forge_now # noqa from swh.web.admin.adminurls import AdminUrls import swh.web.admin.deposit # noqa import swh.web.admin.origin_save # noqa +from swh.web.config import is_feature_enabled + +if is_feature_enabled("add_forge_now"): + import swh.web.admin.add_forge_now # noqa def _admin_default_view(request): diff --git a/swh/web/common/utils.py b/swh/web/common/utils.py --- a/swh/web/common/utils.py +++ b/swh/web/common/utils.py @@ -315,6 +315,7 @@ "iframe_mode": False, "ADMIN_LIST_DEPOSIT_PERMISSION": ADMIN_LIST_DEPOSIT_PERMISSION, "ADD_FORGE_MODERATOR_PERMISSION": ADD_FORGE_MODERATOR_PERMISSION, + "FEATURES": get_config()["features"], } diff --git a/swh/web/config.py b/swh/web/config.py --- a/swh/web/config.py +++ b/swh/web/config.py @@ -127,6 +127,7 @@ "staging_server_names": ("list", SWH_WEB_STAGING_SERVER_NAMES), "instance_name": ("str", "archive-test.softwareheritage.org"), "give": ("dict", {"public_key": "", "token": ""}), + "features": ("dict", {"add_forge_now": False}), } swhweb_config: Dict[str, Any] = {} @@ -207,3 +208,11 @@ """ return get_config()["counters"] + + +def is_feature_enabled(feature_name: str) -> bool: + """Determine whether a feature is enabled or not. If feature_name is not found at all, + it's considered disabled. + + """ + return get_config()["features"].get(feature_name, False) diff --git a/swh/web/settings/tests.py b/swh/web/settings/tests.py --- a/swh/web/settings/tests.py +++ b/swh/web/settings/tests.py @@ -81,6 +81,7 @@ "server_url": "http://localhost:8080/auth/" if _pytest else "", "realm_name": "SoftwareHeritage", }, + "features": {"add_forge_now": True,}, } ) diff --git a/swh/web/templates/layout.html b/swh/web/templates/layout.html --- a/swh/web/templates/layout.html +++ b/swh/web/templates/layout.html @@ -209,12 +209,14 @@ <p>Save code now</p> </a> </li> + {% if FEATURES.add_forge_now %} <li class="nav-item swh-origin-save-item" title="Request adding a new forge listing"> <a href="{% url 'forge-add' %}" class="nav-link swh-forge-add-link"> <i style="color: #e20026;" class="nav-icon mdi mdi-24px mdi-anvil"></i> <p>Add forge now</p> </a> </li> + {% endif %} <li class="nav-item swh-help-item" title="How to browse the archive ?"> <a href="#" class="nav-link swh-help-link" onclick="swh.guided_tour.guidedTourButtonClick(event)"> <i style="color: #e20026;" class="nav-icon mdi mdi-24px mdi-help-circle"></i> @@ -231,13 +233,15 @@ </a> </li> {% endif %} - {% if user.is_staff or ADD_FORGE_MODERATOR_PERMISSION in user.get_all_permissions %} + {% if FEATURES.add_forge_now %} + {% if user.is_staff or ADD_FORGE_MODERATOR_PERMISSION in user.get_all_permissions %} <li class="nav-item swh-forge-add-moderation-item" title="Add forge now moderation"> <a href="{% url 'add-forge-now-requests-moderation' %}" class="nav-link swh-forge-add-moderation-link"> <i style="color: #fecd1b;" class="nav-icon mdi mdi-24px mdi-anvil"></i> <p>Add forge now</p> </a> </li> + {% endif %} {% endif %} {% if user.is_staff or ADMIN_LIST_DEPOSIT_PERMISSION in user.get_all_permissions %} <li class="nav-item swh-deposit-admin-item" title="Deposit administration"> diff --git a/swh/web/tests/test_config.py b/swh/web/tests/test_config.py new file mode 100644 --- /dev/null +++ b/swh/web/tests/test_config.py @@ -0,0 +1,23 @@ +# Copyright (C) 2022 The Software Heritage developers +# See the AUTHORS file at the top-level directory of this distribution +# License: GNU General Public License version 3, or any later version +# See top-level LICENSE file for more information + +import pytest + +from swh.web.config import get_config, is_feature_enabled + + +@pytest.mark.parametrize( + "feature_name", ["inexistant-feature", "awesome-stuff"], +) +def test_is_feature_enabled(feature_name): + config = get_config() + # by default, feature non configured are considered disabled + assert is_feature_enabled(feature_name) is False + + for enabled in [True, False]: + # Let's configure the feature + config["features"] = {feature_name: enabled} + # and check its configuration is properly read + assert is_feature_enabled(feature_name) is enabled diff --git a/swh/web/urls.py b/swh/web/urls.py --- a/swh/web/urls.py +++ b/swh/web/urls.py @@ -1,4 +1,4 @@ -# Copyright (C) 2017-2021 The Software Heritage developers +# Copyright (C) 2017-2022 The Software Heritage developers # See the AUTHORS file at the top-level directory of this distribution # License: GNU Affero General Public License version 3, or any later version # See top-level LICENSE file for more information @@ -28,7 +28,7 @@ swh_handle500, ) from swh.web.common.utils import origin_visit_types -from swh.web.config import get_config +from swh.web.config import get_config, is_feature_enabled swh_web_config = get_config() @@ -60,11 +60,13 @@ name="browse-swhid", ), url(r"^", include("swh.web.misc.urls")), - url(r"^", include("swh.web.add_forge_now.views")), url(r"^", include("swh.web.auth.views")), url(r"^logout/$", LogoutView.as_view(template_name="logout.html"), name="logout"), ] +if is_feature_enabled("add_forge_now"): + urlpatterns += (url(r"^", include("swh.web.add_forge_now.views")),) + # allow to serve assets through django staticfiles # even if settings.DEBUG is False