Skip to content

Commit

Permalink
#12278 move IPAddressField to field_serializers
Browse files Browse the repository at this point in the history
  • Loading branch information
arthanson committed Apr 20, 2023
1 parent b967921 commit dc888e0
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 32 deletions.
38 changes: 38 additions & 0 deletions netbox/ipam/api/field_serializers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from django.utils.translation import gettext_lazy as _
from rest_framework import serializers

from ipam import models
from ipam.validators import validate_ipaddress_with_mask
from netaddr import AddrFormatError, IPNetwork

__all__ = [
'IPAddressField',
]


#
# IP address field
#

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)

def to_internal_value(self, data):
try:
return IPNetwork(data)
except AddrFormatError:
raise serializers.ValidationError("Invalid IP address format: {}".format(data))
except (TypeError, ValueError) as e:
raise serializers.ValidationError(e)

def to_representation(self, value):
return str(value)
33 changes: 1 addition & 32 deletions netbox/ipam/api/nested_serializers.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
from django.utils.translation import gettext_lazy as _
from drf_spectacular.utils import extend_schema_serializer
from rest_framework import serializers

from ipam import models
from ipam.models.l2vpn import L2VPNTermination, L2VPN
from ipam.validators import validate_ipaddress_with_mask
from netbox.api.serializers import WritableNestedSerializer
from netaddr import AddrFormatError, IPNetwork
from .field_serializers import IPAddressField

__all__ = [
'IPAddressField',
'NestedAggregateSerializer',
'NestedASNSerializer',
'NestedASNRangeSerializer',
Expand All @@ -31,34 +28,6 @@
]


#
# IP address field
#

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)

def to_internal_value(self, data):
try:
return IPNetwork(data)
except AddrFormatError:
raise serializers.ValidationError("Invalid IP address format: {}".format(data))
except (TypeError, ValueError) as e:
raise serializers.ValidationError(e)

def to_representation(self, value):
return str(value)


#
# ASN ranges
#
Expand Down
1 change: 1 addition & 0 deletions netbox/ipam/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from utilities.api import get_serializer_for_model
from virtualization.api.nested_serializers import NestedVirtualMachineSerializer
from .nested_serializers import *
from .field_serializers import IPAddressField


#
Expand Down

0 comments on commit dc888e0

Please sign in to comment.