Commit 482d1bb2 authored by ibuler's avatar ibuler

[Update] 修改permisstion util

parent e7c7c3a7
...@@ -140,7 +140,7 @@ class NodeChildrenApi(mixins.ListModelMixin, generics.CreateAPIView): ...@@ -140,7 +140,7 @@ class NodeChildrenApi(mixins.ListModelMixin, generics.CreateAPIView):
node_fake = Node() node_fake = Node()
node_fake.id = asset.id node_fake.id = asset.id
node_fake.is_node = False node_fake.is_node = False
node_fake.parent = node node_fake.parent_id = node.id
node_fake.value = asset.hostname node_fake.value = asset.hostname
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)
......
...@@ -5,7 +5,7 @@ import uuid ...@@ -5,7 +5,7 @@ import uuid
from django.db import models, transaction from django.db import models, transaction
from django.db.models import Q from django.db.models import Q
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
from common.utils import with_cache
__all__ = ['Node'] __all__ = ['Node']
...@@ -166,6 +166,6 @@ class Node(models.Model): ...@@ -166,6 +166,6 @@ class Node(models.Model):
obj, created = cls.objects.get_or_create( obj, created = cls.objects.get_or_create(
key='0', defaults={"key": '0', 'value': "ROOT"} key='0', defaults={"key": '0', 'value': "ROOT"}
) )
print(obj)
return obj return obj
...@@ -64,14 +64,11 @@ class NodeSerializer(serializers.ModelSerializer): ...@@ -64,14 +64,11 @@ class NodeSerializer(serializers.ModelSerializer):
@staticmethod @staticmethod
def get_parent(obj): def get_parent(obj):
return obj.parent.id return obj.parent.id if obj.is_node else obj.parent_id
@staticmethod @staticmethod
def get_assets_amount(obj): def get_assets_amount(obj):
if obj.is_node: return obj.get_all_assets().count() if obj.is_node else 0
return obj.get_all_assets().count()
else:
return 0
def get_fields(self): def get_fields(self):
fields = super().get_fields() fields = super().get_fields()
......
...@@ -16,6 +16,7 @@ import calendar ...@@ -16,6 +16,7 @@ import calendar
import threading import threading
from io import StringIO from io import StringIO
import uuid import uuid
from functools import wraps
import paramiko import paramiko
import sshpubkeys import sshpubkeys
...@@ -395,3 +396,17 @@ class TeeObj: ...@@ -395,3 +396,17 @@ class TeeObj:
def close(self): def close(self):
self.file_obj.close() self.file_obj.close()
def with_cache(func):
cache = {}
key = "_{}.{}".format(func.__module__, func.__name__)
@wraps(func)
def wrapper(*args, **kwargs):
cached = cache.get(key)
if cached:
return cached
res = func(*args, **kwargs)
cache[key] = res
return res
return wrapper
...@@ -104,6 +104,11 @@ class AssetPermissionUtil: ...@@ -104,6 +104,11 @@ class AssetPermissionUtil:
return assets return assets
def get_nodes_with_assets(self): def get_nodes_with_assets(self):
"""
返回节点并且包含资产
{"node": {"assets": set("system_user")}}
:return:
"""
assets = self.get_assets() assets = self.get_assets()
nodes = defaultdict(dict) nodes = defaultdict(dict)
for asset, system_users in assets.items(): for asset, system_users in assets.items():
...@@ -112,7 +117,7 @@ class AssetPermissionUtil: ...@@ -112,7 +117,7 @@ class AssetPermissionUtil:
if asset in nodes[node]: if asset in nodes[node]:
nodes[node][asset].update(system_users) nodes[node][asset].update(system_users)
else: else:
nodes[node] = {asset: system_users} nodes[node][asset] = system_users
return nodes return nodes
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment