1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-12-02 17:06:09 +00:00
webvirtcloud/webvirtcloud/bootstrap.py
catborise 4881c3cd3e fix(ui): forms ask one thing at a time, offer the host's pools and say less twice
- A backup schedule asks Which VMs first (a compute's, chosen VMs or a
  tag's) and shows that one field only (policy_scope.js); the other two
  are not read, so a choice left in them is neither saved nor an error.
- A backup target's, a restore's and a disk restore's storage pool
  field offers the chosen compute's active file pools, the ones a
  restore copies into (a target's: directory and NFS only), from
  vmbackups.views.pools, which reads them without a refresh
  (pool_choices.js); a name can still be typed and is checked as before.
- A field's label is no longer repeated inside it as a placeholder, and
  a list's empty choice says Choose… instead of Django's dashes
  (webvirtcloud.bootstrap.FieldRenderer).
- The schedule, target, e-mail OTP and VM delete forms put Cancel and
  their main button at the right, as the other forms do.
2026-11-22 00:25:53 +03:00

26 lines
1.2 KiB
Python

"""django-bootstrap5's field renderer, marking only the fields in error."""
from django.db.models.fields import BLANK_CHOICE_DASH
from django.forms import ModelChoiceField
from django.utils.translation import gettext_lazy as _
from django_bootstrap5.renderers import FieldRenderer as BootstrapFieldRenderer
DASH = BLANK_CHOICE_DASH[0][1]
class FieldRenderer(BootstrapFieldRenderer):
"""A sent form marks its fields in error red and leaves the others as
they were: a green "valid" mark on a form that was not saved reads as if
it had been. A list's empty choice says to choose, not Django's dashes."""
def __init__(self, field, **kwargs):
super().__init__(field, **kwargs)
form_field = getattr(field, "field", None) # the form's field, its own copy for this form
if isinstance(form_field, ModelChoiceField):
if form_field.empty_label == DASH:
form_field.empty_label = _("Choose…")
elif any(label == DASH for _value, label in getattr(form_field, "choices", ())):
form_field.choices = [(value, _("Choose…") if label == DASH else label) for value, label in form_field.choices]
def get_server_side_validation_classes(self):
return "is-invalid" if self.field_errors else ""