jumpserver/apps/assets/serializers.py

190 lines
6.0 KiB
Python
Raw Normal View History

2016-10-22 12:21:36 +00:00
# -*- coding: utf-8 -*-
from django.utils.translation import ugettext_lazy as _
from django.core.cache import cache
2017-03-15 16:19:47 +00:00
from rest_framework import viewsets, serializers, generics
2017-03-24 06:48:18 +00:00
from .models import AssetGroup, Asset, IDC, AdminUser, SystemUser
2016-11-25 03:00:51 +00:00
from common.mixins import IDInFilterMixin
2016-10-22 12:21:36 +00:00
from rest_framework_bulk import BulkListSerializer, BulkSerializerMixin
2016-11-01 09:21:16 +00:00
2017-01-06 12:34:24 +00:00
class AssetGroupSerializer(BulkSerializerMixin, serializers.ModelSerializer):
2016-11-10 14:06:23 +00:00
assets_amount = serializers.SerializerMethodField()
2017-01-07 14:34:12 +00:00
assets = serializers.PrimaryKeyRelatedField(many=True, queryset=Asset.objects.all())
2016-11-10 14:06:23 +00:00
2016-11-06 13:29:04 +00:00
class Meta:
model = AssetGroup
2017-01-07 14:34:12 +00:00
list_serializer_class = BulkListSerializer
fields = ['id', 'name', 'comment', 'assets_amount', 'assets']
2016-11-06 13:29:04 +00:00
2016-11-10 14:06:23 +00:00
@staticmethod
def get_assets_amount(obj):
return obj.assets.count()
2016-12-29 11:17:00 +00:00
2016-12-15 11:55:15 +00:00
class AssetUpdateGroupSerializer(serializers.ModelSerializer):
groups = serializers.PrimaryKeyRelatedField(many=True, queryset=AssetGroup.objects.all())
class Meta:
model = Asset
fields = ['id', 'groups']
2016-12-29 11:17:00 +00:00
2016-12-15 11:55:15 +00:00
class AssetUpdateSystemUserSerializer(serializers.ModelSerializer):
system_users = serializers.PrimaryKeyRelatedField(many=True, queryset=SystemUser.objects.all())
class Meta:
model = Asset
fields = ['id', 'system_users']
2016-11-06 13:29:04 +00:00
2016-12-29 11:17:00 +00:00
2017-01-06 12:34:24 +00:00
class AssetGroupUpdateSerializer(serializers.ModelSerializer):
2017-01-07 14:34:12 +00:00
"""update the asset group, and add or delete the asset to the group"""
assets = serializers.PrimaryKeyRelatedField(many=True, queryset=Asset.objects.all())
class Meta:
model = AssetGroup
fields = ['id', 'assets']
2017-01-06 12:34:24 +00:00
class AssetGroupUpdateSystemUserSerializer(serializers.ModelSerializer):
2017-01-07 14:34:12 +00:00
system_users = serializers.PrimaryKeyRelatedField(many=True, queryset=SystemUser.objects.all())
class Meta:
model = AssetGroup
fields = ['id', 'system_users']
2017-01-06 12:34:24 +00:00
class IDCUpdateAssetsSerializer(serializers.ModelSerializer):
2017-01-07 14:34:12 +00:00
assets = serializers.PrimaryKeyRelatedField(many=True, queryset=Asset.objects.all())
class Meta:
model = IDC
fields = ['id', 'assets']
2017-01-06 12:34:24 +00:00
2016-11-06 13:29:04 +00:00
class AdminUserSerializer(serializers.ModelSerializer):
2017-01-07 14:34:12 +00:00
assets = serializers.PrimaryKeyRelatedField(many=True, queryset=Asset.objects.all())
2016-11-06 13:29:04 +00:00
class Meta:
model = AdminUser
fields = '__all__'
2016-11-06 13:29:04 +00:00
2016-11-06 14:45:26 +00:00
def get_field_names(self, declared_fields, info):
fields = super(AdminUserSerializer, self).get_field_names(declared_fields, info)
fields.append('assets_amount')
return fields
2016-11-06 13:29:04 +00:00
class SystemUserSerializer(serializers.ModelSerializer):
class Meta:
model = SystemUser
2016-11-10 14:06:23 +00:00
exclude = ('_password', '_private_key', '_public_key')
2016-11-06 13:29:04 +00:00
2016-11-06 14:45:26 +00:00
def get_field_names(self, declared_fields, info):
fields = super(SystemUserSerializer, self).get_field_names(declared_fields, info)
2016-11-18 04:00:23 +00:00
fields.extend(['assets_amount'])
2016-11-06 14:45:26 +00:00
return fields
2016-11-06 13:29:04 +00:00
2017-01-17 08:34:47 +00:00
class AssetSystemUserSerializer(serializers.ModelSerializer):
class Meta:
model = SystemUser
fields = ('id', 'name', 'username', 'protocol', 'auth_method', 'comment')
2017-01-06 12:34:24 +00:00
class SystemUserUpdateAssetsSerializer(serializers.ModelSerializer):
2017-01-07 14:34:12 +00:00
assets = serializers.PrimaryKeyRelatedField(many=True, queryset=Asset.objects.all())
class Meta:
model = SystemUser
fields = ['id', 'assets']
2017-01-06 12:34:24 +00:00
class SystemUserUpdateAssetGroupSerializer(serializers.ModelSerializer):
2017-01-07 14:34:12 +00:00
asset_groups = serializers.PrimaryKeyRelatedField(many=True, queryset=AssetGroup.objects.all())
class Meta:
model = SystemUser
fields = ['id', 'asset_groups']
2016-12-29 11:17:00 +00:00
class SystemUserSimpleSerializer(serializers.ModelSerializer):
class Meta:
model = SystemUser
fields = ('id', 'name', 'username')
2016-11-10 14:06:23 +00:00
class AssetSerializer(BulkSerializerMixin, serializers.ModelSerializer):
2016-11-17 11:28:45 +00:00
# system_users = SystemUserSerializer(many=True, read_only=True)
# admin_user = AdminUserSerializer(many=False, read_only=True)
hardware = serializers.SerializerMethodField()
is_online = serializers.SerializerMethodField()
2016-11-10 14:06:23 +00:00
class Meta(object):
model = Asset
list_serializer_class = BulkListSerializer
fields = '__all__'
2016-11-10 14:06:23 +00:00
2016-11-17 11:28:45 +00:00
@staticmethod
def get_hardware(obj):
2017-03-15 16:19:47 +00:00
if obj.cpu_count:
return '{} Core {} {}'.format(obj.cpu_count*obj.cpu_cores, obj.memory, obj.disk_total)
2016-11-17 11:28:45 +00:00
else:
return ''
@staticmethod
def get_is_online(obj):
hostname = obj.hostname
if cache.get(hostname) == '1':
return True
elif cache.get(hostname) == '0':
return False
else:
return 'Unknown'
2016-11-27 06:37:50 +00:00
def get_field_names(self, declared_fields, info):
fields = super(AssetSerializer, self).get_field_names(declared_fields, info)
fields.extend(['get_type_display', 'get_env_display'])
return fields
2016-11-10 14:06:23 +00:00
class AssetGrantedSerializer(serializers.ModelSerializer):
2017-01-17 08:34:47 +00:00
system_users_granted = AssetSystemUserSerializer(many=True, read_only=True)
2016-11-10 14:06:23 +00:00
is_inherited = serializers.SerializerMethodField()
system_users_join = serializers.SerializerMethodField()
class Meta(object):
model = Asset
2017-01-17 08:34:47 +00:00
fields = ("id", "hostname", "ip", "port", "system_users_granted", "is_inherited",
2016-11-10 14:06:23 +00:00
"is_active", "system_users_join", "comment")
@staticmethod
def get_is_inherited(obj):
if getattr(obj, 'inherited', ''):
return True
else:
return False
@staticmethod
def get_system_users_join(obj):
2017-01-17 08:34:47 +00:00
return ', '.join([system_user.username for system_user in obj.system_users_granted])
2016-11-10 14:06:23 +00:00
2017-01-06 12:34:24 +00:00
class IDCSerializer(BulkSerializerMixin, serializers.ModelSerializer):
2016-11-06 13:29:04 +00:00
assets_amount = serializers.SerializerMethodField()
2017-01-07 14:34:12 +00:00
assets = serializers.PrimaryKeyRelatedField(many=True, queryset=Asset.objects.all())
2016-11-06 13:29:04 +00:00
class Meta:
model = IDC
fields = '__all__'
2016-11-06 13:29:04 +00:00
@staticmethod
def get_assets_amount(obj):
return obj.assets.count()
def get_field_names(self, declared_fields, info):
fields = super(IDCSerializer, self).get_field_names(declared_fields, info)
fields.append('assets_amount')
return fields