mirror of
https://github.com/netbox-community/netbox.git
synced 2025-08-26 17:26:10 -06:00
#12278 add serializer for ipaddressfield to remove spectacular warnings
This commit is contained in:
parent
4452f57f90
commit
91e546c800
@ -1,4 +1,5 @@
|
||||
from django.contrib.contenttypes.models import ContentType
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
from drf_spectacular.utils import extend_schema_field
|
||||
from rest_framework import serializers
|
||||
|
||||
@ -6,6 +7,8 @@ from dcim.api.nested_serializers import NestedDeviceSerializer, NestedSiteSerial
|
||||
from ipam.choices import *
|
||||
from ipam.constants import IPADDRESS_ASSIGNMENT_MODELS, VLANGROUP_SCOPE_TYPES
|
||||
from ipam.models import *
|
||||
from ipam.validators import validate_ipaddress_with_mask
|
||||
from netaddr import AddrFormatError, IPNetwork
|
||||
from netbox.api.fields import ChoiceField, ContentTypeField, SerializedPKRelatedField
|
||||
from netbox.api.serializers import NetBoxModelSerializer
|
||||
from netbox.constants import NESTED_SERIALIZER_PREFIX
|
||||
@ -387,10 +390,23 @@ class IPRangeSerializer(NetBoxModelSerializer):
|
||||
#
|
||||
# IP addresses
|
||||
#
|
||||
class IPAddressField(serializers.CharField):
|
||||
"""IPAddressField with mask"""
|
||||
|
||||
default_error_messages = {
|
||||
'invalid': _('Enter a valid IPv4 or IPv6 address with optional mask.'),
|
||||
}
|
||||
|
||||
def __init__(self, **kwargs):
|
||||
super().__init__(**kwargs)
|
||||
validator = validate_ipaddress_with_mask
|
||||
self.validators.append(validator)
|
||||
|
||||
|
||||
class IPAddressSerializer(NetBoxModelSerializer):
|
||||
url = serializers.HyperlinkedIdentityField(view_name='ipam-api:ipaddress-detail')
|
||||
family = ChoiceField(choices=IPAddressFamilyChoices, read_only=True)
|
||||
address = IPAddressField()
|
||||
vrf = NestedVRFSerializer(required=False, allow_null=True)
|
||||
tenant = NestedTenantSerializer(required=False, allow_null=True)
|
||||
status = ChoiceField(choices=IPAddressStatusChoices, required=False)
|
||||
|
@ -1,5 +1,6 @@
|
||||
from django.core.exceptions import ValidationError
|
||||
from django.core.validators import BaseValidator, RegexValidator
|
||||
from netaddr import AddrFormatError, IPNetwork
|
||||
|
||||
|
||||
def prefix_validator(prefix):
|
||||
@ -28,3 +29,13 @@ DNSValidator = RegexValidator(
|
||||
message='Only alphanumeric characters, asterisks, hyphens, periods, and underscores are allowed in DNS names',
|
||||
code='invalid'
|
||||
)
|
||||
|
||||
|
||||
def validate_ipaddress_with_mask(address):
|
||||
if address:
|
||||
try:
|
||||
IPNetwork(address)
|
||||
except AddrFormatError:
|
||||
raise ValidationError("Invalid IP address format: {}".format(address))
|
||||
except (TypeError, ValueError) as e:
|
||||
raise ValidationError(e)
|
||||
|
Loading…
Reference in New Issue
Block a user