diff --git a/computes/forms.py b/computes/forms.py
index fa03f087..7b307c99 100644
--- a/computes/forms.py
+++ b/computes/forms.py
@@ -1,8 +1,10 @@
from django import forms
+from django.db import transaction
from django.utils.translation import gettext_lazy as _
from vrtManager.connection import CONN_SOCKET, CONN_SSH, CONN_TCP, CONN_TLS
from computes.models import TAG_NAME, Compute, Tag
+from computes.utils import lock_ownership
from .validators import validate_hostname
@@ -72,9 +74,12 @@ class ComputeTagsMixin:
self.initial["tags"] = list(self.instance.tags.values_list("name", flat=True))
def save(self, commit=True):
- compute = super().save(commit)
- if commit:
- compute.tags.set(Tag.named(self.cleaned_data["tags"]))
+ # the host and its tags in one step, not while the Tags page deletes or renames one
+ with transaction.atomic():
+ lock_ownership()
+ compute = super().save(commit)
+ if commit:
+ compute.tags.set(Tag.named(self.cleaned_data["tags"]))
return compute
diff --git a/computes/templates/computes/list.html b/computes/templates/computes/list.html
index 3178ed37..d032fdae 100644
--- a/computes/templates/computes/list.html
+++ b/computes/templates/computes/list.html
@@ -82,7 +82,7 @@
{{ compute.hostname }}
{% if compute.details %}
{{ compute.details }}
{% endif %}
- {% with tags=compute.tags.all %}{% if tags %}
{% include 'common/tag_badges.html' with filter=True %}
{% endif %}{% endwith %}
+ {% with tags=compute.tags.all %}{% if tags %}
{% include 'common/tag_badges.html' with tags=tags|slice:":3" more=tags|slice:"3:" filter=True %}
{% endif %}{% endwith %}
{% if compute.status is not True %}
{{ compute.connection_error }}
{% endif %}
diff --git a/computes/tests/test_tags.py b/computes/tests/test_tags.py
index 1f1b229b..66aacbb6 100644
--- a/computes/tests/test_tags.py
+++ b/computes/tests/test_tags.py
@@ -1,7 +1,7 @@
"""Tags of VMs and hosts: one list of names, kept with a VM that leaves its
host for a while or moves out of band, and set on the host form."""
-from unittest.mock import MagicMock
+from unittest.mock import MagicMock, patch
from django import forms
from django.contrib.auth import get_user_model
@@ -96,6 +96,23 @@ class HostTagsTests(TestCase):
{"name": "kvm1", "hostname": "192.0.2.1", "login": "wvc", "password": "", "details": ""})
self.assertFalse(compute.tags.exists())
+ def test_a_tag_deleted_meanwhile_is_made_again(self):
+ compute = Compute.objects.create(name="kvm1", hostname="192.0.2.1", login="wvc", password="pw", type=1)
+ compute.tags.add(Tag.objects.create(name="gpu"))
+ from computes import utils
+ real = utils.lock_ownership
+
+ def lock(): # the Tags page deletes it while the form waits for the lock
+ Tag.objects.filter(name="gpu").delete()
+ real()
+ with patch("computes.forms.lock_ownership", side_effect=lock):
+ response = self.client.post(reverse("compute_update", args=[compute.id]),
+ {"name": "kvm1b", "hostname": "192.0.2.1", "login": "wvc", "password": "",
+ "details": "", "tags": ["gpu"]})
+ self.assertEqual(response.status_code, 302)
+ compute.refresh_from_db()
+ self.assertEqual((compute.name, list(compute.tags.values_list("name", flat=True))), ("kvm1b", ["gpu"]))
+
def test_the_socket_form_has_them_too(self):
self.client.post(reverse("add_socket_host"), {"name": "local", "hostname": "localhost", "details": "",
"tags": ["lab"]})
@@ -103,12 +120,13 @@ class HostTagsTests(TestCase):
def test_the_hosts_list_shows_them(self):
compute = Compute.objects.create(name="kvm1", hostname="192.0.2.1", login="wvc", password="pw", type=1)
- compute.tags.add(Tag.objects.create(name="rack-1"))
+ compute.tags.add(*(Tag.objects.create(name=name) for name in ("rack-1", "gpu", "ssd", "lab")))
response = self.client.get(reverse("computes"))
+ self.assertContains(response, 'data-show-tags title="ssd"') # three, and "…" for the others
self.assertContains(response, "rack-1")
self.assertContains(response, '', html=True)
self.assertContains(response, 'data-tag-filter="rack-1"')
# a search and a tag that match no host: the page says so and clears them
self.assertContains(response, 'id="no-match"')
self.assertContains(response, 'id="clear-filters"')
- self.assertContains(response, 'data-tags="rack-1"')
+ self.assertContains(response, 'data-tags="gpu lab rack-1 ssd"')
diff --git a/static/js/table_filter.js b/static/js/table_filter.js
index fccd4976..8395af73 100644
--- a/static/js/table_filter.js
+++ b/static/js/table_filter.js
@@ -129,22 +129,23 @@ if (typeof module !== "undefined") {
keep(tags.value, key + "_tag");
filterTable(document);
});
- document.addEventListener("click", function (event) {
- var more = event.target.closest("[data-show-tags]");
- if (more) {
- more.hidden = true;
- more.nextElementSibling.hidden = false;
- more.nextElementSibling.querySelector("button").focus(); // the focus is not lost with the button
- return;
- }
- var badge = event.target.closest("[data-tag-filter]");
- if (badge) {
- tags.value = badge.dataset.tagFilter;
- keep(tags.value, key + "_tag");
- filterTable(document);
- }
- });
}
+ document.addEventListener("click", function (event) {
+ var more = event.target.closest("[data-show-tags]");
+ if (more) {
+ more.hidden = true;
+ more.nextElementSibling.hidden = false;
+ // the focus is not lost with the button: the first tag shown takes it
+ more.nextElementSibling.querySelector("button, [tabindex]")?.focus();
+ return;
+ }
+ var badge = event.target.closest("[data-tag-filter]");
+ if (badge && tags) {
+ tags.value = badge.dataset.tagFilter;
+ keep(tags.value, key + "_tag");
+ filterTable(document);
+ }
+ });
var pills = document.getElementById("state-pills");
function chooseState(pill) {
states.value = pill.dataset.pillState;