Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
Gutyán Gábor
/
circlestack
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
39be77a3
authored
Oct 08, 2013
by
Bach Dániel
Committed by
Bach Dániel
Oct 14, 2013
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
firewall: add IPAddressField
parent
57bd312c
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
83 additions
and
19 deletions
+83
-19
circle/firewall/fields.py
+71
-2
circle/firewall/migrations/0037_auto__chg_field_host_pub_ipv4__chg_field_host_ipv6__chg_field_host_ipv.py
+0
-0
circle/firewall/models.py
+12
-17
No files found.
circle/firewall/fields.py
View file @
39be77a3
...
...
@@ -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
IP
Address
,
IP
Network
,
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
([
"
%03
d"
%
x
for
x
in
value
.
words
]))
else
:
return
(
':'
.
join
([
"
%04
X"
%
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
\
.IP
Network
Field"
])
add_introspection_rules
([],
[
"^firewall
\
.fields
\
.IP
(Address|Network)
Field"
])
def
val_alfanum
(
value
):
...
...
circle/firewall/migrations/0037_auto__chg_field_host_pub_ipv4__chg_field_host_ipv6__chg_field_host_ipv.py
0 → 100644
View file @
39be77a3
This diff is collapsed.
Click to expand it.
circle/firewall/models.py
View file @
39be77a3
...
...
@@ -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
,
verbose_name
=
_
(
'IPv4 address'
),
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
,
ipv4
=
IPAddressField
(
version
=
4
,
unique
=
True
,
verbose_name
=
_
(
'IPv4 address'
),
help_text
=
_
(
'The real IPv4 address of the '
'host, for example 10.5.1.34.'
))
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
,
blank
=
True
,
null
=
True
,
verbose_name
=
_
(
'IPv6 address'
),
help_text
=
_
(
'The global IPv6 address of the '
'host, for example '
'2001:500:88:200::10.'
))
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:db:88:200::10.'
))
shared_ip
=
models
.
BooleanField
(
default
=
False
,
verbose_name
=
_
(
'shared IP'
),
help_text
=
_
(
'If the given WAN IPv4 address is '
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment