2018-02-07 15:25:15 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
from django.core.cache import cache
|
|
|
|
from rest_framework import serializers
|
2018-02-09 07:24:44 +00:00
|
|
|
from ..models import Node, AdminUser
|
2018-02-07 15:25:15 +00:00
|
|
|
from ..const import ADMIN_USER_CONN_CACHE_KEY
|
|
|
|
|
|
|
|
|
|
|
|
class AdminUserSerializer(serializers.ModelSerializer):
|
|
|
|
"""
|
|
|
|
管理用户
|
|
|
|
"""
|
|
|
|
assets_amount = serializers.SerializerMethodField()
|
|
|
|
unreachable_amount = serializers.SerializerMethodField()
|
|
|
|
reachable_amount = serializers.SerializerMethodField()
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = AdminUser
|
|
|
|
fields = '__all__'
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_unreachable_amount(obj):
|
|
|
|
data = cache.get(ADMIN_USER_CONN_CACHE_KEY.format(obj.name))
|
|
|
|
if data:
|
|
|
|
return len(data.get('dark'))
|
|
|
|
else:
|
|
|
|
return 0
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_reachable_amount(obj):
|
|
|
|
data = cache.get(ADMIN_USER_CONN_CACHE_KEY.format(obj.name))
|
|
|
|
if data:
|
|
|
|
return len(data.get('contacted'))
|
|
|
|
else:
|
|
|
|
return 0
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_assets_amount(obj):
|
|
|
|
return obj.assets_amount
|
|
|
|
|
|
|
|
|
2018-02-09 07:24:44 +00:00
|
|
|
class ReplaceNodeAdminUserSerializer(serializers.ModelSerializer):
|
2018-02-07 15:25:15 +00:00
|
|
|
"""
|
|
|
|
管理用户更新关联到的集群
|
|
|
|
"""
|
2018-02-09 07:24:44 +00:00
|
|
|
nodes = serializers.PrimaryKeyRelatedField(
|
|
|
|
many=True, queryset=Node.objects.all()
|
2018-02-07 15:25:15 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = AdminUser
|
2018-02-09 07:24:44 +00:00
|
|
|
fields = ['id', 'nodes']
|