diff --git a/swh/web/misc/badges.py b/swh/web/badges/__init__.py rename from swh/web/misc/badges.py rename to swh/web/badges/__init__.py --- a/swh/web/misc/badges.py +++ b/swh/web/badges/__init__.py @@ -10,7 +10,6 @@ from django.contrib.staticfiles import finders from django.http import HttpRequest, HttpResponse -from django.urls import re_path as url from swh.model.exceptions import ValidationError from swh.model.hashutil import hash_to_bytes, hash_to_hex @@ -25,7 +24,7 @@ _swh_logo_data = None -_badge_config = { +badge_config = { "content": { "color": _blue, "title": "Archived source file", @@ -54,7 +53,7 @@ } -def _get_logo_data() -> str: +def get_logo_data() -> str: """ Get data-URI for Software Heritage SVG logo to embed it in the generated badges. @@ -69,7 +68,7 @@ return _swh_logo_data -def _swh_badge( +def swh_badge( request: HttpRequest, object_type: str, object_id: str, @@ -145,17 +144,17 @@ badge_data = badge( left_text=left_text, right_text=right_text, - right_color=_badge_config[object_type]["color"], + right_color=badge_config[object_type]["color"], whole_link=request.build_absolute_uri(whole_link), - whole_title=_badge_config[object_type]["title"], - logo=_get_logo_data(), + whole_title=badge_config[object_type]["title"], + logo=get_logo_data(), embed_logo=True, ) return HttpResponse(badge_data, content_type="image/svg+xml") -def _swh_badge_swhid(request: HttpRequest, object_swhid: str) -> HttpResponse: +def swh_badge_swhid(request: HttpRequest, object_swhid: str) -> HttpResponse: """ Generate a Software Heritage badge for a given object SWHID. @@ -168,18 +167,4 @@ *image/svg+xml* containing the SVG badge data. If any error occurs, a status code of 400 will be returned. """ - return _swh_badge(request, "", "", object_swhid) - - -urlpatterns = [ - url( - r"^badge/(?P[a-z]+)/(?P.+)/$", - _swh_badge, - name="swh-badge", - ), - url( - r"^badge/(?Pswh:[0-9]+:[a-z]+:[0-9a-f]+.*)/$", - _swh_badge_swhid, - name="swh-badge-swhid", - ), -] + return swh_badge(request, "", "", object_swhid) diff --git a/swh/web/badges/urls.py b/swh/web/badges/urls.py new file mode 100644 --- /dev/null +++ b/swh/web/badges/urls.py @@ -0,0 +1,21 @@ +# Copyright (C) 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 + +from django.urls import re_path as url + +from swh.web.badges import swh_badge, swh_badge_swhid + +urlpatterns = [ + url( + r"^badge/(?P[a-z]+)/(?P.+)/$", + swh_badge, + name="swh-badge", + ), + url( + r"^badge/(?Pswh:[0-9]+:[a-z]+:[0-9a-f]+.*)/$", + swh_badge_swhid, + name="swh-badge-swhid", + ), +] diff --git a/swh/web/browse/templates/includes/show-swhids.html b/swh/web/browse/templates/includes/show-swhids.html --- a/swh/web/browse/templates/includes/show-swhids.html +++ b/swh/web/browse/templates/includes/show-swhids.html @@ -62,22 +62,26 @@
{% if not iframe_mode %} -
- {% if snapshot_context and snapshot_context.origin_info %} - - {% endif %} - {% if swhid_info.object_id %} - +
+ {% if "swh.web.badges" in SWH_DJANGO_APPS %} +
+ {% if snapshot_context and snapshot_context.origin_info %} + + {% endif %} + {% if swhid_info.object_id %} + + {% endif %} +
{% endif %} {% if swhid_info.object_type.name.lower == "content" or swhid_info.object_type.name.lower == "directory" %} - + Iframe embedding {% endif %} @@ -85,7 +89,7 @@ {% endif %} {% if swhid_info.object_id %} -
{{ swhid_info.swhid }}
+
{{ swhid_info.swhid }}
{% endif %} {% if swhid_info.swhid_with_context is not None %}
diff --git a/swh/web/config.py b/swh/web/config.py --- a/swh/web/config.py +++ b/swh/web/config.py @@ -163,6 +163,7 @@ "swh.web.mailmap", "swh.web.save_code_now", "swh.web.deposit", + "swh.web.badges", ], ), } diff --git a/swh/web/misc/urls.py b/swh/web/misc/urls.py --- a/swh/web/misc/urls.py +++ b/swh/web/misc/urls.py @@ -64,7 +64,6 @@ url(r"^", include("swh.web.misc.coverage")), url(r"^jslicenses/$", _jslicenses, name="jslicenses"), url(r"^stat_counters/$", _stat_counters, name="stat-counters"), - url(r"^", include("swh.web.misc.badges")), url(r"^metrics/prometheus/$", prometheus_metrics, name="metrics-prometheus"), url(r"^", include("swh.web.misc.fundraising")), url(r"^hiring/banner/$", hiring_banner, name="swh-hiring-banner"), diff --git a/swh/web/tests/badges/__init__.py b/swh/web/tests/badges/__init__.py new file mode 100644 diff --git a/swh/web/tests/badges/test_app.py b/swh/web/tests/badges/test_app.py new file mode 100644 --- /dev/null +++ b/swh/web/tests/badges/test_app.py @@ -0,0 +1,32 @@ +# Copyright (C) 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 + +import pytest + +from django.urls import get_resolver + +from swh.web.badges.urls import urlpatterns +from swh.web.tests.django_asserts import assert_not_contains +from swh.web.tests.helpers import check_html_get_response +from swh.web.utils import reverse + + +@pytest.mark.django_db +def test_badges_deactivate(client, django_settings, directory): + """Check badges feature is deactivated when the swh.web.badges django + application is not in installed apps.""" + + django_settings.SWH_DJANGO_APPS = [ + app for app in django_settings.SWH_DJANGO_APPS if app != "swh.web.badges" + ] + + url = reverse("browse-directory", url_args={"sha1_git": directory}) + + resp = check_html_get_response(client, url, status_code=200) + assert_not_contains(resp, "swh-badges") + + badges_view_names = set(urlpattern.name for urlpattern in urlpatterns) + all_view_names = set(get_resolver().reverse_dict.keys()) + assert badges_view_names & all_view_names == set() diff --git a/swh/web/tests/misc/test_badges.py b/swh/web/tests/badges/test_badges.py rename from swh/web/tests/misc/test_badges.py rename to swh/web/tests/badges/test_badges.py --- a/swh/web/tests/misc/test_badges.py +++ b/swh/web/tests/badges/test_badges.py @@ -1,4 +1,4 @@ -# Copyright (C) 2019-2021 The Software Heritage developers +# Copyright (C) 2019-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 @@ -8,7 +8,7 @@ from swh.model.hashutil import hash_to_bytes from swh.model.swhids import ObjectType, QualifiedSWHID -from swh.web.misc.badges import _badge_config, _get_logo_data +from swh.web.badges import badge_config, get_logo_data from swh.web.tests.django_asserts import assert_contains from swh.web.tests.helpers import check_http_get_response from swh.web.tests.strategies import new_origin @@ -173,8 +173,8 @@ assert_contains(response, "") - assert_contains(response, _get_logo_data()) - assert_contains(response, _badge_config[object_type]["color"]) - assert_contains(response, _badge_config[object_type]["title"]) + assert_contains(response, get_logo_data()) + assert_contains(response, badge_config[object_type]["color"]) + assert_contains(response, badge_config[object_type]["title"]) assert_contains(response, text) assert_contains(response, link)