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

feat: tag badges choose the tag, untagged rows, counts in the tag list

On the VM and host lists a tag's badge chooses it in the tag list, the
tag list offers "No tag" for the rows without one, and each choice says
how many of the listed rows it shows ("prod (42)"); an owner counts only
their own VMs.
This commit is contained in:
catborise 2026-11-18 22:08:02 +03:00
parent f8cb6f1e0f
commit 904dd860d0
15 changed files with 110 additions and 57 deletions

View file

@ -1,3 +1,4 @@
from collections import Counter
from ipaddress import ip_address
from django.core.exceptions import ValidationError
@ -30,6 +31,13 @@ class Tag(Model):
def __str__(self):
return self.name
@staticmethod
def counts(rows):
"""The tag list's choices for rows (VMs or hosts, their tags read):
[(name, how many have it)] by name, and how many have no tag."""
counts = Counter(tag.name for row in rows for tag in row.tags.all())
return sorted(counts.items()), sum(1 for row in rows if not row.tags.all())
@classmethod
def named(cls, names):
"""The tags of names, those not there yet made."""

View file

@ -82,7 +82,7 @@
</div>
<div class="small text-body-secondary font-monospace mt-1">{{ compute.hostname }}</div>
{% if compute.details %}<div class="small text-body-secondary">{{ compute.details }}</div>{% endif %}
{% with tags=compute.tags.all %}{% if tags %}<div class="mt-1">{% include 'common/tag_badges.html' %}</div>{% endif %}{% endwith %}
{% with tags=compute.tags.all %}{% if tags %}<div class="mt-1">{% include 'common/tag_badges.html' with filter=True %}</div>{% endif %}{% endwith %}
{% if compute.status is not True %}
<div class="small text-danger">{{ compute.connection_error }}</div>
{% endif %}

View file

@ -106,5 +106,6 @@ class HostTagsTests(TestCase):
compute.tags.add(Tag.objects.create(name="rack-1"))
response = self.client.get(reverse("computes"))
self.assertContains(response, "rack-1")
self.assertContains(response, '<option value="rack-1">rack-1</option>', html=True)
self.assertContains(response, '<option value="rack-1">rack-1 (1)</option>', html=True)
self.assertContains(response, 'data-tag-filter="rack-1"')
self.assertContains(response, 'data-tags="rack-1"')

View file

@ -36,7 +36,7 @@ from computes.forms import (
TcpComputeForm,
TlsComputeForm,
)
from computes.models import ENDPOINT, Compute
from computes.models import ENDPOINT, Compute, Tag
from . import utils
@ -180,7 +180,7 @@ def computes(request):
totals["memory"] += given["memory"]
return render(request, "computes/list.html",
{"computes": computes, "totals": totals, "job_resource": claims.bulk_migrate_key(),
"tags": sorted({tag.name for compute in computes for tag in compute.tags.all()})})
"tags": Tag.counts(computes)})
@superuser_only