mirror of
https://github.com/netbox-community/netbox.git
synced 2025-08-09 09:08:15 -06:00
Fixes #3509: IPAddressVar for custom scripts
This commit is contained in:
parent
823e1280d2
commit
9a927b9084
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
## Enhancements
|
## Enhancements
|
||||||
|
|
||||||
|
* [#3509](https://github.com/netbox-community/netbox/issues/3509) - Added IPAddressVar for custom scripts
|
||||||
* [#3525](https://github.com/netbox-community/netbox/issues/3525) - Enable IP address filtering with multiple address terms
|
* [#3525](https://github.com/netbox-community/netbox/issues/3525) - Enable IP address filtering with multiple address terms
|
||||||
|
|
||||||
## Bug Fixes
|
## Bug Fixes
|
||||||
|
@ -14,7 +14,7 @@ from django.db import transaction
|
|||||||
from mptt.forms import TreeNodeChoiceField, TreeNodeMultipleChoiceField
|
from mptt.forms import TreeNodeChoiceField, TreeNodeMultipleChoiceField
|
||||||
from mptt.models import MPTTModel
|
from mptt.models import MPTTModel
|
||||||
|
|
||||||
from ipam.formfields import IPFormField
|
from ipam.formfields import IPAddressFormField, IPNetworkFormField
|
||||||
from utilities.exceptions import AbortTransaction
|
from utilities.exceptions import AbortTransaction
|
||||||
from utilities.validators import MaxPrefixLengthValidator, MinPrefixLengthValidator
|
from utilities.validators import MaxPrefixLengthValidator, MinPrefixLengthValidator
|
||||||
from .constants import LOG_DEFAULT, LOG_FAILURE, LOG_INFO, LOG_SUCCESS, LOG_WARNING
|
from .constants import LOG_DEFAULT, LOG_FAILURE, LOG_INFO, LOG_SUCCESS, LOG_WARNING
|
||||||
@ -27,6 +27,7 @@ __all__ = [
|
|||||||
'ChoiceVar',
|
'ChoiceVar',
|
||||||
'FileVar',
|
'FileVar',
|
||||||
'IntegerVar',
|
'IntegerVar',
|
||||||
|
'IPAddressVar',
|
||||||
'IPNetworkVar',
|
'IPNetworkVar',
|
||||||
'MultiObjectVar',
|
'MultiObjectVar',
|
||||||
'ObjectVar',
|
'ObjectVar',
|
||||||
@ -196,11 +197,18 @@ class FileVar(ScriptVariable):
|
|||||||
form_field = forms.FileField
|
form_field = forms.FileField
|
||||||
|
|
||||||
|
|
||||||
|
class IPAddressVar(ScriptVariable):
|
||||||
|
"""
|
||||||
|
An IPv4 or IPv6 address.
|
||||||
|
"""
|
||||||
|
form_field = IPAddressFormField
|
||||||
|
|
||||||
|
|
||||||
class IPNetworkVar(ScriptVariable):
|
class IPNetworkVar(ScriptVariable):
|
||||||
"""
|
"""
|
||||||
An IPv4 or IPv6 prefix.
|
An IPv4 or IPv6 prefix.
|
||||||
"""
|
"""
|
||||||
form_field = IPFormField
|
form_field = IPNetworkFormField
|
||||||
|
|
||||||
def __init__(self, min_prefix_length=None, max_prefix_length=None, *args, **kwargs):
|
def __init__(self, min_prefix_length=None, max_prefix_length=None, *args, **kwargs):
|
||||||
super().__init__(*args, **kwargs)
|
super().__init__(*args, **kwargs)
|
||||||
|
@ -3,7 +3,7 @@ from django.db import models
|
|||||||
from netaddr import AddrFormatError, IPNetwork, IPAddress
|
from netaddr import AddrFormatError, IPNetwork, IPAddress
|
||||||
|
|
||||||
from . import lookups
|
from . import lookups
|
||||||
from .formfields import IPFormField
|
from .formfields import IPNetworkFormField
|
||||||
|
|
||||||
|
|
||||||
def prefix_validator(prefix):
|
def prefix_validator(prefix):
|
||||||
@ -40,7 +40,7 @@ class BaseIPField(models.Field):
|
|||||||
return str(self.to_python(value))
|
return str(self.to_python(value))
|
||||||
|
|
||||||
def form_class(self):
|
def form_class(self):
|
||||||
return IPFormField
|
return IPNetworkFormField
|
||||||
|
|
||||||
def formfield(self, **kwargs):
|
def formfield(self, **kwargs):
|
||||||
defaults = {'form_class': self.form_class()}
|
defaults = {'form_class': self.form_class()}
|
||||||
|
@ -1,13 +1,35 @@
|
|||||||
from django import forms
|
from django import forms
|
||||||
from django.core.exceptions import ValidationError
|
from django.core.exceptions import ValidationError
|
||||||
from netaddr import IPNetwork, AddrFormatError
|
from netaddr import IPAddress, IPNetwork, AddrFormatError
|
||||||
|
|
||||||
|
|
||||||
#
|
#
|
||||||
# Form fields
|
# Form fields
|
||||||
#
|
#
|
||||||
|
|
||||||
class IPFormField(forms.Field):
|
class IPAddressFormField(forms.Field):
|
||||||
|
default_error_messages = {
|
||||||
|
'invalid': "Enter a valid IPv4 or IPv6 address (without CIDR mask).",
|
||||||
|
}
|
||||||
|
|
||||||
|
def to_python(self, value):
|
||||||
|
if not value:
|
||||||
|
return None
|
||||||
|
|
||||||
|
if isinstance(value, IPAddress):
|
||||||
|
return value
|
||||||
|
|
||||||
|
# Prevent CIDR masks; an IPAddress doesn't have one
|
||||||
|
if len(value.split('/')) != 1:
|
||||||
|
raise ValidationError('CIDR mask (e.g. /24) is not allowed.')
|
||||||
|
|
||||||
|
try:
|
||||||
|
return IPAddress(value)
|
||||||
|
except AddrFormatError:
|
||||||
|
raise ValidationError("Please specify a valid IPv4 or IPv6 address.")
|
||||||
|
|
||||||
|
|
||||||
|
class IPNetworkFormField(forms.Field):
|
||||||
default_error_messages = {
|
default_error_messages = {
|
||||||
'invalid': "Enter a valid IPv4 or IPv6 address (with CIDR mask).",
|
'invalid': "Enter a valid IPv4 or IPv6 address (with CIDR mask).",
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user