Changeset View
Changeset View
Standalone View
Standalone View
swh/web/misc/badges.py
# Copyright (C) 2019-2020 The Software Heritage developers | # Copyright (C) 2019-2020 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 | ||||
from base64 import b64encode | from base64 import b64encode | ||||
from typing import cast, Optional | from typing import cast, Optional | ||||
from django.conf.urls import url | from django.conf.urls import url | ||||
from django.contrib.staticfiles import finders | from django.contrib.staticfiles import finders | ||||
from django.http import HttpResponse, HttpRequest | from django.http import HttpResponse, HttpRequest | ||||
from pybadges import badge | from pybadges import badge | ||||
from swh.model.exceptions import ValidationError | from swh.model.exceptions import ValidationError | ||||
from swh.model.identifiers import ( | from swh.model.identifiers import ( | ||||
persistent_identifier, | swhid, | ||||
parse_persistent_identifier, | parse_swhid, | ||||
CONTENT, | CONTENT, | ||||
DIRECTORY, | DIRECTORY, | ||||
ORIGIN, | ORIGIN, | ||||
RELEASE, | RELEASE, | ||||
REVISION, | REVISION, | ||||
SNAPSHOT, | SNAPSHOT, | ||||
) | ) | ||||
from swh.web.common import service | from swh.web.common import service | ||||
from swh.web.common.exc import BadInputExc, NotFoundExc | from swh.web.common.exc import BadInputExc, NotFoundExc | ||||
from swh.web.common.identifiers import resolve_swh_persistent_id | from swh.web.common.identifiers import resolve_swhid | ||||
from swh.web.common.utils import reverse | from swh.web.common.utils import reverse | ||||
_orange = "#f36a24" | _orange = "#f36a24" | ||||
_blue = "#0172b2" | _blue = "#0172b2" | ||||
_red = "#cd5741" | _red = "#cd5741" | ||||
_swh_logo_data = None | _swh_logo_data = None | ||||
Show All 23 Lines | if _swh_logo_data is None: | ||||
).decode("ascii") | ).decode("ascii") | ||||
return _swh_logo_data | return _swh_logo_data | ||||
def _swh_badge( | def _swh_badge( | ||||
request: HttpRequest, | request: HttpRequest, | ||||
object_type: str, | object_type: str, | ||||
object_id: str, | object_id: str, | ||||
object_pid: Optional[str] = "", | object_swhid: Optional[str] = "", | ||||
) -> HttpResponse: | ) -> HttpResponse: | ||||
""" | """ | ||||
Generate a Software Heritage badge for a given object type and id. | Generate a Software Heritage badge for a given object type and id. | ||||
Args: | Args: | ||||
request: input http request | request: input http request | ||||
object_type: The type of swh object to generate a badge for, | object_type: The type of swh object to generate a badge for, | ||||
either *content*, *directory*, *revision*, *release*, *origin* | either *content*, *directory*, *revision*, *release*, *origin* | ||||
or *snapshot* | or *snapshot* | ||||
object_id: The id of the swh object, either an url for origin | object_id: The id of the swh object, either an url for origin | ||||
type or a *sha1* for other object types | type or a *sha1* for other object types | ||||
object_pid: If provided, the object persistent | object_swhid: If provided, the object SWHID will not be recomputed | ||||
identifier will not be recomputed | |||||
Returns: | Returns: | ||||
HTTP response with content type *image/svg+xml* containing the SVG | HTTP response with content type *image/svg+xml* containing the SVG | ||||
badge data. If the provided parameters are invalid, HTTP 400 status | badge data. If the provided parameters are invalid, HTTP 400 status | ||||
code will be returned. If the object can not be found in the archive, | code will be returned. If the object can not be found in the archive, | ||||
HTTP 404 status code will be returned. | HTTP 404 status code will be returned. | ||||
""" | """ | ||||
left_text = "error" | left_text = "error" | ||||
whole_link = None | whole_link = None | ||||
try: | try: | ||||
if object_type == ORIGIN: | if object_type == ORIGIN: | ||||
service.lookup_origin({"url": object_id}) | service.lookup_origin({"url": object_id}) | ||||
right_text = "repository" | right_text = "repository" | ||||
whole_link = reverse( | whole_link = reverse( | ||||
"browse-origin", query_params={"origin_url": object_id} | "browse-origin", query_params={"origin_url": object_id} | ||||
) | ) | ||||
else: | else: | ||||
# when pid is provided, object type and id will be parsed | # when SWHID is provided, object type and id will be parsed | ||||
# from it | # from it | ||||
if object_pid: | if object_swhid: | ||||
parsed_pid = parse_persistent_identifier(object_pid) | parsed_swhid = parse_swhid(object_swhid) | ||||
object_type = parsed_pid.object_type | object_type = parsed_swhid.object_type | ||||
object_id = parsed_pid.object_id | object_id = parsed_swhid.object_id | ||||
swh_object = service.lookup_object(object_type, object_id) | swh_object = service.lookup_object(object_type, object_id) | ||||
if object_pid: | if object_swhid: | ||||
right_text = object_pid | right_text = object_swhid | ||||
else: | else: | ||||
right_text = persistent_identifier(object_type, object_id) | right_text = swhid(object_type, object_id) | ||||
whole_link = resolve_swh_persistent_id(right_text)["browse_url"] | whole_link = resolve_swhid(right_text)["browse_url"] | ||||
# remove pid metadata if any for badge text | # remove SWHID metadata if any for badge text | ||||
if object_pid: | if object_swhid: | ||||
right_text = right_text.split(";")[0] | right_text = right_text.split(";")[0] | ||||
# use release name for badge text | # use release name for badge text | ||||
if object_type == RELEASE: | if object_type == RELEASE: | ||||
right_text = "release %s" % swh_object["name"] | right_text = "release %s" % swh_object["name"] | ||||
left_text = "archived" | left_text = "archived" | ||||
except (BadInputExc, ValidationError): | except (BadInputExc, ValidationError): | ||||
right_text = f'invalid {object_type if object_type else "object"} id' | right_text = f'invalid {object_type if object_type else "object"} id' | ||||
object_type = "error" | object_type = "error" | ||||
Show All 9 Lines | badge_data = badge( | ||||
whole_title=_badge_config[object_type]["title"], | whole_title=_badge_config[object_type]["title"], | ||||
logo=_get_logo_data(), | logo=_get_logo_data(), | ||||
embed_logo=True, | embed_logo=True, | ||||
) | ) | ||||
return HttpResponse(badge_data, content_type="image/svg+xml") | return HttpResponse(badge_data, content_type="image/svg+xml") | ||||
def _swh_badge_pid(request: HttpRequest, object_pid: str) -> HttpResponse: | def _swh_badge_swhid(request: HttpRequest, object_swhid: str) -> HttpResponse: | ||||
""" | """ | ||||
Generate a Software Heritage badge for a given object persistent | Generate a Software Heritage badge for a given object SWHID. | ||||
identifier. | |||||
Args: | Args: | ||||
request (django.http.HttpRequest): input http request | request (django.http.HttpRequest): input http request | ||||
object_pid (str): A swh object persistent identifier | object_swhid (str): a SWHID of an archived object | ||||
Returns: | Returns: | ||||
django.http.HttpResponse: An http response with content type | django.http.HttpResponse: An http response with content type | ||||
*image/svg+xml* containing the SVG badge data. If any error | *image/svg+xml* containing the SVG badge data. If any error | ||||
occurs, a status code of 400 will be returned. | occurs, a status code of 400 will be returned. | ||||
""" | """ | ||||
return _swh_badge(request, "", "", object_pid) | return _swh_badge(request, "", "", object_swhid) | ||||
urlpatterns = [ | urlpatterns = [ | ||||
url( | url( | ||||
r"^badge/(?P<object_type>[a-z]+)/(?P<object_id>.+)/$", | r"^badge/(?P<object_type>[a-z]+)/(?P<object_id>.+)/$", | ||||
_swh_badge, | _swh_badge, | ||||
name="swh-badge", | name="swh-badge", | ||||
), | ), | ||||
url( | url( | ||||
r"^badge/(?P<object_pid>swh:[0-9]+:[a-z]+:[0-9a-f]+.*)/$", | r"^badge/(?P<object_swhid>swh:[0-9]+:[a-z]+:[0-9a-f]+.*)/$", | ||||
_swh_badge_pid, | _swh_badge_swhid, | ||||
name="swh-badge-pid", | name="swh-badge-swhid", | ||||
), | ), | ||||
] | ] |