Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
CIRCLE
/
cloud
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
94
Merge Requests
10
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
8c89de42
authored
May 29, 2014
by
Bach Dániel
Committed by
Bach Dániel
Jun 04, 2014
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
firewall: add netaddr.EUI based MACAddressField
fixes
#9
parent
8316409d
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
41 additions
and
9 deletions
+41
-9
circle/firewall/fields.py
+41
-9
No files found.
circle/firewall/fields.py
View file @
8c89de42
...
@@ -16,13 +16,13 @@
...
@@ -16,13 +16,13 @@
# with CIRCLE. If not, see <http://www.gnu.org/licenses/>.
# with CIRCLE. If not, see <http://www.gnu.org/licenses/>.
from
django.core.exceptions
import
ValidationError
from
django.core.exceptions
import
ValidationError
from
django.forms
import
fields
from
django.db
import
models
from
django.db
import
models
from
django.utils.translation
import
ugettext_lazy
as
_
from
django.utils.translation
import
ugettext_lazy
as
_
from
django.utils.ipv6
import
is_valid_ipv6_address
from
django.utils.ipv6
import
is_valid_ipv6_address
from
south.modelsinspector
import
add_introspection_rules
from
south.modelsinspector
import
add_introspection_rules
from
django
import
forms
from
django
import
forms
from
netaddr
import
IPAddress
,
IPNetwork
,
AddrFormatError
,
ZEROFILL
from
netaddr
import
(
IPAddress
,
IPNetwork
,
AddrFormatError
,
ZEROFILL
,
EUI
,
mac_unix
)
import
re
import
re
...
@@ -34,35 +34,67 @@ reverse_domain_re = re.compile(r'^(%\([abcd]\)d|[a-z0-9.-])+$')
...
@@ -34,35 +34,67 @@ reverse_domain_re = re.compile(r'^(%\([abcd]\)d|[a-z0-9.-])+$')
ipv6_template_re
=
re
.
compile
(
r'^(
%
\([abcd]\)[dxX]|[A-Za-z0-9:-])+$'
)
ipv6_template_re
=
re
.
compile
(
r'^(
%
\([abcd]\)[dxX]|[A-Za-z0-9:-])+$'
)
class
MACAddressFormField
(
f
ields
.
Regex
Field
):
class
MACAddressFormField
(
f
orms
.
Field
):
default_error_messages
=
{
default_error_messages
=
{
'invalid'
:
_
(
u'Enter a valid MAC address.'
),
'invalid'
:
_
(
u'Enter a valid MAC address.'
),
}
}
def
__init__
(
self
,
*
args
,
**
kwargs
):
def
validate
(
self
,
value
):
super
(
MACAddressFormField
,
self
)
.
__init__
(
mac_re
,
*
args
,
**
kwargs
)
try
:
return
EUI
.
from_str
(
value
)
except
(
AddrFormatError
,
TypeError
),
e
:
raise
ValidationError
(
self
.
default_error_messages
[
'invalid'
]
%
unicode
(
e
))
class
MACAddressField
(
models
.
Field
):
class
MACAddressField
(
models
.
Field
):
empty_strings_allowed
=
False
description
=
_
(
'MAC Address object'
)
__metaclass__
=
models
.
SubfieldBase
class
mac_custom
(
mac_unix
):
word_fmt
=
'
%.2
X'
def
__init__
(
self
,
*
args
,
**
kwargs
):
def
__init__
(
self
,
*
args
,
**
kwargs
):
kwargs
[
'max_length'
]
=
17
kwargs
[
'max_length'
]
=
17
super
(
MACAddressField
,
self
)
.
__init__
(
*
args
,
**
kwargs
)
super
(
MACAddressField
,
self
)
.
__init__
(
*
args
,
**
kwargs
)
@staticmethod
def
from_str
(
value
):
if
not
value
or
value
==
""
:
return
None
if
isinstance
(
value
,
EUI
):
return
value
return
EUI
(
value
,
dialect
=
MACAddressField
.
mac_custom
)
def
get_internal_type
(
self
):
def
get_internal_type
(
self
):
return
'CharField'
return
'CharField'
def
to_python
(
self
,
value
):
def
to_python
(
self
,
value
):
if
not
mac_re
.
search
(
str
(
value
)):
return
MACAddressField
.
from_str
(
value
)
raise
ValidationError
(
MACAddressFormField
.
default_error_messages
[
'invalid'
])
def
get_db_prep_value
(
self
,
value
,
connection
,
prepared
=
False
):
if
not
value
or
value
==
""
:
return
None
if
isinstance
(
value
,
EUI
):
return
str
(
value
)
return
str
(
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
(
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"
])
add_introspection_rules
([],
[
"firewall
\
.fields
\
.MACAddressField"
])
...
...
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