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