From d1de4e365cd1f5b09393dc5a6b994ef853ae78cb Mon Sep 17 00:00:00 2001 From: Gabriel Detraz Date: Sat, 18 Apr 2020 19:17:04 +0200 Subject: [PATCH 1/3] Fix #248 --- machines/models.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/machines/models.py b/machines/models.py index 96a6eb48..55fb8838 100644 --- a/machines/models.py +++ b/machines/models.py @@ -1623,15 +1623,15 @@ class Domain(RevMixin, AclMixin, FieldPermissionModelMixin, models.Model): raise ValidationError( _("You can't create a CNAME record pointing to itself.") ) - HOSTNAME_LABEL_PATTERN = re.compile(r"(?!-)[A-Z\d-]+(? 63: + HOSTNAME_LABEL_PATTERN = re.compile(r"(?!-)[a-z\d-]+(? 63: raise ValidationError( - _("The domain name %s is too long (over 63 characters).") % dns + _("The domain name %s is too long (over 63 characters).") % self.name ) - if not HOSTNAME_LABEL_PATTERN.match(dns): + if not HOSTNAME_LABEL_PATTERN.match(self.name): raise ValidationError( - _("The domain name %s contains forbidden characters.") % dns + _("The domain name %s contains forbidden characters.") % self.name ) self.validate_unique() super(Domain, self).clean() From c8ede78c6645ec7c215fe8cf7d892f49a1b329ff Mon Sep 17 00:00:00 2001 From: Gabriel Detraz Date: Sat, 18 Apr 2020 19:57:14 +0200 Subject: [PATCH 2/3] Fix doublons proprement en migration --- .../migrations/0107_fix_lowercase_domain.py | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 machines/migrations/0107_fix_lowercase_domain.py diff --git a/machines/migrations/0107_fix_lowercase_domain.py b/machines/migrations/0107_fix_lowercase_domain.py new file mode 100644 index 00000000..c9aebba2 --- /dev/null +++ b/machines/migrations/0107_fix_lowercase_domain.py @@ -0,0 +1,31 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import migrations +from django.core.exceptions import ValidationError + +def fix_duplicate(apps, schema_editor): + db_alias = schema_editor.connection.alias + domain = apps.get_model("machines", "Domain") + machines_to_fix = list(filter(lambda m : not m.name.islower(), domain.objects.using(db_alias).all())) + for machine in machines_to_fix: + try: + machine.name = machine.name.lower() + machine.validate_unique() + machine.clean() + except ValidationError: + machine.name = machine.name.lower() + str(machine.interface_parent.id) + machine.clean() + machine.save() + +def unfix_duplicate(apps, schema_editor): + return + + +class Migration(migrations.Migration): + + dependencies = [("machines", "0106_auto_20191120_0159")] + + operations = [ + migrations.RunPython(fix_duplicate, unfix_duplicate), + ] From 79cfc6b6b72476df084d5f8952a895273b9e4d0e Mon Sep 17 00:00:00 2001 From: Gabriel Detraz Date: Sat, 18 Apr 2020 20:21:43 +0200 Subject: [PATCH 3/3] Add logger message and rename var --- .../migrations/0107_fix_lowercase_domain.py | 23 +++++++++++-------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/machines/migrations/0107_fix_lowercase_domain.py b/machines/migrations/0107_fix_lowercase_domain.py index c9aebba2..e47a4680 100644 --- a/machines/migrations/0107_fix_lowercase_domain.py +++ b/machines/migrations/0107_fix_lowercase_domain.py @@ -3,20 +3,25 @@ from __future__ import unicode_literals from django.db import migrations from django.core.exceptions import ValidationError +import logging def fix_duplicate(apps, schema_editor): + logger = logging.getLogger(__name__) db_alias = schema_editor.connection.alias - domain = apps.get_model("machines", "Domain") - machines_to_fix = list(filter(lambda m : not m.name.islower(), domain.objects.using(db_alias).all())) - for machine in machines_to_fix: + Domain = apps.get_model("machines", "Domain") + domains_to_fix = filter(lambda m : not m.name.islower(), Domain.objects.using(db_alias).all()) + for domain in domains_to_fix: try: - machine.name = machine.name.lower() - machine.validate_unique() - machine.clean() + domain.name = domain.name.lower() + domain.validate_unique() + domain.clean() except ValidationError: - machine.name = machine.name.lower() + str(machine.interface_parent.id) - machine.clean() - machine.save() + old_name = domain.name + domain.name = domain.name.lower() + str(domain.interface_parent.id) + domain.clean() + warning_message = "Warning : Domain %s has been renamed %s due to dns uniqueness" % (old_name, domain.name) + logger.warning(warning_message) + domain.save() def unfix_duplicate(apps, schema_editor): return