Commit 39be77a3 by Bach Dániel Committed by Bach Dániel

firewall: add IPAddressField

parent 57bd312c
......@@ -5,7 +5,7 @@ from django.utils.translation import ugettext_lazy as _
from django.utils.ipv6 import is_valid_ipv6_address
from south.modelsinspector import add_introspection_rules
from django import forms
from netaddr import IPNetwork, AddrFormatError
from netaddr import IPAddress, IPNetwork, AddrFormatError
import re
......@@ -42,6 +42,75 @@ class MACAddressField(models.Field):
add_introspection_rules([], ["firewall\.fields\.MACAddressField"])
class IPAddressFormField(forms.Field):
default_error_messages = {
'invalid': _(u'Enter a valid IP address. %s'),
}
def validate(self, value):
try:
return IPAddressField.from_str(value, version=self.version)
except (AddrFormatError, TypeError), e:
raise ValidationError(self.default_error_messages['invalid']
% unicode(e))
def __init__(self, *args, **kwargs):
self.version = kwargs['version']
del kwargs['version']
super(IPAddressFormField, self).__init__(*args, **kwargs)
class IPAddressField(models.Field):
description = _('IP Network object')
__metaclass__ = models.SubfieldBase
def __init__(self, version=4, serialize=True, *args, **kwargs):
kwargs['max_length'] = 100
self.version = version
super(IPAddressField, self).__init__(*args, **kwargs)
@staticmethod
def from_str(value, version):
if not value or value == "":
return None
if isinstance(value, IPAddress):
return value
return IPAddress(value, version=version)
def get_internal_type(self):
return "CharField"
def to_python(self, value):
return IPAddressField.from_str(value, self.version)
def get_db_prep_value(self, value, connection, prepared=False):
if not value or value == "":
return None
if isinstance(value, IPAddress):
if self.version == 4:
return ('.'.join(["%03d" % x for x in value.words]))
else:
return (':'.join(["%04X" % x for x in value.words]))
return value
def value_to_string(self, obj):
value = self._get_val_from_obj(obj)
return str(self.get_prep_value(value))
def clean(self, value, model_instance):
value = super(IPAddressField, self).clean(value, model_instance)
return self.get_prep_value(value)
def formfield(self, **kwargs):
defaults = {'form_class': IPAddressFormField}
defaults['version'] = self.version
defaults.update(kwargs)
return super(IPAddressField, self).formfield(**defaults)
class IPNetworkFormField(forms.Field):
default_error_messages = {
'invalid': _(u'Enter a valid IP network. %s'),
......@@ -112,7 +181,7 @@ class IPNetworkField(models.Field):
defaults.update(kwargs)
return super(IPNetworkField, self).formfield(**defaults)
add_introspection_rules([], ["^firewall\.fields\.IPNetworkField"])
add_introspection_rules([], ["^firewall\.fields\.IP(Address|Network)Field"])
def val_alfanum(value):
......
......@@ -6,7 +6,7 @@ from django.forms import ValidationError
from django.utils.translation import ugettext_lazy as _
from firewall.fields import (MACAddressField, val_alfanum, val_reverse_domain,
val_domain, val_ipv4, val_ipv6, val_mx,
ipv4_2_ipv6, IPNetworkField)
ipv4_2_ipv6, IPNetworkField, IPAddressField)
from django.core.validators import MinValueValidator, MaxValueValidator
import django.conf
from django.db.models.signals import post_save, post_delete
......@@ -182,8 +182,6 @@ class Vlan(models.Model):
'For example vlan0004 or eth2.'))
network4 = IPNetworkField(unique=False,
version=4,
null=True,
blank=True,
verbose_name=_('IPv4 address/prefix'),
help_text=_(
'The IPv4 address and the prefix length '
......@@ -374,23 +372,20 @@ class Host(models.Model):
help_text=_('The MAC (Ethernet) address of the '
'network interface. For example: '
'99:AA:BB:CC:DD:EE.'))
ipv4 = models.GenericIPAddressField(protocol='ipv4', unique=True,
ipv4 = IPAddressField(version=4, unique=True,
verbose_name=_('IPv4 address'),
help_text=_(
'The real IPv4 address of the '
help_text=_('The real IPv4 address of the '
'host, for example 10.5.1.34.'))
pub_ipv4 = models.GenericIPAddressField(
protocol='ipv4', blank=True, null=True,
pub_ipv4 = IPAddressField(
version=4, blank=True, null=True,
verbose_name=_('WAN IPv4 address'),
help_text=_('The public IPv4 address of the host on the wide '
'area network, if different.'))
ipv6 = models.GenericIPAddressField(protocol='ipv6', unique=True,
ipv6 = IPAddressField(version=6, unique=True,
blank=True, null=True,
verbose_name=_('IPv6 address'),
help_text=_(
'The global IPv6 address of the '
'host, for example '
'2001:500:88:200::10.'))
help_text=_('The global IPv6 address of the host'
', for example 2001:db:88:200::10.'))
shared_ip = models.BooleanField(default=False, verbose_name=_('shared IP'),
help_text=_(
'If the given WAN IPv4 address is '
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment