mirror of
https://github.com/netbox-community/netbox.git
synced 2025-07-21 03:27:21 -06:00
Move prefix_validator() to ipam.validators
This commit is contained in:
parent
ba6df87d10
commit
aa56c020ab
@ -2,15 +2,10 @@ from django.core.exceptions import ValidationError
|
|||||||
from django.db import models
|
from django.db import models
|
||||||
from netaddr import AddrFormatError, IPNetwork
|
from netaddr import AddrFormatError, IPNetwork
|
||||||
|
|
||||||
from . import lookups
|
from . import lookups, validators
|
||||||
from .formfields import IPFormField
|
from .formfields import IPFormField
|
||||||
|
|
||||||
|
|
||||||
def prefix_validator(prefix):
|
|
||||||
if prefix.ip != prefix.cidr.ip:
|
|
||||||
raise ValidationError("{} is not a valid prefix. Did you mean {}?".format(prefix, prefix.cidr))
|
|
||||||
|
|
||||||
|
|
||||||
class BaseIPField(models.Field):
|
class BaseIPField(models.Field):
|
||||||
|
|
||||||
def python_type(self):
|
def python_type(self):
|
||||||
@ -51,7 +46,7 @@ class IPNetworkField(BaseIPField):
|
|||||||
IP prefix (network and mask)
|
IP prefix (network and mask)
|
||||||
"""
|
"""
|
||||||
description = "PostgreSQL CIDR field"
|
description = "PostgreSQL CIDR field"
|
||||||
default_validators = [prefix_validator]
|
default_validators = [validators.prefix_validator]
|
||||||
|
|
||||||
def db_type(self, connection):
|
def db_type(self, connection):
|
||||||
return 'cidr'
|
return 'cidr'
|
||||||
|
@ -1,6 +1,12 @@
|
|||||||
|
from django.core.exceptions import ValidationError
|
||||||
from django.core.validators import BaseValidator, RegexValidator
|
from django.core.validators import BaseValidator, RegexValidator
|
||||||
|
|
||||||
|
|
||||||
|
def prefix_validator(prefix):
|
||||||
|
if prefix.ip != prefix.cidr.ip:
|
||||||
|
raise ValidationError("{} is not a valid prefix. Did you mean {}?".format(prefix, prefix.cidr))
|
||||||
|
|
||||||
|
|
||||||
class MaxPrefixLengthValidator(BaseValidator):
|
class MaxPrefixLengthValidator(BaseValidator):
|
||||||
message = 'The prefix length must be less than or equal to %(limit_value)s.'
|
message = 'The prefix length must be less than or equal to %(limit_value)s.'
|
||||||
code = 'max_prefix_length'
|
code = 'max_prefix_length'
|
||||||
|
Loading…
Reference in New Issue
Block a user