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

fix(tags): at most 12 characters; "…" does not move the columns

- A new or renamed tag has at most 12 characters (computes.models.TAG_LENGTH,
  a validator: the column keeps 32, so names made before the limit stay
  usable until renamed and the migration changes no SQL); the tag box
  offers no longer name.
- The tags "…" shows in a table's cell go below the others within the
  cell's width as it is, so the list's columns no longer move.
This commit is contained in:
catborise 2026-11-19 09:34:07 +03:00
parent 0e711ec058
commit bc1894e4d8
12 changed files with 115 additions and 78 deletions

View file

@ -3,7 +3,7 @@ from django.db import transaction
from django.utils.translation import gettext_lazy as _
from vrtManager.connection import CONN_SOCKET, CONN_SSH, CONN_TCP, CONN_TLS
from computes.models import TAG_NAME, Compute, Tag
from computes.models import TAG_LENGTH, TAG_NAME, Compute, Tag
from computes.utils import lock_ownership
from .validators import validate_hostname
@ -60,9 +60,10 @@ class TagsField(forms.Field):
return set(self.to_python(initial)) != set(self.to_python(data))
def validate(self, value):
existing = set(Tag.objects.filter(name__in=value).values_list("name", flat=True))
for name in value:
if len(name) > 32:
raise forms.ValidationError(_("A tag has at most 32 characters"))
if name not in existing: # a tag made before the limit can still be given
TAG_LENGTH(name)
TAG_NAME(name)

View file

@ -0,0 +1,19 @@
# Generated by Django 5.2.18 on 2026-11-19 06:31
import django.core.validators
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('computes', '0012_tags'),
]
operations = [
migrations.AlterField(
model_name='tag',
name='name',
field=models.CharField(max_length=32, unique=True, validators=[django.core.validators.RegexValidator('^[a-z0-9][a-z0-9._-]*\\Z', 'A tag is letters a-z, digits, dots, dashes and underscores, and begins with a letter or digit'), django.core.validators.MaxLengthValidator(12, 'A tag has at most %(limit_value)d characters')], verbose_name='name'),
),
]

View file

@ -2,7 +2,7 @@ from collections import Counter
from ipaddress import ip_address
from django.core.exceptions import ValidationError
from django.core.validators import RegexValidator
from django.core.validators import MaxLengthValidator, RegexValidator
from django.db import IntegrityError, transaction
from django.db.models import CharField, DateTimeField, IntegerField, ManyToManyField, Model, Q, UniqueConstraint
from django.utils.functional import cached_property
@ -16,6 +16,9 @@ from vrtManager.connection import connection_manager
from vrtManager.hostdetails import wvmHostDetails
# a new or renamed tag's length, so its badge stays short in a list's row; the
# column keeps 32: names made before the limit stay until renamed
TAG_LENGTH = MaxLengthValidator(12, _("A tag has at most %(limit_value)d characters"))
TAG_NAME = RegexValidator(r"^[a-z0-9][a-z0-9._-]*\Z",
_("A tag is letters a-z, digits, dots, dashes and underscores, and begins with a letter or digit"))
@ -23,7 +26,7 @@ TAG_NAME = RegexValidator(r"^[a-z0-9][a-z0-9._-]*\Z",
class Tag(Model):
"""A name given to VMs and hosts, kept in lower case: one name is one tag."""
name = CharField(_("name"), max_length=32, unique=True, validators=[TAG_NAME])
name = CharField(_("name"), max_length=32, unique=True, validators=[TAG_NAME, TAG_LENGTH])
class Meta:
ordering = ["name"]

View file

@ -23,7 +23,11 @@ class TagsFieldTests(TestCase):
self.assertEqual(TagsField().clean(["Prod", "db", "prod"]), ["prod", "db"])
def test_a_name_is_letters_digits_and_dot_dash_underscore(self):
for bad in ("two words", "-lead", "ç", "x" * 33):
self.assertEqual(TagsField().clean(["x" * 12]), ["x" * 12])
# a tag made before the limit can still be given
Tag.objects.create(name="y" * 20)
self.assertEqual(TagsField().clean(["y" * 20]), ["y" * 20])
for bad in ("two words", "-lead", "ç", "x" * 13):
with self.subTest(bad), self.assertRaises(forms.ValidationError):
TagsField().clean([bad])