mirror of
https://github.com/netbox-community/netbox.git
synced 2024-05-10 07:54:54 +00:00
* 15049 add missing gettext to error strings * 15049 add missing gettext to error strings * 15094 review change * 15094 review change * Formatting cleanup --------- Co-authored-by: Jeremy Stretch <jstretch@netboxlabs.com>
36 lines
1.1 KiB
Python
36 lines
1.1 KiB
Python
from django.core.exceptions import ValidationError
|
|
from django.core.validators import BaseValidator, RegexValidator
|
|
from django.utils.translation import gettext_lazy as _
|
|
|
|
|
|
def prefix_validator(prefix):
|
|
if prefix.ip != prefix.cidr.ip:
|
|
raise ValidationError(
|
|
_("{prefix} is not a valid prefix. Did you mean {suggested}?").format(
|
|
prefix=prefix, suggested=prefix.cidr
|
|
)
|
|
)
|
|
|
|
|
|
class MaxPrefixLengthValidator(BaseValidator):
|
|
message = _('The prefix length must be less than or equal to %(limit_value)s.')
|
|
code = 'max_prefix_length'
|
|
|
|
def compare(self, a, b):
|
|
return a.prefixlen > b
|
|
|
|
|
|
class MinPrefixLengthValidator(BaseValidator):
|
|
message = _('The prefix length must be greater than or equal to %(limit_value)s.')
|
|
code = 'min_prefix_length'
|
|
|
|
def compare(self, a, b):
|
|
return a.prefixlen < b
|
|
|
|
|
|
DNSValidator = RegexValidator(
|
|
regex=r'^([0-9A-Za-z_-]+|\*)(\.[0-9A-Za-z_-]+)*\.?$',
|
|
message=_('Only alphanumeric characters, asterisks, hyphens, periods, and underscores are allowed in DNS names'),
|
|
code='invalid'
|
|
)
|