Fix regex for IPAddress.dns_name (but see #3106)

This commit is contained in:
Jeremy Stretch 2019-04-25 14:49:52 -04:00
parent 06245f6422
commit e1bca52d57
3 changed files with 16 additions and 7 deletions

View File

@ -14,6 +14,6 @@ class Migration(migrations.Migration):
migrations.AddField(
model_name='ipaddress',
name='dns_name',
field=models.CharField(blank=True, max_length=255, validators=[django.core.validators.RegexValidator(code='invalid', message='Only alphanumeric characters, hyphens, and periods are allowed in DNS names', regex='^[0-9A-Za-z\\.-]+$')]),
field=models.CharField(blank=True, max_length=255, validators=[django.core.validators.RegexValidator(code='invalid', message='Only alphanumeric characters, hyphens, and periods are allowed in DNS names', regex='^[0-9A-Za-z.-]+$')]),
),
]

View File

@ -368,11 +368,15 @@ class Prefix(ChangeLoggedModel, CustomFieldModel):
})
def save(self, *args, **kwargs):
if self.prefix:
if isinstance(self.prefix, netaddr.IPNetwork):
# Clear host bits from prefix
self.prefix = self.prefix.cidr
# Infer address family from IPNetwork object
# Record address family
self.family = self.prefix.version
super().save(*args, **kwargs)
def to_csv(self):
@ -579,7 +583,7 @@ class IPAddress(ChangeLoggedModel, CustomFieldModel):
blank=True,
validators=[DNSValidator],
verbose_name='DNS Name',
help_text='Hostname or FQDN'
help_text='Hostname or FQDN (not case-sensitive)'
)
description = models.CharField(
max_length=100,
@ -633,9 +637,14 @@ class IPAddress(ChangeLoggedModel, CustomFieldModel):
})
def save(self, *args, **kwargs):
if self.address:
# Infer address family from IPAddress object
# Record address family
if isinstance(self.address, netaddr.IPNetwork):
self.family = self.address.version
# Force dns_name to lowercase
self.dns_name = self.dns_name.lower()
super().save(*args, **kwargs)
def log_change(self, user, request_id, action):

View File

@ -2,7 +2,7 @@ from django.core.validators import RegexValidator
DNSValidator = RegexValidator(
regex='^[a-z]+$',
regex='^[0-9A-Za-z.-]+$',
message='Only alphanumeric characters, hyphens, and periods are allowed in DNS names',
code='invalid'
)