mirror of
https://github.com/retspen/webvirtcloud
synced 2026-12-02 08:56:07 +00:00
fix(tags): third audit: host form and tag delete, hosts list badges
- A host's form saves the host and its tags in one transaction under the ownership lock: a tag the Tags page deletes meanwhile is made again, instead of a 500 after the host's other changes were saved. - The hosts list shows three tags and "…" for the others, as the VM list. - "…" shows the other tags on any page, the tag filter or not.
This commit is contained in:
parent
7810fb39fe
commit
6cdaa2affd
4 changed files with 46 additions and 22 deletions
|
|
@ -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
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -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' with filter=True %}</div>{% endif %}{% endwith %}
|
||||
{% with tags=compute.tags.all %}{% if tags %}<div class="mt-1">{% include 'common/tag_badges.html' with tags=tags|slice:":3" more=tags|slice:"3:" filter=True %}</div>{% endif %}{% endwith %}
|
||||
{% if compute.status is not True %}
|
||||
<div class="small text-danger">{{ compute.connection_error }}</div>
|
||||
{% endif %}
|
||||
|
|
|
|||
|
|
@ -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, '<option value="rack-1">rack-1 (1)</option>', 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"')
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue