From aefe5514503592f588b13ac26202364527a0f49d Mon Sep 17 00:00:00 2001 From: catborise Date: Tue, 10 Nov 2026 09:15:43 +0300 Subject: [PATCH] feat(ui): the owner and VM lists of the ownership forms are searchable The VM page's Add owner and a user's Add VM page listed every user and every VM in a plain drop-down: with 800 users and 300 VMs one was found by scrolling. They now have a search box (Tom Select, already used by the create form), through searchable_select.js for a select with data-searchable. The box loses focus once an item is chosen: focused, it is a line taller, and on a phone the tap on Save shrank it and moved the button away from under the finger, so the first tap did nothing. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Kk4h2TcUiaWmU5MUszYSea --- accounts/forms.py | 4 +++- accounts/tests/test_views.py | 6 ++++++ accounts/views.py | 1 + instances/templates/add_instance_owner_block.html | 2 +- instances/templates/instance.html | 8 ++++++++ instances/tests/test_pending_edits.py | 5 +++++ static/js/searchable_select.js | 8 ++++++++ templates/common/form.html | 12 ++++++++++++ 8 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 static/js/searchable_select.js diff --git a/accounts/forms.py b/accounts/forms.py index f2d665fb..2e185540 100644 --- a/accounts/forms.py +++ b/accounts/forms.py @@ -2,7 +2,7 @@ from appsettings.settings import app_settings from django.contrib.auth import get_user_model from django.contrib.auth.forms import AuthenticationForm from django.core.validators import MaxLengthValidator -from django.forms import CharField, EmailField, Form, ModelForm, TypedChoiceField, ValidationError +from django.forms import CharField, EmailField, Form, ModelForm, Select, TypedChoiceField, ValidationError from django.utils.translation import gettext_lazy as _ from .models import UserInstance, UserSSHKey @@ -36,6 +36,8 @@ class UserInstanceForm(ModelForm): labels = {"is_change": _("Change settings"), "is_delete": _("Delete the VM"), "is_vnc": _("Change console settings")} help_texts = {"is_vnc": _("Only together with Change settings")} + # a list of every user and every VM + widgets = {"user": Select(attrs={"data-searchable": ""}), "instance": Select(attrs={"data-searchable": ""})} class UniqueEmailMixin: diff --git a/accounts/tests/test_views.py b/accounts/tests/test_views.py index b0685d11..cec60105 100644 --- a/accounts/tests/test_views.py +++ b/accounts/tests/test_views.py @@ -10,6 +10,7 @@ from django.shortcuts import reverse from django.test import Client, RequestFactory, TestCase, override_settings from instances.models import Instance from libvirt import libvirtError +from lxml import html from accounts.forms import UserInstanceForm, UserSSHKeyForm @@ -115,6 +116,11 @@ class AccountsTestCase(TestCase): logged_in = self.client.login(request=RequestFactory().get("/"), username="test", password="Wvc-changed-9") self.assertTrue(logged_in) + def test_the_user_and_the_vm_of_a_new_ownership_are_searched(self): + doc = html.fromstring(self.client.get(reverse("accounts:user_instance_create", args=[self.test_user.id])).content) + self.assertEqual(doc.xpath("//select[@data-searchable]/@name"), ["user", "instance"]) + self.assertIn("/static/js/searchable_select.js", doc.xpath("//script/@src")) + def test_user_instance_create_update_delete(self): # create response = self.client.get( diff --git a/accounts/views.py b/accounts/views.py index b8171944..0ffcdb34 100644 --- a/accounts/views.py +++ b/accounts/views.py @@ -254,6 +254,7 @@ def user_instance_create(request, user_id): { "form": form, "title": _("Create User Instance"), + "searchable": True, }, ) diff --git a/instances/templates/add_instance_owner_block.html b/instances/templates/add_instance_owner_block.html index 8c971b2f..426ef730 100644 --- a/instances/templates/add_instance_owner_block.html +++ b/instances/templates/add_instance_owner_block.html @@ -15,7 +15,7 @@ {# users who are not owners yet; a superuser can use every VM #} {% if users %} - {% for user in users %} {% endfor %} diff --git a/instances/templates/instance.html b/instances/templates/instance.html index 54acb0b8..b7254639 100644 --- a/instances/templates/instance.html +++ b/instances/templates/instance.html @@ -7,6 +7,10 @@ {% block title %}{% trans "Instance" %} - {{ instance.name }}{% endblock %} +{% block style %} +{% if request.user.is_superuser and users %}{% endif %} +{% endblock style %} + {% block page_header %}{% endblock page_header %}{# the card below has the heading #} {% block content %} @@ -126,6 +130,10 @@ {% endblock %} {% block script %} +{% if request.user.is_superuser and users %}{# Add owner's user list #} + + +{% endif %} diff --git a/instances/tests/test_pending_edits.py b/instances/tests/test_pending_edits.py index f1d953a3..85c03253 100644 --- a/instances/tests/test_pending_edits.py +++ b/instances/tests/test_pending_edits.py @@ -270,6 +270,11 @@ class PendingEditsTestCase(TestCase): [f"/instances/{self.instance.id}/#users"] * 2) # not an owner yet and not an administrator, who can use every VM anyway self.assertEqual(doc.xpath("//select[@id='add-owner-user']/option/text()"), ["pe-new"]) + # a search box for a list of every user (searchable_select.js) + self.assertEqual(doc.xpath("//select[@id='add-owner-user']/@data-searchable"), [""]) + self.assertIn("/static/js/searchable_select.js", doc.xpath("//script/@src")) + User.objects.filter(username="pe-new").delete() # no one left to add: no search box to load + self.assertNotIn("/static/js/searchable_select.js", self.page().xpath("//script/@src")) def test_a_paused_vm_offers_to_turn_vcpus_on_only(self): # unplugging a vCPU needs the guest, which a paused VM does not run diff --git a/static/js/searchable_select.js b/static/js/searchable_select.js new file mode 100644 index 00000000..a2147cb9 --- /dev/null +++ b/static/js/searchable_select.js @@ -0,0 +1,8 @@ +// A