Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
J
jumpserver
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
ops
jumpserver
Commits
a3f1622a
Unverified
Commit
a3f1622a
authored
May 31, 2018
by
老广
Committed by
GitHub
May 31, 2018
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #1371 from jumpserver/fix_asset_count
[Update] 修改资产树节点(数量)总显示此节点以及子孙节点下的所有资产总数
parents
0021f2e5
e9d0104a
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
46 additions
and
62 deletions
+46
-62
node.py
apps/assets/api/node.py
+30
-39
node.py
apps/assets/models/node.py
+11
-11
node.py
apps/assets/serializers/node.py
+1
-7
_asset_list_modal.html
apps/assets/templates/assets/_asset_list_modal.html
+1
-1
asset_list.html
apps/assets/templates/assets/asset_list.html
+3
-4
No files found.
apps/assets/api/node.py
View file @
a3f1622a
...
@@ -31,7 +31,7 @@ from .. import serializers
...
@@ -31,7 +31,7 @@ from .. import serializers
logger
=
get_logger
(
__file__
)
logger
=
get_logger
(
__file__
)
__all__
=
[
__all__
=
[
'NodeViewSet'
,
'NodeChildrenApi'
,
'NodeViewSet'
,
'NodeChildrenApi'
,
'NodeAssetsApi'
,
'NodeWithAssetsApi'
,
'NodeAssetsApi'
,
'NodeAddAssetsApi'
,
'NodeRemoveAssetsApi'
,
'NodeAddAssetsApi'
,
'NodeRemoveAssetsApi'
,
'NodeReplaceAssetsApi'
,
'NodeReplaceAssetsApi'
,
'NodeAddChildrenApi'
,
'RefreshNodeHardwareInfoApi'
,
'NodeAddChildrenApi'
,
'RefreshNodeHardwareInfoApi'
,
...
@@ -42,14 +42,7 @@ __all__ = [
...
@@ -42,14 +42,7 @@ __all__ = [
class
NodeViewSet
(
BulkModelViewSet
):
class
NodeViewSet
(
BulkModelViewSet
):
queryset
=
Node
.
objects
.
all
()
queryset
=
Node
.
objects
.
all
()
permission_classes
=
(
IsSuperUser
,)
permission_classes
=
(
IsSuperUser
,)
# serializer_class = serializers.NodeSerializer
serializer_class
=
serializers
.
NodeSerializer
def
get_serializer_class
(
self
):
show_current_asset
=
self
.
request
.
query_params
.
get
(
'show_current_asset'
)
if
show_current_asset
:
return
serializers
.
NodeCurrentSerializer
else
:
return
serializers
.
NodeSerializer
def
perform_create
(
self
,
serializer
):
def
perform_create
(
self
,
serializer
):
child_key
=
Node
.
root
()
.
get_next_child_key
()
child_key
=
Node
.
root
()
.
get_next_child_key
()
...
@@ -57,32 +50,32 @@ class NodeViewSet(BulkModelViewSet):
...
@@ -57,32 +50,32 @@ class NodeViewSet(BulkModelViewSet):
serializer
.
save
()
serializer
.
save
()
class
NodeWithAssetsApi
(
generics
.
ListAPIView
):
#
class NodeWithAssetsApi(generics.ListAPIView):
permission_classes
=
(
IsSuperUser
,)
#
permission_classes = (IsSuperUser,)
serializers
=
serializers
.
NodeSerializer
#
serializers = serializers.NodeSerializer
#
def
get_node
(
self
):
#
def get_node(self):
pk
=
self
.
kwargs
.
get
(
'pk'
)
or
self
.
request
.
query_params
.
get
(
'node'
)
#
pk = self.kwargs.get('pk') or self.request.query_params.get('node')
if
not
pk
:
#
if not pk:
node
=
Node
.
root
()
#
node = Node.root()
else
:
#
else:
node
=
get_object_or_404
(
Node
,
pk
)
#
node = get_object_or_404(Node, pk)
return
node
#
return node
#
def
get_queryset
(
self
):
#
def get_queryset(self):
queryset
=
[]
#
queryset = []
node
=
self
.
get_node
()
#
node = self.get_node()
children
=
node
.
get_children
()
#
children = node.get_children()
assets
=
node
.
get_assets
()
#
assets = node.get_assets()
queryset
.
extend
(
list
(
children
))
#
queryset.extend(list(children))
#
for
asset
in
assets
:
#
for asset in assets:
node
=
Node
()
#
node = Node()
node
.
id
=
asset
.
id
#
node.id = asset.id
node
.
parent
=
node
.
id
#
node.parent = node.id
node
.
value
=
asset
.
hostname
#
node.value = asset.hostname
queryset
.
append
(
node
)
#
queryset.append(node)
return
queryset
#
return queryset
class
NodeChildrenApi
(
mixins
.
ListModelMixin
,
generics
.
CreateAPIView
):
class
NodeChildrenApi
(
mixins
.
ListModelMixin
,
generics
.
CreateAPIView
):
...
@@ -146,9 +139,9 @@ class NodeChildrenApi(mixins.ListModelMixin, generics.CreateAPIView):
...
@@ -146,9 +139,9 @@ class NodeChildrenApi(mixins.ListModelMixin, generics.CreateAPIView):
for
asset
in
assets
:
for
asset
in
assets
:
node_fake
=
Node
()
node_fake
=
Node
()
node_fake
.
id
=
asset
.
id
node_fake
.
id
=
asset
.
id
node_fake
.
is_node
=
False
node_fake
.
parent
=
node
node_fake
.
parent
=
node
node_fake
.
value
=
asset
.
hostname
node_fake
.
value
=
asset
.
hostname
node_fake
.
is_node
=
False
queryset
.
append
(
node_fake
)
queryset
.
append
(
node_fake
)
queryset
=
sorted
(
queryset
,
key
=
lambda
x
:
x
.
is_node
,
reverse
=
True
)
queryset
=
sorted
(
queryset
,
key
=
lambda
x
:
x
.
is_node
,
reverse
=
True
)
return
queryset
return
queryset
...
@@ -184,9 +177,7 @@ class NodeAddChildrenApi(generics.UpdateAPIView):
...
@@ -184,9 +177,7 @@ class NodeAddChildrenApi(generics.UpdateAPIView):
for
node
in
children
:
for
node
in
children
:
if
not
node
:
if
not
node
:
continue
continue
# node.parent = instance
node
.
parent
=
instance
# node.save()
node
.
set_parent
(
instance
)
return
Response
(
"OK"
)
return
Response
(
"OK"
)
...
...
apps/assets/models/node.py
View file @
a3f1622a
...
@@ -38,16 +38,6 @@ class Node(models.Model):
...
@@ -38,16 +38,6 @@ class Node(models.Model):
def
level
(
self
):
def
level
(
self
):
return
len
(
self
.
key
.
split
(
':'
))
return
len
(
self
.
key
.
split
(
':'
))
def
set_parent
(
self
,
instance
):
children
=
self
.
get_all_children
()
old_key
=
self
.
key
with
transaction
.
atomic
():
self
.
parent
=
instance
for
child
in
children
:
child
.
key
=
child
.
key
.
replace
(
old_key
,
self
.
key
,
1
)
child
.
save
()
self
.
save
()
def
get_next_child_key
(
self
):
def
get_next_child_key
(
self
):
mark
=
self
.
child_mark
mark
=
self
.
child_mark
self
.
child_mark
+=
1
self
.
child_mark
+=
1
...
@@ -125,7 +115,17 @@ class Node(models.Model):
...
@@ -125,7 +115,17 @@ class Node(models.Model):
@parent.setter
@parent.setter
def
parent
(
self
,
parent
):
def
parent
(
self
,
parent
):
self
.
key
=
parent
.
get_next_child_key
()
if
self
.
is_node
:
children
=
self
.
get_all_children
()
old_key
=
self
.
key
with
transaction
.
atomic
():
self
.
key
=
parent
.
get_next_child_key
()
for
child
in
children
:
child
.
key
=
child
.
key
.
replace
(
old_key
,
self
.
key
,
1
)
child
.
save
()
self
.
save
()
else
:
self
.
key
=
parent
.
key
+
':fake'
@property
@property
def
ancestor
(
self
):
def
ancestor
(
self
):
...
...
apps/assets/serializers/node.py
View file @
a3f1622a
...
@@ -9,7 +9,7 @@ from .asset import AssetGrantedSerializer
...
@@ -9,7 +9,7 @@ from .asset import AssetGrantedSerializer
__all__
=
[
__all__
=
[
'NodeSerializer'
,
"NodeGrantedSerializer"
,
"NodeAddChildrenSerializer"
,
'NodeSerializer'
,
"NodeGrantedSerializer"
,
"NodeAddChildrenSerializer"
,
"NodeAssetsSerializer"
,
"NodeCurrentSerializer"
,
"NodeAssetsSerializer"
,
]
]
...
@@ -77,12 +77,6 @@ class NodeSerializer(serializers.ModelSerializer):
...
@@ -77,12 +77,6 @@ class NodeSerializer(serializers.ModelSerializer):
return
fields
return
fields
class
NodeCurrentSerializer
(
NodeSerializer
):
@staticmethod
def
get_assets_amount
(
obj
):
return
obj
.
get_assets
()
.
count
()
class
NodeAssetsSerializer
(
serializers
.
ModelSerializer
):
class
NodeAssetsSerializer
(
serializers
.
ModelSerializer
):
assets
=
serializers
.
PrimaryKeyRelatedField
(
many
=
True
,
queryset
=
Asset
.
objects
.
all
())
assets
=
serializers
.
PrimaryKeyRelatedField
(
many
=
True
,
queryset
=
Asset
.
objects
.
all
())
...
...
apps/assets/templates/assets/_asset_list_modal.html
View file @
a3f1622a
...
@@ -95,7 +95,7 @@ function initTree2() {
...
@@ -95,7 +95,7 @@ function initTree2() {
};
};
var
zNodes
=
[];
var
zNodes
=
[];
$
.
get
(
"{% url 'api-assets:node-list' %}
?show_current_asset=1
"
,
function
(
data
,
status
){
$
.
get
(
"{% url 'api-assets:node-list' %}"
,
function
(
data
,
status
){
$
.
each
(
data
,
function
(
index
,
value
)
{
$
.
each
(
data
,
function
(
index
,
value
)
{
value
[
"pId"
]
=
value
[
"parent"
];
value
[
"pId"
]
=
value
[
"parent"
];
{
#
value
[
"open"
]
=
true
;
#
}
{
#
value
[
"open"
]
=
true
;
#
}
...
...
apps/assets/templates/assets/asset_list.html
View file @
a3f1622a
...
@@ -399,8 +399,7 @@ function initTree() {
...
@@ -399,8 +399,7 @@ function initTree() {
};
};
var
zNodes
=
[];
var
zNodes
=
[];
var
query_params
=
{
'show_current_asset'
:
getCookie
(
'show_current_asset'
)};
$
.
get
(
"{% url 'api-assets:node-list' %}"
,
function
(
data
,
status
){
$
.
get
(
"{% url 'api-assets:node-list' %}"
,
query_params
,
function
(
data
,
status
){
$
.
each
(
data
,
function
(
index
,
value
)
{
$
.
each
(
data
,
function
(
index
,
value
)
{
value
[
"pId"
]
=
value
[
"parent"
];
value
[
"pId"
]
=
value
[
"parent"
];
if
(
value
[
"key"
]
===
"0"
)
{
if
(
value
[
"key"
]
===
"0"
)
{
...
@@ -436,7 +435,7 @@ $(document).ready(function(){
...
@@ -436,7 +435,7 @@ $(document).ready(function(){
initTable
();
initTable
();
initTree
();
initTree
();
if
(
getCookie
(
'show_current_asset'
)
===
'
yes
'
){
if
(
getCookie
(
'show_current_asset'
)
===
'
1
'
){
$
(
'#show_all_asset'
).
css
(
'display'
,
'inline-block'
);
$
(
'#show_all_asset'
).
css
(
'display'
,
'inline-block'
);
}
}
else
{
else
{
...
@@ -564,7 +563,7 @@ $(document).ready(function(){
...
@@ -564,7 +563,7 @@ $(document).ready(function(){
hideRMenu
();
hideRMenu
();
$
(
this
).
css
(
'display'
,
'none'
);
$
(
this
).
css
(
'display'
,
'none'
);
$
(
'#show_all_asset'
).
css
(
'display'
,
'inline-block'
);
$
(
'#show_all_asset'
).
css
(
'display'
,
'inline-block'
);
setCookie
(
'show_current_asset'
,
'
yes
'
);
setCookie
(
'show_current_asset'
,
'
1
'
);
location
.
reload
();
location
.
reload
();
})
})
.
on
(
'click'
,
'.btn-show-all-asset'
,
function
(){
.
on
(
'click'
,
'.btn-show-all-asset'
,
function
(){
...
...
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