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

fix(tags): the Tags page lists the tags by name and has a search box

The list counts each tag's VMs and hosts; a query with a count leaves
the model's ordering out, so the tags came in the order they were made.
With dozens of tags the page also gets the search box the other
administration lists have.
This commit is contained in:
catborise 2026-11-19 13:36:39 +03:00
parent a5d0dbfe66
commit 0a99713dda
7 changed files with 103 additions and 63 deletions

View file

@ -1,5 +1,6 @@
{% extends "base.html" %}
{% load i18n %}
{% load static %}
{% load bootstrap_icons %}
{% block title %}{% trans "Tags" %}{% endblock %}
@ -11,30 +12,43 @@
{% if not tags %}
<div class="alert alert-info shadow-sm">{% bs_icon 'info-circle' %} {% trans "No tags yet" %}</div>
{% else %}
<div class="table-responsive">
<table class="table table-hover align-middle" data-sortable>
<thead>
<tr>
<th scope="col">{% trans "Tag" %}</th>
<th scope="col" data-sort-method="number">{% trans "VMs" %}</th>
<th scope="col" data-sort-method="number">{% trans "Hosts" %}</th>
<th scope="col" data-sort-method="none"><span class="visually-hidden">{% trans "Actions" %}</span></th>
</tr>
</thead>
<tbody>
{% for tag in tags %}
<tr>
<td>{% include 'common/tag_badge.html' with tag=tag.name %}</td>
<td>{{ tag.vm_count }}</td>
<td>{{ tag.host_count }}</td>
<td class="text-end text-nowrap">
<a class="btn btn-sm btn-outline-secondary" href="{% url 'admin:tag_update' tag.id %}">{% trans "Rename" %}</a>
<a class="btn btn-sm btn-outline-danger" href="{% url 'admin:tag_delete' tag.id %}">{% trans "Delete" %}</a>
</td>
</tr>
{% endfor %}
</tbody>
</table>
<div class="card mb-3">
<div class="card-header d-flex flex-wrap align-items-center gap-2">
<span class="me-auto">{% trans "Tags" %} <span class="badge text-bg-light border">{{ tags|length }}</span></span>
<input id="filter" class="form-control form-control-sm w-auto" type="search" placeholder="{% trans "Search" %}" aria-label="{% trans "Search tags" %}">
</div>
<div class="table-responsive">
<table class="table table-hover align-middle mb-0" data-sortable>
<thead>
<tr>
<th scope="col">{% trans "Tag" %}</th>
<th scope="col" data-sort-method="number">{% trans "VMs" %}</th>
<th scope="col" data-sort-method="number">{% trans "Hosts" %}</th>
<th scope="col" data-sort-method="none"><span class="visually-hidden">{% trans "Actions" %}</span></th>
</tr>
</thead>
<tbody class="searchable">
{% for tag in tags %}
<tr data-search="{{ tag.name }}">
<td>{% include 'common/tag_badge.html' with tag=tag.name %}</td>
<td>{{ tag.vm_count }}</td>
<td>{{ tag.host_count }}</td>
<td class="text-end text-nowrap">
<a class="btn btn-sm btn-outline-secondary" href="{% url 'admin:tag_update' tag.id %}">{% trans "Rename" %}</a>
<a class="btn btn-sm btn-outline-danger" href="{% url 'admin:tag_delete' tag.id %}">{% trans "Delete" %}</a>
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
<div role="status">
<div id="no-match" class="text-center text-body-secondary py-3" hidden>{% trans "No tag matches the search." %}</div>
</div>
</div>
{% endif %}
{% endblock content %}
{% block script %}
{% if tags %}<script src="{% static 'js/table_filter.js' %}"></script>{% endif %}
{% endblock script %}

View file

@ -30,6 +30,14 @@ class TagAdminTests(TestCase):
self.assertEqual([(t.name, t.vm_count, t.host_count) for t in response.context["tags"]], [("prod", 1, 1)])
self.assertContains(response, 'class="badge tag-badge') # as small as on the other pages
def test_the_list_is_in_name_order_and_searched(self):
Tag.objects.create(name="web")
Tag.objects.create(name="db")
response = self.client.get(reverse("admin:tag_list"))
self.assertEqual([t.name for t in response.context["tags"]], ["db", "prod", "web"])
self.assertContains(response, 'id="filter"')
self.assertContains(response, 'data-search="web"')
def test_a_rename_keeps_the_tag_on_its_vms_hosts_and_removed_vms(self):
self.client.post(reverse("admin:tag_update", args=[self.prod.pk]), {"name": "Live"})
self.prod.refresh_from_db()

View file

@ -470,7 +470,9 @@ def tombstone_delete(request, pk):
@superuser_only
def tag_list(request):
"""The tags of VMs and hosts, with how many of each have them."""
tags = Tag.objects.annotate(vm_count=Count("instances", distinct=True), host_count=Count("computes", distinct=True))
# in name order: a query that counts leaves the model's ordering out
tags = Tag.objects.annotate(vm_count=Count("instances", distinct=True),
host_count=Count("computes", distinct=True)).order_by("name")
return render(request, "admin/tag_list.html", {"tags": list(tags)})