1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-11-11 12:46:49 +00:00

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 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Kk4h2TcUiaWmU5MUszYSea
This commit is contained in:
catborise 2026-11-10 09:15:43 +03:00
parent 0368d94f27
commit aefe551450
8 changed files with 44 additions and 2 deletions

View file

@ -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:

View file

@ -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(

View file

@ -254,6 +254,7 @@ def user_instance_create(request, user_id):
{
"form": form,
"title": _("Create User Instance"),
"searchable": True,
},
)

View file

@ -15,7 +15,7 @@
{# users who are not owners yet; a superuser can use every VM #}
{% if users %}
<label class="form-label" for="add-owner-user">{% trans "User" %}</label>
<select class="form-select" name="user_id" id="add-owner-user">
<select class="form-select" name="user_id" id="add-owner-user" data-searchable>
{% for user in users %}
<option value="{{ user.id }}">{{ user.username }}</option>
{% endfor %}

View file

@ -7,6 +7,10 @@
{% block title %}{% trans "Instance" %} - {{ instance.name }}{% endblock %}
{% block style %}
{% if request.user.is_superuser and users %}<link href="{% static 'css/tom-select.bootstrap5.min.css' %}" rel="stylesheet">{% 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 #}
<script src="{% static 'js/tom-select.complete.min.js' %}"></script>
<script src="{% static 'js/searchable_select.js' %}"></script>
{% endif %}
<script src="{% static 'js/ace/ace.js' %}" type="text/javascript" charset="utf-8"></script>
<script src="{% static 'js/xml_editor.js' %}"></script>
<script src="{% static 'js/chart.umd.min.js' %}"></script>

View file

@ -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

View file

@ -0,0 +1,8 @@
// A <select data-searchable> of many users or VMs gets a search box (Tom Select).
document.querySelectorAll("select[data-searchable]").forEach(function (select) {
new TomSelect(select, {
// the box leaves its focused, taller look at once: else the next tap's
// button moves away under the finger on a phone
onItemAdd: function () { this.blur(); },
});
});

View file

@ -1,9 +1,14 @@
{% extends "base.html" %}
{% load django_bootstrap5 %}
{% load i18n %}
{% load static %}
{% block title %}{{ title }}{% endblock %}
{% block style %}
{% if searchable %}<link href="{% static 'css/tom-select.bootstrap5.min.css' %}" rel="stylesheet">{% endif %}
{% endblock style %}
{% block page_header %}
<div class="row justify-content-center">
<div class="col-lg-8"><h3 class="mb-3">{{ title }}</h3></div>
@ -26,3 +31,10 @@
</div>
</div>
{% endblock content %}
{% block script %}
{% if searchable %}{# its selects with data-searchable #}
<script src="{% static 'js/tom-select.complete.min.js' %}"></script>
<script src="{% static 'js/searchable_select.js' %}"></script>
{% endif %}
{% endblock script %}