Skip to content
Snippets Groups Projects
Commit c24f1a76 authored by Antoine Lambert's avatar Antoine Lambert
Browse files

misc/badges: Move feature into a dedicated django application

It only depends on swh.web.browse which is a base django application
always activated so it can be moved into its own django application.
parent ddcfa6ce
No related branches found
No related tags found
No related merge requests found
......@@ -10,7 +10,6 @@ from pybadges import badge
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 @@ _red = "#cd5741"
_swh_logo_data = None
_badge_config = {
badge_config = {
"content": {
"color": _blue,
"title": "Archived source file",
......@@ -54,7 +53,7 @@ _badge_config = {
}
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 @@ def _get_logo_data() -> str:
return _swh_logo_data
def _swh_badge(
def swh_badge(
request: HttpRequest,
object_type: str,
object_id: str,
......@@ -145,17 +144,17 @@ def _swh_badge(
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 @@ def _swh_badge_swhid(request: HttpRequest, object_swhid: str) -> HttpResponse:
*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<object_type>[a-z]+)/(?P<object_id>.+)/$",
_swh_badge,
name="swh-badge",
),
url(
r"^badge/(?P<object_swhid>swh:[0-9]+:[a-z]+:[0-9a-f]+.*)/$",
_swh_badge_swhid,
name="swh-badge-swhid",
),
]
return swh_badge(request, "", "", object_swhid)
# 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<object_type>[a-z]+)/(?P<object_id>.+)/$",
swh_badge,
name="swh-badge",
),
url(
r"^badge/(?P<object_swhid>swh:[0-9]+:[a-z]+:[0-9a-f]+.*)/$",
swh_badge_swhid,
name="swh-badge-swhid",
),
]
......@@ -62,22 +62,26 @@ See top-level LICENSE file for more information
<div class="card">
<div class="card-body swhid-ui">
{% if not iframe_mode %}
<div class="swh-badges">
{% if snapshot_context and snapshot_context.origin_info %}
<img class="swh-badge swh-badge-origin"
src="{% url 'swh-badge' 'origin' snapshot_context.origin_info.url %}"
onclick="swh.webapp.showBadgeInfoModal('origin', '{{ snapshot_context.origin_info.url|urlencode:"/?:@&" }}')"
title="Click to display badge integration info">
{% endif %}
{% if swhid_info.object_id %}
<img class="swh-badge swh-badge-{{ swhid_info.object_type.name.lower }}"
src="{% url 'swh-badge' swhid_info.object_type.name.lower swhid_info.object_id %}"
onclick="swh.webapp.showBadgeInfoModal('{{ swhid_info.object_type.name.lower }}', $(this).parent().parent().find('.swhid').text())"
title="Click to display badge integration info">
<div class="row">
{% if "swh.web.badges" in SWH_DJANGO_APPS %}
<div class="swh-badges">
{% if snapshot_context and snapshot_context.origin_info %}
<img class="swh-badge swh-badge-origin"
src="{% url 'swh-badge' 'origin' snapshot_context.origin_info.url %}"
onclick="swh.webapp.showBadgeInfoModal('origin', '{{ snapshot_context.origin_info.url|urlencode:"/?:@&" }}')"
title="Click to display badge integration info">
{% endif %}
{% if swhid_info.object_id %}
<img class="swh-badge swh-badge-{{ swhid_info.object_type.name.lower }}"
src="{% url 'swh-badge' swhid_info.object_type.name.lower swhid_info.object_id %}"
onclick="swh.webapp.showBadgeInfoModal('{{ swhid_info.object_type.name.lower }}', $(this).closest('.swhid-ui').find('.swhid').text())"
title="Click to display badge integration info">
{% endif %}
</div>
{% endif %}
{% if swhid_info.object_type.name.lower == "content" or swhid_info.object_type.name.lower == "directory" %}
<a class="float-right" style="cursor: pointer;"
onclick="swh.browse.showIframeInfoModal('{{ swhid_info.object_type.name.lower }}', $(this).parent().parent().find('.swhid').text())">
<a class="ml-auto float-right" style="cursor: pointer;"
onclick="swh.browse.showIframeInfoModal('{{ swhid_info.object_type.name.lower }}', $(this).closest('.swhid-ui').find('.swhid').text())">
Iframe embedding
</a>
{% endif %}
......@@ -85,7 +89,7 @@ See top-level LICENSE file for more information
{% endif %}
{% if swhid_info.object_id %}
<pre><a class="swhid" target="_blank" rel="noopener noreferrer" id="{{ swhid_info.swhid }}" href="{{ swhid_info.swhid_url }}">{{ swhid_info.swhid }}</a></pre>
<pre><a class="swhid" target="_blank" rel="noopener noreferrer" id="{{ swhid_info.swhid }}" href="{{ swhid_info.swhid_url }}">{{ swhid_info.swhid }}</a></pre>
{% endif %}
{% if swhid_info.swhid_with_context is not None %}
<div class="float-left">
......
......@@ -163,6 +163,7 @@ DEFAULT_CONFIG = {
"swh.web.mailmap",
"swh.web.save_code_now",
"swh.web.deposit",
"swh.web.badges",
],
),
}
......
......@@ -64,7 +64,6 @@ urlpatterns = [
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"),
......
# 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()
# 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 hypothesis import given
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 @@ def _check_generated_badge(response, object_type, object_id, error=None):
assert_contains(response, "<svg ")
assert_contains(response, "</svg>")
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)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment