1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-12-03 17:36:05 +00:00

feat(ui): the menu marks the page's section, names Jobs and Administration and counts unfinished jobs

The page's own section was not marked: the active class sat on the list item,
which Bootstrap 5 does not style. It now sits on the link with aria-current,
underlined (a side bar on a phone). The links are full white on the theme's
navbar instead of 55 %. Jobs and Administration carry labels (icons only between
768 and 991px), the menus open aligned to the right, and Jobs shows a badge with
the user's queued, running and cancelling jobs. The user menu's disabled
'Language: tr' line is a working language switch.

Operation.visible_to replaces the jobs views' _visible, shared with the badge.
This commit is contained in:
catborise 2026-11-25 16:29:29 +03:00
parent f62a62c027
commit 1c8e204c95
13 changed files with 173 additions and 44 deletions

Binary file not shown.

View file

@ -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ş"

Binary file not shown.

View file

@ -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 незавершеного завдання"

Binary file not shown.

View file

@ -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 个任务未完成"

View file

@ -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}"

View file

@ -77,6 +77,12 @@ def wrap_at_slash(label):
return format_html_join(mark_safe("/<wbr>"), '<span class="text-nowrap">{}</span>', ((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."""

View file

@ -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,

View file

@ -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;

View file

@ -1,11 +0,0 @@
<?xml version="1.0" ?>
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-person-plus" viewBox="0 0 16 16">
<path d="M6 8a3 3 0 1 0 0-6 3 3 0 0 0 0 6zm2-3a2 2 0 1 1-4 0 2 2 0 0 1 4 0zm4 8c0 1-1 1-1 1H1s-1 0-1-1 1-4 6-4 6 3 6 4zm-1-.004c-.001-.246-.154-.986-.832-1.664C9.516 10.68 8.289 10 6 10c-2.29 0-3.516.68-4.168 1.332-.678.678-.83 1.418-.832 1.664h10z"/>
<path fill-rule="evenodd" d="M13.5 5a.5.5 0 0 1 .5.5V7h1.5a.5.5 0 0 1 0 1H14v1.5a.5.5 0 0 1-1 0V8h-1.5a.5.5 0 0 1 0-1H13V5.5a.5.5 0 0 1 .5-.5z"/>
</svg>

Before

Width:  |  Height:  |  Size: 573 B

View file

@ -1,8 +1,9 @@
{% load i18n %}
{% load bootstrap_icons %}
{% load common_tags %}
<!-- Fixed navbar -->
<nav class="navbar sticky-top navbar-expand-md navbar-dark bg-primary mb-3" aria-label="Main top navbar">
{% load operations %}
{# the page's own section is marked (active, aria-current); the labels beside the icons hide on a mid-size screen, where they do not fit #}
<nav class="navbar sticky-top navbar-expand-md navbar-dark bg-primary mb-3" aria-label="{% trans 'Menu' %}">
<div class="container">
<a class="navbar-brand" href="{% url 'index' %}">WebVirtCloud</a>
<button type="button" class="navbar-toggler" data-bs-toggle="collapse" data-bs-target="#navbar" aria-expanded="false" aria-label="{% trans 'Menu' %}">
@ -10,56 +11,77 @@
</button>
<div class="collapse navbar-collapse" id="navbar">
<ul class="navbar-nav">
<li class="nav-item {% class_active request '^/instances' %}">
<a class="nav-link" href="{% url 'instances:index' %}">{% bs_icon 'pc-display-horizontal' %} {% trans "Instances" %}</a>
{% class_active request '^/instances' as here %}
<li class="nav-item">
<a class="nav-link {{ here }}" href="{% url 'instances:index' %}"{% if here %} aria-current="page"{% endif %}>{% bs_icon 'pc-display-horizontal' %} {% trans "Instances" %}</a>
</li>
{% if request.user.is_superuser %}
<li class="nav-item {% class_active request '^/computes' %}">
<a class="nav-link" href="{% url 'computes' %}">{% bs_icon 'pc-display' %} {% trans "Computes" %}</a>
{% class_active request '^/computes' as here %}
<li class="nav-item">
<a class="nav-link {{ here }}" href="{% url 'computes' %}"{% if here %} aria-current="page"{% endif %}>{% bs_icon 'pc-display' %} {% trans "Computes" %}</a>
</li>
<li class="nav-item {% class_active request '^/backups' %}">
<a class="nav-link" href="{% url 'vmbackups:index' %}">{% bs_icon 'archive' %} {% trans "Backups" %}</a>
{% class_active request '^/backups' as here %}
<li class="nav-item">
<a class="nav-link {{ here }}" href="{% url 'vmbackups:index' %}"{% if here %} aria-current="page"{% endif %}>{% bs_icon 'archive' %} {% trans "Backups" %}</a>
</li>
{% endif %}
</ul>
<ul class="navbar-nav ms-auto">
<li class="nav-item {% class_active request '^/jobs' %}">
<a class="nav-link" href="{% url 'operations:jobs' %}" aria-label="{% trans 'Jobs' %}">{% bs_icon 'list-task' %}</a>
{% class_active request '^/jobs' as here %}
{% active_job_count request.user as active_jobs %}
<li class="nav-item">
<a class="nav-link {{ here }}" href="{% url 'operations:jobs' %}" title="{% trans 'Jobs' %}"{% if here %} aria-current="page"{% endif %}>
{% bs_icon 'list-task' %} <span class="nav-label">{% trans "Jobs" %}</span>
{% if active_jobs %}<span class="badge rounded-pill text-bg-light"><span aria-hidden="true">{{ active_jobs }}</span><span class="visually-hidden">{% blocktrans count n=active_jobs %}{{ n }} job not finished{% plural %}{{ n }} jobs not finished{% endblocktrans %}</span></span>{% endif %}
</a>
</li>
{% if request.user.is_superuser %}
<li class="nav-item dropdown {% app_active request 'admin' %}">
<a class="nav-link" id="administration" href="#" title="{% trans "Administration" %}" data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
{% bs_icon 'wrench-adjustable' %}
{% app_active request 'admin' as here %}
{% if not here %}{% view_active request 'appsettings' as here %}{% endif %}
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle {{ here }}" id="administration" href="#" title="{% trans 'Administration' %}" role="button" data-bs-toggle="dropdown" aria-expanded="false">
{% bs_icon 'wrench-adjustable' %} <span class="nav-label">{% trans "Administration" %}</span>
</a>
<div class="dropdown-menu" aria-labelledby="administration">
<a class="dropdown-item {% view_active request 'admin:user_list' %}" href="{% url 'admin:user_list' %}">{% bs_icon 'person-plus' %} {% trans "Users" %}</a>
<div class="dropdown-menu dropdown-menu-end" aria-labelledby="administration">
<a class="dropdown-item {% view_active request 'admin:user_list' %}" href="{% url 'admin:user_list' %}">{% bs_icon 'person' %} {% trans "Users" %}</a>
<a class="dropdown-item {% view_active request 'admin:group_list' %}" href="{% url 'admin:group_list' %}">{% bs_icon 'people-fill' %} {% trans "Groups" %}</a>
<a class="dropdown-item {% view_active request 'admin:logs' %}" href="{% url 'admin:logs' %}">{% bs_icon 'card-list' %} {% trans "Logs" %}</a>
<a class="dropdown-item {% view_active request 'admin:tombstone_list' %}" href="{% url 'admin:tombstone_list' %}">{% bs_icon 'trash' %} {% trans "Removed VMs" %}</a>
<a class="dropdown-item {% view_active request 'admin:tag_list' %}" href="{% url 'admin:tag_list' %}">{% bs_icon 'tag' %} {% trans "Tags" %}</a>
<a class="dropdown-item" href="{% url 'instances:vm_report' %}">{% bs_icon 'download' %} {% trans "VM report (.xlsx)" %}</a>
<div class="dropdown-divider"></div>
<a class="dropdown-item {% view_active request 'appsettings' %}" href="{% url 'appsettings' %}">{% bs_icon 'gear-wide-connected' %} {% trans "Settings" %}</a>
</div>
</li>
{% endif %}
<li class="nav-item dropdown {% class_active request '^/accounts' %}">
<a class="nav-link dropdown-toggle" href="#" id="navbarUserDropdown" data-bs-toggle="dropdown" role="button" aria-expanded="false">
{% bs_icon 'person-square' %} {{ request.user.username }}
{% class_active request '^/accounts' as here %}
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle {{ here }}" href="#" id="navbarUserDropdown" role="button" data-bs-toggle="dropdown" aria-expanded="false">
{% bs_icon 'person-square' %} {{ request.user.username }}
</a>
<div class="dropdown-menu" aria-labelledby="navbarUserDropdown">
{% get_current_language as LANGUAGE_CODE %}
<a class="dropdown-item disabled" href="#">
{% trans "Language" %}: <span class="badge bg-secondary">{{ LANGUAGE_CODE }}</span>
</a>
<div class="dropdown-menu dropdown-menu-end" aria-labelledby="navbarUserDropdown">
<a class="dropdown-item {% view_active request 'accounts:profile' %}" href="{% url 'accounts:profile' %}">{% bs_icon 'person-vcard' %} {% trans "Profile" %}</a>
<div class="dropdown-divider"></div>
{# the languages, each a button that sets it; Django comes back to this page (the Referer) #}
<div class="dropdown-header">{% trans "Language" %}</div>
<form method="post" action="{% url 'set_language' %}">
{% csrf_token %}
{% get_current_language as LANGUAGE_CODE %}
{% get_available_languages as LANGUAGES %}
{% get_language_info_list for LANGUAGES as languages %}
{% for language in languages %}
<button type="submit" name="language" value="{{ language.code }}" lang="{{ language.code }}"
class="dropdown-item{% if language.code == LANGUAGE_CODE %} active{% endif %}"{% if language.code == LANGUAGE_CODE %} aria-current="true"{% endif %}>{{ language.name_local }}</button>
{% endfor %}
</form>
<div class="dropdown-divider"></div>
<form method="post" action="{% url 'accounts:logout' %}">
{% csrf_token %}
<button type="submit" class="dropdown-item">{% bs_icon 'power' %} {% trans "Log out" %}</button>
<button type="submit" class="dropdown-item">{% bs_icon 'power' %} {% trans "Log out" %}</button>
</form>
</div>
</li>
</ul>
</div><!--/.nav-collapse -->
</div>
</div>
</nav>
</nav>

View file

@ -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")