1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-12-02 17:06:09 +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)})

Binary file not shown.

View file

@ -11,7 +11,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2026-11-19 08:03+0000\n"
"POT-Creation-Date: 2026-11-19 10:35+0000\n"
"PO-Revision-Date: 2020-11-04 12:00+0000\n"
"Last-Translator: catborise <muhammetalisag@gmail.com>, 2020\n"
"Language-Team: Turkish (https://www.transifex.com/catborise/teams/110663/tr/)\n"
@ -223,7 +223,7 @@ msgid "User sections"
msgstr "Kullanıcı bölümleri"
#: accounts/templates/account.html:50 accounts/templates/account.html:58
#: admin/forms.py:221 admin/templates/admin/tag_list.html:19
#: admin/forms.py:221 admin/templates/admin/tag_list.html:25
#: computes/templates/computes/list.html:33
#: computes/templates/computes/list.html:61 computes/templates/overview.html:90
#: instances/views.py:267 vmbackups/templates/vmbackups/policies.html:17
@ -468,7 +468,7 @@ msgstr "%(name)s API anahtarı için işlemler"
#: accounts/templates/api_token_list.html:12
#: accounts/templates/ssh_key_list.html:13
#: admin/templates/admin/group_list.html:44
#: admin/templates/admin/tag_list.html:32
#: admin/templates/admin/tag_list.html:38
#: admin/templates/admin/tombstone_list.html:43
#: admin/templates/admin/user_list.html:74
#: admin/templates/admin/user_list.html:76
@ -884,7 +884,7 @@ msgstr "Bu izinleri kimlerin alacağı."
#: admin/templates/admin/group_form.html:60
#: admin/templates/admin/group_list.html:25 admin/templates/admin/logs.html:20
#: admin/templates/admin/logs.html:26
#: admin/templates/admin/logs.html:26 admin/templates/admin/tag_list.html:18
#: admin/templates/admin/tombstone_list.html:20
#: admin/templates/admin/user_list.html:20
#: instances/templates/allinstances.html:11 networks/templates/network.html:130
@ -1029,14 +1029,14 @@ msgstr "Tamamlandı"
msgid "Failed"
msgstr "Başarısız"
#: admin/templates/admin/tag_list.html:5 admin/templates/admin/tag_list.html:7
#: computes/forms.py:55 instances/templates/instances/settings_tab.html:925
#: instances/views.py:244 templates/common/tag_filter.html:6
#: templates/navbar.html:39
#: admin/templates/admin/tag_list.html:6 admin/templates/admin/tag_list.html:8
#: admin/templates/admin/tag_list.html:17 computes/forms.py:55
#: instances/templates/instances/settings_tab.html:925 instances/views.py:244
#: templates/common/tag_filter.html:6 templates/navbar.html:39
msgid "Tags"
msgstr "Etiketler"
#: admin/templates/admin/tag_list.html:10
#: admin/templates/admin/tag_list.html:11
msgid ""
"The tags of VMs and hosts. A tag is made when it is first given, on a VM's "
"Options tab or a host's form."
@ -1045,20 +1045,24 @@ msgstr ""
"makinenin Seçenekler sekmesinde ya da bir sunucunun formunda verildiğinde "
"oluşur."
#: admin/templates/admin/tag_list.html:12
#: admin/templates/admin/tag_list.html:13
msgid "No tags yet"
msgstr "Henüz etiket yok"
#: admin/templates/admin/tag_list.html:18
msgid "Search tags"
msgstr "Etiket ara"
#: admin/templates/admin/tag_list.html:24
msgid "Tag"
msgstr "Etiket"
#: admin/templates/admin/tag_list.html:20
#: admin/templates/admin/tag_list.html:26
#: computes/templates/computes/list.html:26
msgid "Hosts"
msgstr "Sunucular"
#: admin/templates/admin/tag_list.html:21
#: admin/templates/admin/tag_list.html:27
#: computes/templates/computes/instances.html:58
#: computes/templates/computes/list.html:64
#: instances/templates/allinstances_index_grouped.html:18
@ -1072,10 +1076,14 @@ msgstr "Sunucular"
msgid "Actions"
msgstr "Eylemler"
#: admin/templates/admin/tag_list.html:31
#: admin/templates/admin/tag_list.html:37
msgid "Rename"
msgstr "Yeniden adlandır"
#: admin/templates/admin/tag_list.html:46
msgid "No tag matches the search."
msgstr "Aramayla eşleşen etiket yok."
#: admin/templates/admin/tombstone_list.html:6
#: admin/templates/admin/tombstone_list.html:8
#: admin/templates/admin/tombstone_list.html:19 templates/navbar.html:38
@ -1328,12 +1336,12 @@ msgid "Its owners and tags no longer come back if its UUID shows up again."
msgstr ""
"UUID'si yeniden görünse bile sahipleri ve etiketleri artık geri gelmez."
#: admin/views.py:510
#: admin/views.py:512
#, python-format
msgid "Rename tag %(tag)s"
msgstr "%(tag)s etiketini yeniden adlandır"
#: admin/views.py:515
#: admin/views.py:517
#, python-format
msgid "The backup schedule %(schedules)s backs up its VMs: change it first"
msgid_plural ""
@ -1345,24 +1353,24 @@ msgstr[1] ""
"%(schedules)s yedekleme zamanlamaları bu etiketin sanal makinelerini "
"yedekliyor: önce onları değiştirin"
#: admin/views.py:533
#: admin/views.py:535
#, python-format
msgid "Tag %(tag)s deleted"
msgstr "%(tag)s etiketi silindi"
#: admin/views.py:541
#: admin/views.py:543
#, python-format
msgid "It is taken off %(vms)s and %(hosts)s."
msgstr "%(vms)s ve %(hosts)s üzerinden kaldırılır."
#: admin/views.py:542
#: admin/views.py:544
#, python-format
msgid "%(count)s VM"
msgid_plural "%(count)s VMs"
msgstr[0] "%(count)s sanal makine"
msgstr[1] "%(count)s sanal makine"
#: admin/views.py:543
#: admin/views.py:545
#, python-format
msgid "%(count)s host"
msgid_plural "%(count)s hosts"

