diff --git a/locale/tr/LC_MESSAGES/django.mo b/locale/tr/LC_MESSAGES/django.mo index 422183d3..6c2baca7 100644 Binary files a/locale/tr/LC_MESSAGES/django.mo and b/locale/tr/LC_MESSAGES/django.mo differ diff --git a/locale/tr/LC_MESSAGES/django.po b/locale/tr/LC_MESSAGES/django.po index aee1b154..65fc3339 100644 --- a/locale/tr/LC_MESSAGES/django.po +++ b/locale/tr/LC_MESSAGES/django.po @@ -13415,3 +13415,9 @@ msgstr "%(name)s ekranı" msgctxt "screen" msgid "Live" msgstr "Canlı" + +#: templates/navbar.html:35 +msgid "%(n)s job not finished" +msgid_plural "%(n)s jobs not finished" +msgstr[0] "Bitmemiş %(n)s iş" +msgstr[1] "Bitmemiş %(n)s iş" diff --git a/locale/uk/LC_MESSAGES/django.mo b/locale/uk/LC_MESSAGES/django.mo index 0da85173..aabae6e0 100644 Binary files a/locale/uk/LC_MESSAGES/django.mo and b/locale/uk/LC_MESSAGES/django.mo differ diff --git a/locale/uk/LC_MESSAGES/django.po b/locale/uk/LC_MESSAGES/django.po index e3f22e8c..35b0cb5b 100644 --- a/locale/uk/LC_MESSAGES/django.po +++ b/locale/uk/LC_MESSAGES/django.po @@ -13511,3 +13511,11 @@ msgstr "Екран ВМ %(name)s" msgctxt "screen" msgid "Live" msgstr "Наживо" + +#: templates/navbar.html:35 +msgid "%(n)s job not finished" +msgid_plural "%(n)s jobs not finished" +msgstr[0] "%(n)s незавершене завдання" +msgstr[1] "%(n)s незавершені завдання" +msgstr[2] "%(n)s незавершених завдань" +msgstr[3] "%(n)s незавершеного завдання" diff --git a/locale/zh/LC_MESSAGES/django.mo b/locale/zh/LC_MESSAGES/django.mo index 20a00ebe..6b4aae52 100644 Binary files a/locale/zh/LC_MESSAGES/django.mo and b/locale/zh/LC_MESSAGES/django.mo differ diff --git a/locale/zh/LC_MESSAGES/django.po b/locale/zh/LC_MESSAGES/django.po index bf96e844..9d611541 100644 --- a/locale/zh/LC_MESSAGES/django.po +++ b/locale/zh/LC_MESSAGES/django.po @@ -12541,3 +12541,8 @@ msgstr "%(name)s 的屏幕" msgctxt "screen" msgid "Live" msgstr "实时" + +#: templates/navbar.html:35 +msgid "%(n)s job not finished" +msgid_plural "%(n)s jobs not finished" +msgstr[0] "%(n)s 个任务未完成" diff --git a/operations/models.py b/operations/models.py index e8ae6979..dd587125 100644 --- a/operations/models.py +++ b/operations/models.py @@ -66,6 +66,11 @@ class Operation(models.Model): ordering = ["-created_at"] constraints = [models.UniqueConstraint(fields=["user", "submission_key"], name="unique_submission")] + @classmethod + def visible_to(cls, user): + """The jobs a user sees: all for a superuser, their own for others.""" + return cls.objects.all() if user.is_superuser else cls.objects.filter(user=user) + def __str__(self): return f"{self.kind} {self.id}" diff --git a/operations/templatetags/operations.py b/operations/templatetags/operations.py index 5950749d..b236207a 100644 --- a/operations/templatetags/operations.py +++ b/operations/templatetags/operations.py @@ -77,6 +77,12 @@ def wrap_at_slash(label): return format_html_join(mark_safe("/"), '{}', ((part,) for part in label.split("/"))) +@register.simple_tag +def active_job_count(user): + """How many of the jobs the user sees are queued, running or cancelling: the menu's Jobs badge.""" + return Operation.visible_to(user).filter(state__in=Operation.ACTIVE).count() + + @register.inclusion_tag("operations/job_panel_script.html", takes_context=True) def job_panel_script(context, resource): """The job panel's script for the page of resource.""" diff --git a/operations/views.py b/operations/views.py index f456ea3a..ec37b51a 100644 --- a/operations/views.py +++ b/operations/views.py @@ -24,19 +24,15 @@ from .models import Operation, last_seen, worker_gone from .utils import said, words -def _visible(user): - return Operation.objects.all() if user.is_superuser else Operation.objects.filter(user=user) - - def _get(user, pk): try: - return _visible(user).get(pk=pk) + return Operation.visible_to(user).get(pk=pk) except Operation.DoesNotExist: raise Http404 from None def jobs(request): - qs = _visible(request.user) + qs = Operation.visible_to(request.user) if request.user.is_superuser: # the host and the user as typed: the start of the host, any part of the name if compute := request.GET.get("compute"): @@ -54,7 +50,7 @@ def jobs(request): seen = last_seen() # a job the worker has to start, run or stop, not one a request runs (job_panel.js uses the same rule) unavailable = (worker_gone(seen) - and _visible(request.user).filter(state__in=Operation.ACTIVE) + and Operation.visible_to(request.user).filter(state__in=Operation.ACTIVE) .exclude(worker=Operation.REQUEST).exists()) return render(request, "operations/jobs.html", {"page": page, "rows": rows, "worker_last_seen": seen, "worker_unavailable": unavailable, diff --git a/static/css/webvirtcloud.css b/static/css/webvirtcloud.css index c320ea2b..5bbde57a 100644 --- a/static/css/webvirtcloud.css +++ b/static/css/webvirtcloud.css @@ -349,6 +349,50 @@ td .tag-more { height: 0.85em; } +/* the menu: its links in the full light of the theme's navbar, an icon beside each label; the page's own + section underlined (on a phone, a bar at its side) and bolder */ +.navbar-dark { + --bs-navbar-color: #fff; + --bs-navbar-hover-color: #fff; + --bs-navbar-active-color: #fff; +} +.navbar .nav-link { + display: flex; + align-items: center; + gap: .375rem; +} +.navbar .nav-link svg { + flex: none; +} +.navbar .nav-link.active { + font-weight: 600; + box-shadow: inset 3px 0 0 currentColor; + padding-left: .75rem; +} +@media (min-width: 768px) { + .navbar .nav-link.active { + box-shadow: inset 0 -2px 0 currentColor; + padding-left: var(--bs-navbar-nav-link-padding-x); + } + .navbar .nav-link:hover:not(.active) { + box-shadow: inset 0 -2px 0 rgba(255, 255, 255, .45); + } +} +.navbar .dropdown-item svg { + vertical-align: -.125em; +} +/* between a phone and a wide screen the labels of Jobs and Administration do not fit: their icons stay */ +@media (min-width: 768px) and (max-width: 991.98px) { + .navbar .nav-label { + position: absolute; + width: 1px; + height: 1px; + overflow: hidden; + clip: rect(0, 0, 0, 0); + white-space: nowrap; + } +} + /* "Skip to content" brings #main up below the sticky navbar, not behind it */ #main { scroll-margin-top: 5rem; diff --git a/static/icon_cache/person-plus_None_None_None.svg b/static/icon_cache/person-plus_None_None_None.svg deleted file mode 100644 index 377a8e7d..00000000 --- a/static/icon_cache/person-plus_None_None_None.svg +++ /dev/null @@ -1,11 +0,0 @@ - - - - - - - - - - - diff --git a/templates/navbar.html b/templates/navbar.html index 4f0a0cbc..26652518 100644 --- a/templates/navbar.html +++ b/templates/navbar.html @@ -1,8 +1,9 @@ {% load i18n %} {% load bootstrap_icons %} {% load common_tags %} - - diff --git a/webvirtcloud/tests/test_navbar.py b/webvirtcloud/tests/test_navbar.py new file mode 100644 index 00000000..01118da2 --- /dev/null +++ b/webvirtcloud/tests/test_navbar.py @@ -0,0 +1,48 @@ +"""The menu on every page: the page's own section marked, the count of unfinished jobs, the languages.""" + +from django.contrib.auth import get_user_model +from django.test import TestCase +from lxml import html + +from operations.models import Operation + + +class NavbarTests(TestCase): + def setUp(self): + users = get_user_model().objects + self.owner = users.create_user("navbar_owner", password="pw") + self.other = users.create_user("navbar_other", password="pw") + self.client.force_login(self.owner) + + def menu(self, url="/jobs/"): + return html.fromstring(self.client.get(url).content).get_element_by_id("navbar") + + def job(self, user, state, key): + Operation.objects.create(kind="test.noop", user=user, username=user.username, state=state, + submission_key=key, resource="r", resource_label="c/vm") + + def test_the_pages_section_is_marked_for_eyes_and_screen_readers(self): + current = self.menu().xpath('.//a[@aria-current="page"]') + self.assertEqual([a.get("href") for a in current], ["/jobs/"]) + self.assertIn("active", current[0].get("class").split()) + + def test_jobs_shows_how_many_of_ones_own_jobs_are_not_finished(self): + self.job(self.owner, Operation.QUEUED, "a") + self.job(self.owner, Operation.RUNNING, "b") + self.job(self.owner, Operation.SUCCEEDED, "c") + self.job(self.other, Operation.RUNNING, "d") # another user's job is not counted for this one + badge = self.menu().xpath('.//a[@href="/jobs/"]//span[contains(@class, "badge")]')[0] + self.assertEqual(badge.xpath('./span[@aria-hidden="true"]')[0].text, "2") + self.assertEqual(badge.xpath('./span[@class="visually-hidden"]')[0].text, "2 jobs not finished") + + def test_no_badge_without_unfinished_jobs(self): + self.job(self.owner, Operation.FAILED, "a") + self.assertEqual(self.menu().xpath('.//span[contains(@class, "badge")]'), []) + + def test_a_language_button_sets_the_language_and_comes_back_to_the_page(self): + buttons = self.menu().xpath('.//form[@action="/i18n/setlang/"]//button[@name="language"]') + self.assertEqual([b.get("value") for b in buttons], ["en", "tr", "zh-hans", "uk"]) + self.assertEqual([b.get("value") for b in buttons if b.get("aria-current")], ["en"]) + answer = self.client.post("/i18n/setlang/", {"language": "tr"}, HTTP_REFERER="http://testserver/jobs/") + self.assertEqual(answer["Location"], "http://testserver/jobs/") + self.assertEqual(self.client.cookies["django_language"].value, "tr")