Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
Gelencsér Szabolcs
/
cloud
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
bd2cb60b
authored
May 16, 2013
by
Bence Dányi
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
firewall_gui: Some comments
parent
dd20a906
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
14 additions
and
3 deletions
+14
-3
firewall_gui/views.py
+14
-3
No files found.
firewall_gui/views.py
View file @
bd2cb60b
...
@@ -7,12 +7,14 @@ from firewall.fw import *
...
@@ -7,12 +7,14 @@ from firewall.fw import *
from
firewall.models
import
*
from
firewall.models
import
*
def
req_staff
(
user
):
def
req_staff
(
user
):
''' decorator function for user permission checking '''
return
user
.
is_staff
return
user
.
is_staff
def
index
(
request
):
def
index
(
request
):
return
render
(
request
,
'firewall/index.html'
)
return
render
(
request
,
'firewall/index.html'
)
def
map_rule_target
(
rule
):
def
map_rule_target
(
rule
):
''' get the actual target from rule field (vlan|vlangroup|host|hostgroup|firewall) '''
return
{
return
{
'name'
:
rule
.
vlan
.
name
,
'name'
:
rule
.
vlan
.
name
,
'id'
:
rule
.
vlan
.
id
,
'id'
:
rule
.
vlan
.
id
,
...
@@ -36,37 +38,46 @@ def map_rule_target(rule):
...
@@ -36,37 +38,46 @@ def map_rule_target(rule):
}
}
def
json_attr
(
entity
,
attr
):
def
json_attr
(
entity
,
attr
):
''' jsonify the `attr` attribute of `entity` '''
# an objects name usually is in the `name` attribute, but not always, so put here the exceptions
common_names
=
{
common_names
=
{
'host'
:
'hostname'
,
'host'
:
'hostname'
,
'owner'
:
'username'
,
'owner'
:
'username'
,}
}
try
:
try
:
return
{
return
{
# if `attr` is an entity, parse its name&id
'ForeignKey'
:
lambda
entity
:
{
'ForeignKey'
:
lambda
entity
:
{
'id'
:
entity
.
id
,
'id'
:
entity
.
id
,
'name'
:
getattr
(
entity
,
common_names
[
attr
]
if
attr
in
common_names
.
keys
()
else
'name'
)
'name'
:
getattr
(
entity
,
common_names
[
attr
]
if
attr
in
common_names
.
keys
()
else
'name'
)
}
if
entity
else
None
,
}
if
entity
else
None
,
# if `attr` is a date, format it with isoformat
'DateTimeField'
:
lambda
entity
:
entity
.
isoformat
(),
'DateTimeField'
:
lambda
entity
:
entity
.
isoformat
(),
# if `attr` is a Crazy ManyToMany field, fetch all objects, and get their name&id
'ManyToManyField'
:
lambda
field
:
[{
'ManyToManyField'
:
lambda
field
:
[{
'id'
:
entity
.
id
,
'id'
:
entity
.
id
,
'name'
:
getattr
(
entity
,
common_names
[
attr
]
if
attr
in
common_names
.
keys
()
else
'name'
)
'name'
:
getattr
(
entity
,
common_names
[
attr
]
if
attr
in
common_names
.
keys
()
else
'name'
)
}
for
entity
in
field
.
all
()]
}
for
entity
in
field
.
all
()]
}[
entity
.
_meta
.
get_field_by_name
(
attr
)[
0
]
.
__class__
.
__name__
](
getattr
(
entity
,
attr
))
}[
entity
.
_meta
.
get_field_by_name
(
attr
)[
0
]
.
__class__
.
__name__
](
getattr
(
entity
,
attr
))
except
Exception
as
e
:
except
Exception
as
e
:
return
getattr
(
entity
,
attr
)
# if `attr` is something else, we hope it can be converted to JSON
return
getattr
(
entity
,
attr
)
return
getattr
(
entity
,
attr
)
def
make_entity_lister
(
entity_type
,
mapping
):
def
make_entity_lister
(
entity_type
,
mapping
):
''' makes a function that lists the given entities '''
def
jsonify
(
entity
):
def
jsonify
(
entity
):
''' jsonify one entity '''
result
=
{}
result
=
{}
for
attr
in
mapping
:
for
attr
in
mapping
:
# if `attr` is a tuple, the first element is the key in the JSON, the second is a lambda function that calculates the value
if
type
(
attr
)
is
tuple
:
if
type
(
attr
)
is
tuple
:
result
[
attr
[
0
]]
=
attr
[
1
](
entity
)
result
[
attr
[
0
]]
=
attr
[
1
](
entity
)
else
:
else
:
# if `attr` is just a string, the try to jsonify it
result
[
attr
]
=
json_attr
(
entity
,
attr
)
result
[
attr
]
=
json_attr
(
entity
,
attr
)
return
result
return
result
def
entity_lister
(
request
):
def
entity_lister
(
request
):
''' jsonify all objects of the given model type '''
return
[
jsonify
(
entity
)
for
entity
in
entity_type
.
objects
.
all
()]
return
[
jsonify
(
entity
)
for
entity
in
entity_type
.
objects
.
all
()]
return
entity_lister
return
entity_lister
...
...
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