Browse Source

[Update] 优化代码

pull/1370/head
ibuler 7 years ago
parent
commit
7b57d24dc9
  1. 16
      apps/assets/api/asset.py
  2. 4
      apps/assets/models/asset.py
  3. 8
      apps/assets/models/node.py
  4. 2
      apps/assets/serializers/node.py
  5. 2
      apps/assets/templates/assets/_asset_list_modal.html

16
apps/assets/api/asset.py

@ -13,7 +13,7 @@ from common.mixins import IDInFilterMixin
from common.utils import get_logger
from ..hands import IsSuperUser, IsValidUser, IsSuperUserOrAppUser, \
NodePermissionUtil
from ..models import Asset, SystemUser, AdminUser, Node
from ..models import Asset, SystemUser, AdminUser, Node
from .. import serializers
from ..tasks import update_asset_hardware_info_manual, \
test_asset_connectability_manual
@ -48,14 +48,22 @@ class AssetViewSet(IDInFilterMixin, LabelFilter, BulkModelViewSet):
if admin_user_id:
admin_user = get_object_or_404(AdminUser, id=admin_user_id)
queryset = queryset.filter(admin_user=admin_user)
if node_id:
if node_id and show_current_asset:
queryset = queryset.filter(
Q(nodes=node_id) | Q(nodes__isnull=True)
).distinct()
if node_id and not show_current_asset:
node = get_object_or_404(Node, id=node_id)
if not node.is_root():
queryset = queryset.filter(
Q(nodes__key__regex='^{}(:[0-9]+)*$'.format(node.key)) |
Q(nodes__isnull=True),
).distinct()
else:
queryset = queryset.filter(
nodes__key__regex='^{}(:[0-9]+)*$'.format(node.key),
).distinct()
if show_current_asset and node_id:
queryset = queryset.filter(nodes=node_id).distinct()
return queryset

4
apps/assets/models/asset.py

@ -4,6 +4,7 @@
import uuid
import logging
import random
from django.db import models
from django.utils.translation import ugettext_lazy as _
@ -191,7 +192,8 @@ class Asset(models.Model):
seed()
for i in range(count):
asset = cls(ip='%s.%s.%s.%s' % (i, i, i, i),
ip = [str(i) for i in random.sample(range(255), 4)]
asset = cls(ip='.'.join(ip),
hostname=forgery_py.internet.user_name(True),
admin_user=choice(AdminUser.objects.all()),
port=22,

8
apps/assets/models/node.py

@ -108,14 +108,6 @@ class Node(models.Model):
assets = Asset.objects.filter(nodes__in=nodes).distinct()
return assets
def get_current_assets(self):
from .asset import Asset
assets = Asset.objects.filter(nodes=self).distinct()
return assets
def has_assets(self):
return self.get_all_assets()
def get_all_valid_assets(self):
return self.get_all_assets().valid()

2
apps/assets/serializers/node.py

@ -80,7 +80,7 @@ class NodeSerializer(serializers.ModelSerializer):
class NodeCurrentSerializer(NodeSerializer):
@staticmethod
def get_assets_amount(obj):
return obj.get_current_assets().count()
return obj.get_assets().count()
class NodeAssetsSerializer(serializers.ModelSerializer):

2
apps/assets/templates/assets/_asset_list_modal.html

@ -59,7 +59,7 @@ var zTree2, asset_table2 = 0;
function initTable2() {
var options = {
ele: $('#asset_list_modal_table'),
ajax_url: '{% url "api-assets:asset-list" %}',
ajax_url: '{% url "api-assets:asset-list" %}?show_current_asset=1',
columns: [
{data: "id"}, {data: "hostname" }, {data: "ip" }
],

Loading…
Cancel
Save