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

fix(instances): a refused VM create form comes back with what was entered

When the server refused a VM create submission (a field error, a busy
resource, a reused submission key, an existing VM name, a missing MAC
for a new disk), the page came back on its first tab with the flavor
dialog closed and every entered value gone: the hand-written fields
showed fixed defaults, and the errors sat in a block at the top of the
page, outside #messages and tied to no field.

The view builds one NewVMForm per create form on the page (custom,
template, one per flavor; create_forms), each with its own defaults; a
hidden form_id, which is also the prefix of the form's field ids, names
the submitted one, and it comes back bound. Every field renders from its
own form and is marked by two small template tags (form_fields:
error_attrs, field_error) when refused; a summary at the top of the
form lists all of its errors, also those of fields it keeps hidden. The
submitted tab or flavor dialog, marked data-form-id, reopens with any
"Advanced" section that holds an error; a reopened dialog shows the
messages too; the template form gets its pool, template and storage
back. base.html's error focus is a function that walks the places an
error can be, and a page can call it once it is set up, as the create
page does. form_id changes nothing in how a POST is processed; one of
an unknown form (a flavor deleted since) has its errors in the
messages. The default video now applies to every create form.

The custom form's disks and networks are not restored yet.
This commit is contained in:
catborise 2026-10-19 23:13:25 +03:00
parent 35468bc0fd
commit dba41f97f3
9 changed files with 640 additions and 164 deletions

View file

@ -2009,6 +2009,21 @@ def create_instance_select_type(request, compute_id):
return render(request, "create_instance_w1.html", locals())
def create_forms(flavors, defaults, mac):
"""One unbound NewVMForm per create form of the page, keyed by its
form_id, each with that form's defaults."""
forms = {
"custom": NewVMForm(initial={**defaults, "vcpu": 1, "memory": 512}),
"template": NewVMForm(initial={**defaults, "vcpu": 1, "memory": 512, "mac": mac}),
}
for flavor in flavors:
forms[f"flavor{flavor.id}"] = NewVMForm(initial={
**defaults, "vcpu": flavor.vcpu, "memory": flavor.memory, "hdd_size": flavor.disk,
"mac": mac,
})
return forms
@superuser_only
def create_instance(request, compute_id, arch, machine):
"""
@ -2028,6 +2043,8 @@ def create_instance(request, compute_id, arch, machine):
compute = get_object_or_404(Compute, pk=compute_id)
flavors = Flavor.objects.filter().order_by("id")
appsettings = AppSettings.objects.all()
vm_forms = {}
submitted_form = ""
try:
conn = wvmCreate(
@ -2083,6 +2100,13 @@ def create_instance(request, compute_id, arch, machine):
flavor_form = FlavorForm()
vm_forms = create_forms(flavors, mac=mac_auto, defaults={
"firmware": default_firmware, "vcpu_mode": default_cpu_mode, "cache_mode": default_cache,
"net_model": default_nic_type, "listener_addr": "0.0.0.0", "add_cdrom": default_cdrom,
"add_input": default_input_device_bus, "video": default_video, "qemu_ga": True,
"virtio": bool(virtio_support),
})
if conn:
if not storages:
raise util.OperationError(_("You haven't defined any storage pools"))
@ -2092,6 +2116,16 @@ def create_instance(request, compute_id, arch, machine):
if request.method == "POST":
if "create" in request.POST:
form = NewVMForm(request.POST)
# the submitted form comes back bound if the page shows it again;
# a stale form_id (a flavor deleted since) has no form for its errors
form_id = request.POST.get("form_id", "")
if form_id in vm_forms:
vm_forms[form_id] = form
submitted_form = form_id
else:
for name, errors in form.errors.items():
for error in errors:
messages.error(request, f"{form[name].label}: {error}" if name in form.fields else error)
if form.is_valid():
data = form.cleaned_data
if data["hdd_size"] and not data["mac"]:
@ -2121,6 +2155,9 @@ def create_instance(request, compute_id, arch, machine):
except libvirtError as lib_err:
messages.error(request, lib_err)
custom_form, template_form = vm_forms.get("custom"), vm_forms.get("template")
flavor_forms = [(flavor, f"flavor{flavor.id}", vm_forms[f"flavor{flavor.id}"])
for flavor in flavors if f"flavor{flavor.id}" in vm_forms]
return render(request, "create_instance_w2.html", locals())