mirror of
https://github.com/peeringdb/peeringdb.git
synced 2024-05-11 05:55:09 +00:00
initial commit of code
This commit is contained in:
51
peeringdb_server/validators.py
Normal file
51
peeringdb_server/validators.py
Normal file
@@ -0,0 +1,51 @@
|
||||
"""
|
||||
peeringdb model / field validators
|
||||
"""
|
||||
|
||||
import ipaddress
|
||||
|
||||
from django.conf import settings
|
||||
from django.core.exceptions import ValidationError
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
from peeringdb_server.inet import network_is_pdb_valid
|
||||
|
||||
|
||||
def validate_address_space(prefix):
|
||||
"""
|
||||
validate an ipaddress according to peeringdb specs
|
||||
|
||||
Arguments:
|
||||
- prefix: ipaddress.IPv4Network or an ipaddress.IPv6Network
|
||||
|
||||
Raises:
|
||||
- ValidationError on failed validation
|
||||
"""
|
||||
|
||||
if isinstance(prefix, unicode):
|
||||
try:
|
||||
prefix = ipaddress.ip_network(prefix)
|
||||
except ValueError as exc:
|
||||
raise ValidationError(_("Invalid prefix: {}").format(prefix))
|
||||
|
||||
if not network_is_pdb_valid(prefix):
|
||||
raise ValidationError(_("Address space invalid: {}").format(prefix))
|
||||
|
||||
|
||||
def validate_info_prefixes4(value):
|
||||
if value > settings.DATA_QUALITY_MAX_PREFIX_V4_LIMIT:
|
||||
raise ValidationError(
|
||||
_("Maximum value allowed {}").format(
|
||||
settings.DATA_QUALITY_MAX_PREFIX_V4_LIMIT))
|
||||
if value < 0:
|
||||
raise ValidationError(_("Negative value not allowed"))
|
||||
|
||||
|
||||
def validate_info_prefixes6(value):
|
||||
if value > settings.DATA_QUALITY_MAX_PREFIX_V6_LIMIT:
|
||||
raise ValidationError(
|
||||
_("Maximum value allowed {}").format(
|
||||
settings.DATA_QUALITY_MAX_PREFIX_V6_LIMIT))
|
||||
|
||||
if value < 0:
|
||||
raise ValidationError(_("Negative value not allowed"))
|
Reference in New Issue
Block a user