Binary file not shown.

View file

@ -7,7 +7,7 @@ msgid ""
msgstr ""
"Project-Id-Version: \n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2026-11-19 08:03+0000\n"
"POT-Creation-Date: 2026-11-19 10:35+0000\n"
"PO-Revision-Date: \n"
"Last-Translator: \n"
"Language-Team: \n"
@ -218,7 +218,7 @@ msgid "User sections"
msgstr "用户页面导航"
#: accounts/templates/account.html:50 accounts/templates/account.html:58
#: admin/forms.py:221 admin/templates/admin/tag_list.html:19
#: admin/forms.py:221 admin/templates/admin/tag_list.html:25
#: computes/templates/computes/list.html:33
#: computes/templates/computes/list.html:61 computes/templates/overview.html:90
#: instances/views.py:267 vmbackups/templates/vmbackups/policies.html:17
@ -461,7 +461,7 @@ msgstr "API 令牌 %(name)s 的操作"
#: accounts/templates/api_token_list.html:12
#: accounts/templates/ssh_key_list.html:13
#: admin/templates/admin/group_list.html:44
#: admin/templates/admin/tag_list.html:32
#: admin/templates/admin/tag_list.html:38
#: admin/templates/admin/tombstone_list.html:43
#: admin/templates/admin/user_list.html:74
#: admin/templates/admin/user_list.html:76
@ -858,7 +858,7 @@ msgstr "获得这些权限的用户。"
#: admin/templates/admin/group_form.html:60
#: admin/templates/admin/group_list.html:25 admin/templates/admin/logs.html:20
#: admin/templates/admin/logs.html:26
#: admin/templates/admin/logs.html:26 admin/templates/admin/tag_list.html:18
#: admin/templates/admin/tombstone_list.html:20
#: admin/templates/admin/user_list.html:20
#: instances/templates/allinstances.html:11 networks/templates/network.html:130
@ -1001,33 +1001,37 @@ msgstr "完成"
msgid "Failed"
msgstr "失败"
#: admin/templates/admin/tag_list.html:5 admin/templates/admin/tag_list.html:7
#: computes/forms.py:55 instances/templates/instances/settings_tab.html:925
#: instances/views.py:244 templates/common/tag_filter.html:6
#: templates/navbar.html:39
#: admin/templates/admin/tag_list.html:6 admin/templates/admin/tag_list.html:8
#: admin/templates/admin/tag_list.html:17 computes/forms.py:55
#: instances/templates/instances/settings_tab.html:925 instances/views.py:244
#: templates/common/tag_filter.html:6 templates/navbar.html:39
msgid "Tags"
msgstr "标签"
#: admin/templates/admin/tag_list.html:10
#: admin/templates/admin/tag_list.html:11
msgid ""
"The tags of VMs and hosts. A tag is made when it is first given, on a VM's "
"Options tab or a host's form."
msgstr "虚拟机和主机的标签。标签在首次于虚拟机的“选项”选项卡或主机表单中添加时创建。"
#: admin/templates/admin/tag_list.html:12
#: admin/templates/admin/tag_list.html:13
msgid "No tags yet"
msgstr "还没有标签"
#: admin/templates/admin/tag_list.html:18
msgid "Search tags"
msgstr "搜索标签"
#: admin/templates/admin/tag_list.html:24
msgid "Tag"
msgstr "标签"
#: admin/templates/admin/tag_list.html:20
#: admin/templates/admin/tag_list.html:26
#: computes/templates/computes/list.html:26
msgid "Hosts"
msgstr "主机"
#: admin/templates/admin/tag_list.html:21
#: admin/templates/admin/tag_list.html:27
#: computes/templates/computes/instances.html:58
#: computes/templates/computes/list.html:64
#: instances/templates/allinstances_index_grouped.html:18
@ -1041,10 +1045,14 @@ msgstr "主机"
msgid "Actions"
msgstr "操作"
#: admin/templates/admin/tag_list.html:31
#: admin/templates/admin/tag_list.html:37
msgid "Rename"
msgstr "重命名"
#: admin/templates/admin/tag_list.html:46
msgid "No tag matches the search."
msgstr "没有与搜索匹配的标签。"
#: admin/templates/admin/tombstone_list.html:6
#: admin/templates/admin/tombstone_list.html:8
#: admin/templates/admin/tombstone_list.html:19 templates/navbar.html:38
@ -1286,35 +1294,35 @@ msgstr "已删除已移除的虚拟机 %(name)s"
msgid "Its owners and tags no longer come back if its UUID shows up again."
msgstr "若其 UUID 再次出现,其所有者和标签将不再恢复。"
#: admin/views.py:510
#: admin/views.py:512
#, python-format
msgid "Rename tag %(tag)s"
msgstr "重命名标签 %(tag)s"
#: admin/views.py:515
#: admin/views.py:517
#, python-format
msgid "The backup schedule %(schedules)s backs up its VMs: change it first"
msgid_plural ""
"The backup schedules %(schedules)s back up its VMs: change them first"
msgstr[0] "备份计划 %(schedules)s 正在备份该标签的虚拟机:请先修改计划"
#: admin/views.py:533
#: admin/views.py:535
#, python-format
msgid "Tag %(tag)s deleted"
msgstr "标签 %(tag)s 已删除"
#: admin/views.py:541
#: admin/views.py:543
#, python-format
msgid "It is taken off %(vms)s and %(hosts)s."
msgstr "它将从 %(vms)s 和 %(hosts)s 上移除。"
#: admin/views.py:542
#: admin/views.py:544
#, python-format
msgid "%(count)s VM"
msgid_plural "%(count)s VMs"
msgstr[0] "%(count)s 个虚拟机"
#: admin/views.py:543
#: admin/views.py:545
#, python-format
msgid "%(count)s host"
msgid_plural "%(count)s hosts"