diff --git a/apps/assets/api/node.py b/apps/assets/api/node.py index 32b6c33bb..0788db3cd 100644 --- a/apps/assets/api/node.py +++ b/apps/assets/api/node.py @@ -112,12 +112,16 @@ class NodeChildrenAsTreeApi(generics.ListAPIView): is_root = False def get_queryset(self): + self.check_need_refresh_nodes() node_key = self.request.query_params.get('key') util = NodeUtil() + # 是否包含自己 + with_self = False if not node_key: node_key = Node.root().key + with_self = True self.node = util.get_node_by_key(node_key) - queryset = self.node.get_children(with_self=True) + queryset = self.node.get_children(with_self=with_self) queryset = [node.as_tree_node() for node in queryset] queryset = sorted(queryset) return queryset @@ -133,14 +137,11 @@ class NodeChildrenAsTreeApi(generics.ListAPIView): def filter_queryset(self, queryset): queryset = self.filter_assets(queryset) - queryset = self.filter_refresh_nodes(queryset) return queryset - def filter_refresh_nodes(self, queryset): + def check_need_refresh_nodes(self): if self.request.query_params.get('refresh', '0') == '1': - Node.expire_nodes_assets_amount() - Node.expire_nodes_full_value() - return queryset + Node.refresh_nodes() class NodeChildrenApi(mixins.ListModelMixin, generics.CreateAPIView): diff --git a/apps/assets/forms/asset.py b/apps/assets/forms/asset.py index 624b6c818..8f6ffab18 100644 --- a/apps/assets/forms/asset.py +++ b/apps/assets/forms/asset.py @@ -6,7 +6,7 @@ from django.utils.translation import gettext_lazy as _ from common.utils import get_logger from orgs.mixins import OrgModelForm -from ..models import Asset, Protocol +from ..models import Asset, Protocol, Node logger = get_logger(__file__) @@ -33,6 +33,12 @@ class ProtocolForm(forms.ModelForm): class AssetCreateForm(OrgModelForm): PROTOCOL_CHOICES = Protocol.PROTOCOL_CHOICES + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + if not self.data: + nodes_field = self.fields['nodes'] + nodes_field._queryset = Node.get_queryset() + class Meta: model = Asset fields = [ diff --git a/apps/assets/models/node.py b/apps/assets/models/node.py index 5ef664c85..ace1237cc 100644 --- a/apps/assets/models/node.py +++ b/apps/assets/models/node.py @@ -186,9 +186,6 @@ class FullValueMixin: def expire_nodes_full_value(cls, nodes=None): key = cls._full_value_cache_key.format('*') cache.delete_pattern(key+'*') - from ..utils import NodeUtil - util = NodeUtil() - util.set_full_value() class AssetsAmountMixin: @@ -216,7 +213,7 @@ class AssetsAmountMixin: def assets_amount(self, value): self._assets_amount = value cache_key = self._assets_amount_cache_key.format(self.key) - cache.set(cache_key, value, 3600 * 24) + cache.set(cache_key, value) def expire_assets_amount(self): ancestor_keys = self.get_ancestor_keys(with_self=True) @@ -226,11 +223,15 @@ class AssetsAmountMixin: @classmethod def expire_nodes_assets_amount(cls, nodes=None): - from ..utils import NodeUtil key = cls._assets_amount_cache_key.format('*') cache.delete_pattern(key) + + @classmethod + def refresh_nodes(cls): + from ..utils import NodeUtil util = NodeUtil(with_assets_amount=True) util.set_assets_amount() + util.set_full_value() class Node(OrgModelMixin, FamilyMixin, FullValueMixin, AssetsAmountMixin): @@ -375,9 +376,7 @@ class Node(OrgModelMixin, FamilyMixin, FullValueMixin, AssetsAmountMixin): def as_tree_node(self): from common.tree import TreeNode - from ..serializers import NodeSerializer name = '{} ({})'.format(self.value, self.assets_amount) - node_serializer = NodeSerializer(instance=self) data = { 'id': self.key, 'name': name, @@ -386,7 +385,12 @@ class Node(OrgModelMixin, FamilyMixin, FullValueMixin, AssetsAmountMixin): 'isParent': True, 'open': self.is_root(), 'meta': { - 'node': node_serializer.data, + 'node': { + "id": self.id, + "name": self.name, + "value": self.value, + "key": self.key, + }, 'type': 'node' } } diff --git a/apps/assets/serializers/node.py b/apps/assets/serializers/node.py index 61df12f64..b33f22116 100644 --- a/apps/assets/serializers/node.py +++ b/apps/assets/serializers/node.py @@ -1,5 +1,6 @@ # -*- coding: utf-8 -*- from rest_framework import serializers +from django.utils.translation import ugettext as _ from orgs.mixins import BulkOrgResourceModelSerializer from ..models import Asset, Node @@ -25,11 +26,11 @@ class NodeSerializer(BulkOrgResourceModelSerializer): def validate_value(self, data): instance = self.instance if self.instance else Node.root() - children = instance.parent.get_children().exclude(key=instance.key) - values = [child.value for child in children] - if data in values: + children = instance.parent.get_children() + children_values = [node.value for node in children if node != instance] + if data in children_values: raise serializers.ValidationError( - 'The same level node name cannot be the same' + _('The same level node name cannot be the same') ) return data diff --git a/apps/assets/templates/assets/_asset_list_modal.html b/apps/assets/templates/assets/_asset_list_modal.html index 4abfa0587..beafa9ae1 100644 --- a/apps/assets/templates/assets/_asset_list_modal.html +++ b/apps/assets/templates/assets/_asset_list_modal.html @@ -67,6 +67,7 @@ function initTable2() { columns: [ {data: "id"}, {data: "hostname" }, {data: "ip" } ], + lengthMenu: [[10, 25, 50], [10, 25, 50]], pageLength: 10 }; asset_table2 = jumpserver.initServerSideDataTable(options); diff --git a/apps/assets/templates/assets/_node_tree.html b/apps/assets/templates/assets/_node_tree.html new file mode 100644 index 000000000..3f6e51596 --- /dev/null +++ b/apps/assets/templates/assets/_node_tree.html @@ -0,0 +1,358 @@ +{% load static %} +{% load i18n %} + +{# #} + + + + +
+
+
+
+
+
+
+
+
+ +
+ +
+ \ No newline at end of file diff --git a/apps/assets/templates/assets/asset_list.html b/apps/assets/templates/assets/asset_list.html index 8ad19600c..3b9df3373 100644 --- a/apps/assets/templates/assets/asset_list.html +++ b/apps/assets/templates/assets/asset_list.html @@ -49,15 +49,7 @@
-
-
-
-
-
-
-
-
-
+ {% include 'assets/_node_tree.html' %}
@@ -132,26 +124,7 @@
- +{% include 'assets/_node_tree.html' %} {% include 'assets/_asset_update_modal.html' %} {% include 'assets/_asset_import_modal.html' %} {% include 'assets/_asset_list_modal.html' %} @@ -159,11 +132,11 @@ {% block custom_foot_js %} {% endblock %} \ No newline at end of file diff --git a/apps/perms/templates/perms/asset_permission_list.html b/apps/perms/templates/perms/asset_permission_list.html index b7b6d4d6f..b8c5b3af9 100644 --- a/apps/perms/templates/perms/asset_permission_list.html +++ b/apps/perms/templates/perms/asset_permission_list.html @@ -24,15 +24,7 @@
-
-
-
-
-
-
-
-
-
+ {% include 'assets/_node_tree.html' %}
@@ -86,7 +78,7 @@