Skip to content

Commit 60b9d0d

Browse files
feat #17666: Display icons for component priority
- Add 5 new svg (1 for each priority) - Add trumping logic to select the svg based on the component priority - Display the priority along with the remaining icons related to the component - Add a test to validate this logic checking each boundary Closes #17666 Co-authored-by: Dinis Sales <dinis.sales@tecnico.ulisboa.pt>
1 parent 22c893b commit 60b9d0d

7 files changed

Lines changed: 116 additions & 1 deletion

File tree

weblate/static/priorities/dash.svg

Lines changed: 7 additions & 0 deletions
Loading
Lines changed: 8 additions & 0 deletions
Loading
Lines changed: 8 additions & 0 deletions
Loading
Lines changed: 7 additions & 0 deletions
Loading
Lines changed: 7 additions & 0 deletions
Loading

weblate/trans/templatetags/translations.py

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
from weblate.utils.docs import get_doc_url
4848
from weblate.utils.hash import hash_to_checksum
4949
from weblate.utils.html import format_html_join_comma, list_to_tuples
50+
from weblate.utils.icons import load_icon
5051
from weblate.utils.markdown import render_markdown
5152
from weblate.utils.messages import get_message_kind as get_message_kind_impl
5253
from weblate.utils.random import get_random_identifier
@@ -1241,6 +1242,35 @@ def indicate_alerts(
12411242
# GhostProjectLanguageStats and GhostCategoryLanguageStats as these would
12421243
# be confusing (showing alert or admin icon on ghost containers).
12431244

1245+
priority_html = ""
1246+
if component and (priority := getattr(component, "priority", None)) is not None:
1247+
levels = (
1248+
(60, "double_arrow_up", "text-danger", gettext_lazy("Priority: Very high")),
1249+
(80, "single_arrow_up", "text-warning", gettext_lazy("Priority: High")),
1250+
(100, "dash", "text-info", gettext_lazy("Priority: Medium")),
1251+
(120, "single_arrow_down", "text-muted", gettext_lazy("Priority: Low")),
1252+
)
1253+
1254+
selected_svg = "double_arrow_down"
1255+
css_class = "text-secondary"
1256+
title_text = gettext_lazy("Priority: Very low")
1257+
for threshold, svg, css, title in levels:
1258+
if priority <= threshold:
1259+
selected_svg = svg
1260+
css_class = css
1261+
title_text = title
1262+
break
1263+
1264+
priority_html = format_html(
1265+
'<span class="state-icon {}" data-bs-toggle="tooltip" title="{}" alt="{}" style="margin: 0">{}</span>',
1266+
css_class,
1267+
title_text,
1268+
title_text,
1269+
mark_safe( # noqa: S308
1270+
load_icon(f"priorities/{selected_svg}.svg", auto_prefix=False).decode()
1271+
),
1272+
)
1273+
12441274
icons = format_html_join(
12451275
"\n",
12461276
'{}<span class="state-icon {}" title="{}" alt="{}">{}</span>{}',
@@ -1276,7 +1306,7 @@ def indicate_alerts(
12761306
component.license,
12771307
)
12781308

1279-
return format_html("{}{}", icons, license_badge)
1309+
return format_html("{}{}{}", priority_html, icons, license_badge)
12801310

12811311

12821312
@register.filter(is_safe=True)

weblate/trans/tests/test_templatetags.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66

77
from __future__ import annotations
88

9+
from unittest.mock import patch
10+
11+
from django.template import Context
912
from django.test import SimpleTestCase, TestCase
1013
from django.utils import timezone
1114

@@ -16,6 +19,7 @@
1619
from weblate.trans.templatetags.translations import (
1720
format_translation,
1821
get_location_links,
22+
indicate_alerts,
1923
naturaltime,
2024
translation_progress_render,
2125
)
@@ -35,6 +39,50 @@ def test_natural(self) -> None:
3539
self.assertIn('class="naturaltime"', result)
3640

3741

42+
class IndicateAlertsPriorityTest(SimpleTestCase):
43+
def test_priority_icons(self) -> None:
44+
project = Project(slug="p", name="p")
45+
context = Context({})
46+
47+
cases = (
48+
(1, "double_arrow_up", "text-danger", 'title="Priority: Very high"'),
49+
(60, "double_arrow_up", "text-danger", 'title="Priority: Very high"'),
50+
(61, "single_arrow_up", "text-warning", 'title="Priority: High"'),
51+
(80, "single_arrow_up", "text-warning", 'title="Priority: High"'),
52+
(81, "dash", "text-info", 'title="Priority: Medium"'),
53+
(100, "dash", "text-info", 'title="Priority: Medium"'),
54+
(101, "single_arrow_down", "text-muted", 'title="Priority: Low"'),
55+
(120, "single_arrow_down", "text-muted", 'title="Priority: Low"'),
56+
(121, "double_arrow_down", "text-secondary", 'title="Priority: Very low"'),
57+
(999, "double_arrow_down", "text-secondary", 'title="Priority: Very low"'),
58+
)
59+
60+
def fake_load_icon(path: str, *, auto_prefix: bool = True) -> bytes:
61+
return f'<svg data-icon="{path}"></svg>'.encode()
62+
63+
with (
64+
patch(
65+
"weblate.trans.templatetags.translations.get_alerts", return_value=[]
66+
),
67+
patch(
68+
"weblate.trans.templatetags.translations.load_icon",
69+
side_effect=fake_load_icon,
70+
),
71+
):
72+
for priority, svg, css_class, title in cases:
73+
component = Component(
74+
project=project,
75+
slug="c",
76+
name="c",
77+
priority=priority,
78+
source_language=Language(),
79+
)
80+
html = str(indicate_alerts(context, component))
81+
self.assertIn(f'data-icon="priorities/{svg}.svg"', html)
82+
self.assertIn(f'class="state-icon {css_class}"', html)
83+
self.assertIn(title, html)
84+
85+
3886
class LocationLinksTest(TestCase):
3987
def setUp(self) -> None:
4088
self.unit = Unit(

0 commit comments

Comments
 (0)