Skip to content

Commit

Permalink
#12278 add ipaddressfieldserializer to nested serializers
Browse files Browse the repository at this point in the history
  • Loading branch information
arthanson committed Apr 18, 2023
1 parent 91e546c commit fb1827c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 14 deletions.
23 changes: 23 additions & 0 deletions netbox/ipam/api/nested_serializers.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
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

__all__ = [
'IPAddressField',
'NestedAggregateSerializer',
'NestedASNSerializer',
'NestedASNRangeSerializer',
Expand All @@ -27,6 +30,23 @@
]


#
# 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)


#
# ASN ranges
#
Expand Down Expand Up @@ -182,6 +202,8 @@ class Meta:
class NestedIPRangeSerializer(WritableNestedSerializer):
url = serializers.HyperlinkedIdentityField(view_name='ipam-api:iprange-detail')
family = serializers.IntegerField(read_only=True)
start_address = IPAddressField()
end_address = IPAddressField()

class Meta:
model = models.IPRange
Expand All @@ -195,6 +217,7 @@ class Meta:
class NestedIPAddressSerializer(WritableNestedSerializer):
url = serializers.HyperlinkedIdentityField(view_name='ipam-api:ipaddress-detail')
family = serializers.IntegerField(read_only=True)
address = IPAddressField()

class Meta:
model = models.IPAddress
Expand Down
16 changes: 2 additions & 14 deletions netbox/ipam/api/serializers.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
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

from dcim.api.nested_serializers import NestedDeviceSerializer, NestedSiteSerializer
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
Expand Down Expand Up @@ -372,6 +370,8 @@ def to_representation(self, instance):
class IPRangeSerializer(NetBoxModelSerializer):
url = serializers.HyperlinkedIdentityField(view_name='ipam-api:iprange-detail')
family = ChoiceField(choices=IPAddressFamilyChoices, read_only=True)
start_address = IPAddressField()
end_address = IPAddressField()
vrf = NestedVRFSerializer(required=False, allow_null=True)
tenant = NestedTenantSerializer(required=False, allow_null=True)
status = ChoiceField(choices=IPRangeStatusChoices, required=False)
Expand All @@ -390,18 +390,6 @@ class Meta:
#
# 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')
Expand Down

0 comments on commit fb1827c

Please sign in to comment.