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
8f38c8c5
authored
Jun 04, 2014
by
Bach Dániel
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
firewall: refactor netaddr fields
parent
4aaf5188
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
30 additions
and
66 deletions
+30
-66
circle/firewall/fields.py
+30
-66
No files found.
circle/firewall/fields.py
View file @
8f38c8c5
...
@@ -40,8 +40,8 @@ class MACAddressFormField(forms.Field):
...
@@ -40,8 +40,8 @@ class MACAddressFormField(forms.Field):
def
validate
(
self
,
value
):
def
validate
(
self
,
value
):
try
:
try
:
return
MACAddressField
.
from_str
(
value
)
return
MACAddressField
.
to_python
.
im_func
(
None
,
value
)
except
(
AddrFormatError
,
TypeError
),
e
:
except
(
AddrFormatError
,
TypeError
,
ValidationError
)
as
e
:
raise
ValidationError
(
self
.
default_error_messages
[
'invalid'
]
raise
ValidationError
(
self
.
default_error_messages
[
'invalid'
]
%
unicode
(
e
))
%
unicode
(
e
))
...
@@ -57,9 +57,8 @@ class MACAddressField(models.Field):
...
@@ -57,9 +57,8 @@ class MACAddressField(models.Field):
kwargs
[
'max_length'
]
=
17
kwargs
[
'max_length'
]
=
17
super
(
MACAddressField
,
self
)
.
__init__
(
*
args
,
**
kwargs
)
super
(
MACAddressField
,
self
)
.
__init__
(
*
args
,
**
kwargs
)
@staticmethod
def
to_python
(
self
,
value
):
def
from_str
(
value
):
if
not
value
:
if
not
value
or
value
==
""
:
return
None
return
None
if
isinstance
(
value
,
EUI
):
if
isinstance
(
value
,
EUI
):
...
@@ -70,32 +69,20 @@ class MACAddressField(models.Field):
...
@@ -70,32 +69,20 @@ class MACAddressField(models.Field):
def
get_internal_type
(
self
):
def
get_internal_type
(
self
):
return
'CharField'
return
'CharField'
def
to_python
(
self
,
value
):
def
get_prep_value
(
self
,
value
,
prepared
=
False
):
return
MACAddressField
.
from_str
(
value
)
if
not
value
:
def
get_db_prep_value
(
self
,
value
,
connection
,
prepared
=
False
):
if
not
value
or
value
==
""
:
return
None
return
None
if
isinstance
(
value
,
EUI
):
if
isinstance
(
value
,
EUI
):
return
str
(
value
)
return
str
(
value
)
return
value
def
value_to_string
(
self
,
obj
):
return
value
value
=
self
.
_get_val_from_obj
(
obj
)
return
str
(
self
.
get_prep_value
(
value
))
def
clean
(
self
,
value
,
model_instance
):
value
=
super
(
MACAddressField
,
self
)
.
clean
(
value
,
model_instance
)
return
self
.
get_prep_value
(
value
)
def
formfield
(
self
,
**
kwargs
):
def
formfield
(
self
,
**
kwargs
):
defaults
=
{
'form_class'
:
MACAddressFormField
}
defaults
=
{
'form_class'
:
MACAddressFormField
}
defaults
.
update
(
kwargs
)
defaults
.
update
(
kwargs
)
return
super
(
MACAddressField
,
self
)
.
formfield
(
**
defaults
)
return
super
(
MACAddressField
,
self
)
.
formfield
(
**
defaults
)
add_introspection_rules
([],
[
"firewall
\
.fields
\
.MACAddressField"
])
class
IPAddressFormField
(
forms
.
Field
):
class
IPAddressFormField
(
forms
.
Field
):
default_error_messages
=
{
default_error_messages
=
{
...
@@ -104,8 +91,8 @@ class IPAddressFormField(forms.Field):
...
@@ -104,8 +91,8 @@ class IPAddressFormField(forms.Field):
def
validate
(
self
,
value
):
def
validate
(
self
,
value
):
try
:
try
:
return
IPAddressField
.
from_str
(
value
,
version
=
self
.
version
)
IPAddressField
(
version
=
self
.
version
)
.
to_python
(
value
)
except
(
AddrFormatError
,
TypeError
),
e
:
except
(
AddrFormatError
,
TypeError
,
ValueError
)
as
e
:
raise
ValidationError
(
self
.
default_error_messages
[
'invalid'
]
raise
ValidationError
(
self
.
default_error_messages
[
'invalid'
]
%
unicode
(
e
))
%
unicode
(
e
))
...
@@ -124,42 +111,30 @@ class IPAddressField(models.Field):
...
@@ -124,42 +111,30 @@ class IPAddressField(models.Field):
self
.
version
=
version
self
.
version
=
version
super
(
IPAddressField
,
self
)
.
__init__
(
*
args
,
**
kwargs
)
super
(
IPAddressField
,
self
)
.
__init__
(
*
args
,
**
kwargs
)
@staticmethod
def
get_internal_type
(
self
):
def
from_str
(
value
,
version
):
return
"CharField"
if
not
value
or
value
==
""
:
def
to_python
(
self
,
value
):
if
not
value
:
return
None
return
None
if
isinstance
(
value
,
IPAddress
):
if
isinstance
(
value
,
IPAddress
):
return
value
return
value
return
IPAddress
(
value
.
split
(
'/'
)[
0
],
version
=
version
,
return
IPAddress
(
value
.
split
(
'/'
)[
0
],
version
=
self
.
version
,
flags
=
ZEROFILL
)
flags
=
ZEROFILL
)
def
get_internal_type
(
self
):
def
get_prep_value
(
self
,
value
,
prepared
=
False
):
return
"CharField"
if
not
value
:
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
return
None
if
isinstance
(
value
,
IPAddress
):
if
isinstance
(
value
,
IPAddress
):
if
self
.
version
==
4
:
if
self
.
version
==
4
:
return
(
'.'
.
join
([
"
%03
d"
%
x
for
x
in
value
.
words
])
)
return
'.'
.
join
(
"
%03
d"
%
x
for
x
in
value
.
words
)
else
:
else
:
return
(
':'
.
join
([
"
%04
X"
%
x
for
x
in
value
.
words
])
)
return
':'
.
join
(
"
%04
X"
%
x
for
x
in
value
.
words
)
return
value
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
):
def
formfield
(
self
,
**
kwargs
):
defaults
=
{
'form_class'
:
IPAddressFormField
}
defaults
=
{
'form_class'
:
IPAddressFormField
}
defaults
[
'version'
]
=
self
.
version
defaults
[
'version'
]
=
self
.
version
...
@@ -174,8 +149,8 @@ class IPNetworkFormField(forms.Field):
...
@@ -174,8 +149,8 @@ class IPNetworkFormField(forms.Field):
def
validate
(
self
,
value
):
def
validate
(
self
,
value
):
try
:
try
:
return
IPNetworkField
.
from_str
(
value
,
version
=
self
.
version
)
return
IPNetworkField
(
version
=
self
.
version
)
.
to_python
(
value
)
except
(
AddrFormatError
,
TypeError
)
,
e
:
except
(
AddrFormatError
,
TypeError
)
as
e
:
raise
ValidationError
(
self
.
default_error_messages
[
'invalid'
]
raise
ValidationError
(
self
.
default_error_messages
[
'invalid'
]
%
unicode
(
e
))
%
unicode
(
e
))
...
@@ -194,50 +169,39 @@ class IPNetworkField(models.Field):
...
@@ -194,50 +169,39 @@ class IPNetworkField(models.Field):
self
.
version
=
version
self
.
version
=
version
super
(
IPNetworkField
,
self
)
.
__init__
(
*
args
,
**
kwargs
)
super
(
IPNetworkField
,
self
)
.
__init__
(
*
args
,
**
kwargs
)
@staticmethod
def
to_python
(
self
,
value
):
def
from_str
(
value
,
version
):
if
not
value
:
if
not
value
or
value
==
""
:
return
None
return
None
if
isinstance
(
value
,
IPNetwork
):
if
isinstance
(
value
,
IPNetwork
):
return
value
return
value
return
IPNetwork
(
value
,
version
=
version
)
return
IPNetwork
(
value
,
version
=
self
.
version
)
def
get_internal_type
(
self
):
def
get_internal_type
(
self
):
return
"CharField"
return
"CharField"
def
to_python
(
self
,
value
):
def
get_prep_value
(
self
,
value
,
prepared
=
False
):
return
IPNetworkField
.
from_str
(
value
,
self
.
version
)
if
not
value
:
def
get_db_prep_value
(
self
,
value
,
connection
,
prepared
=
False
):
if
not
value
or
value
==
""
:
return
None
return
None
if
isinstance
(
value
,
IPNetwork
):
if
isinstance
(
value
,
IPNetwork
):
if
self
.
version
==
4
:
if
self
.
version
==
4
:
return
(
'.'
.
join
(
[
"
%03
d"
%
x
for
x
in
value
.
ip
.
words
]
)
return
(
'.'
.
join
(
"
%03
d"
%
x
for
x
in
value
.
ip
.
words
)
+
'/
%02
d'
%
value
.
prefixlen
)
+
'/
%02
d'
%
value
.
prefixlen
)
else
:
else
:
return
(
':'
.
join
(
[
"
%04
X"
%
x
for
x
in
value
.
ip
.
words
]
)
return
(
':'
.
join
(
"
%04
X"
%
x
for
x
in
value
.
ip
.
words
)
+
'/
%03
d'
%
value
.
prefixlen
)
+
'/
%03
d'
%
value
.
prefixlen
)
return
value
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
(
IPNetworkField
,
self
)
.
clean
(
value
,
model_instance
)
return
self
.
get_prep_value
(
value
)
def
formfield
(
self
,
**
kwargs
):
def
formfield
(
self
,
**
kwargs
):
defaults
=
{
'form_class'
:
IPNetworkFormField
}
defaults
=
{
'form_class'
:
IPNetworkFormField
}
defaults
[
'version'
]
=
self
.
version
defaults
[
'version'
]
=
self
.
version
defaults
.
update
(
kwargs
)
defaults
.
update
(
kwargs
)
return
super
(
IPNetworkField
,
self
)
.
formfield
(
**
defaults
)
return
super
(
IPNetworkField
,
self
)
.
formfield
(
**
defaults
)
add_introspection_rules
([],
[
"^firewall
\
.fields
\
.IP(Address|Network)Field"
])
add_introspection_rules
([],
[
"firewall
\
.fields
\
.MACAddressField"
])
def
val_alfanum
(
value
):
def
val_alfanum
(
value
):
...
...
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