2020-01-22 16:33:34 -05:00
|
|
|
from django.core.exceptions import ValidationError
|
2020-01-22 16:26:06 -05:00
|
|
|
from django.core.validators import BaseValidator, RegexValidator
|
2024-02-20 06:44:02 -08:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2020-01-22 16:26:06 -05:00
|
|
|
|
|
|
|
|
2020-01-22 16:33:34 -05:00
|
|
|
def prefix_validator(prefix):
|
|
|
|
if prefix.ip != prefix.cidr.ip:
|
2024-02-20 06:44:02 -08:00
|
|
|
raise ValidationError(
|
|
|
|
_("{prefix} is not a valid prefix. Did you mean {suggested}?").format(
|
|
|
|
prefix=prefix, suggested=prefix.cidr
|
|
|
|
)
|
|
|
|
)
|
2020-01-22 16:33:34 -05:00
|
|
|
|
|
|
|
|
2020-01-22 16:26:06 -05:00
|
|
|
class MaxPrefixLengthValidator(BaseValidator):
|
2024-02-20 06:44:02 -08:00
|
|
|
message = _('The prefix length must be less than or equal to %(limit_value)s.')
|
2020-01-22 16:26:06 -05:00
|
|
|
code = 'max_prefix_length'
|
|
|
|
|
|
|
|
def compare(self, a, b):
|
|
|
|
return a.prefixlen > b
|
|
|
|
|
|
|
|
|
|
|
|
class MinPrefixLengthValidator(BaseValidator):
|
2024-02-20 06:44:02 -08:00
|
|
|
message = _('The prefix length must be greater than or equal to %(limit_value)s.')
|
2020-01-22 16:26:06 -05:00
|
|
|
code = 'min_prefix_length'
|
|
|
|
|
|
|
|
def compare(self, a, b):
|
|
|
|
return a.prefixlen < b
|
2019-04-22 18:10:28 -04:00
|
|
|
|
|
|
|
|
|
|
|
DNSValidator = RegexValidator(
|
2022-03-28 15:20:19 +02:00
|
|
|
regex=r'^([0-9A-Za-z_-]+|\*)(\.[0-9A-Za-z_-]+)*\.?$',
|
2024-02-20 06:44:02 -08:00
|
|
|
message=_('Only alphanumeric characters, asterisks, hyphens, periods, and underscores are allowed in DNS names'),
|
2019-04-22 18:10:28 -04:00
|
|
|
code='invalid'
|
|
|
|
)
|