1
0
mirror of https://github.com/netbox-community/netbox.git synced 2024-05-10 07:54:54 +00:00

32 lines
858 B
Python

from __future__ import unicode_literals
from django import forms
from django.core.exceptions import ValidationError
from netaddr import IPNetwork, AddrFormatError
#
# Form fields
#
class IPFormField(forms.Field):
default_error_messages = {
'invalid': "Enter a valid IPv4 or IPv6 address (with CIDR mask).",
}
def to_python(self, value):
if not value:
return None
if isinstance(value, IPNetwork):
return value
# Ensure that a subnet mask has been specified. This prevents IPs from defaulting to a /32 or /128.
if len(value.split('/')) != 2:
raise ValidationError('CIDR mask (e.g. /24) is required.')
try:
return IPNetwork(value)
except AddrFormatError:
raise ValidationError("Please specify a valid IPv4 or IPv6 address.")