Changeset View
Changeset View
Standalone View
Standalone View
swh/web/misc/badges.py
Show First 20 Lines • Show All 92 Lines • ▼ Show 20 Lines | 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 = '' | whole_link = '' | ||||
status = 200 | |||||
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('browse-origin', | whole_link = reverse('browse-origin', | ||||
url_args={'origin_url': object_id}) | url_args={'origin_url': object_id}) | ||||
else: | else: | ||||
Show All 14 Lines | try: | ||||
if object_pid: | if object_pid: | ||||
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' | ||||
status = 400 | |||||
object_type = 'error' | object_type = 'error' | ||||
except NotFoundExc: | except NotFoundExc: | ||||
right_text = f'{object_type if object_type else "object"} not found' | right_text = f'{object_type if object_type else "object"} not found' | ||||
status = 404 | |||||
object_type = 'error' | object_type = 'error' | ||||
badge_data = badge(left_text=left_text, | badge_data = badge(left_text=left_text, | ||||
right_text=right_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_link=request.build_absolute_uri(whole_link), | ||||
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') | ||||
status=status) | |||||
def _swh_badge_pid(request: HttpRequest, object_pid: str) -> HttpResponse: | def _swh_badge_pid(request: HttpRequest, object_pid: str) -> HttpResponse: | ||||
""" | """ | ||||
Generate a Software Heritage badge for a given object persistent | Generate a Software Heritage badge for a given object persistent | ||||
identifier. | identifier. | ||||
Args: | Args: | ||||
Show All 17 Lines |