jumpserver/apps/assets/serializers.py

100 lines
3.1 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 rest_framework import viewsets, serializers,generics
2016-11-06 13:29:04 +00:00
from .models import AssetGroup, Asset, IDC, AssetExtend, 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
2016-11-06 13:29:04 +00:00
class AssetGroupSerializer(serializers.ModelSerializer):
2016-11-10 14:06:23 +00:00
assets_amount = serializers.SerializerMethodField()
2016-11-15 11:33:04 +00:00
# assets = serializers.PrimaryKeyRelatedField(many=True, read_only=True)
2016-11-10 14:06:23 +00:00
2016-11-06 13:29:04 +00:00
class Meta:
model = AssetGroup
2016-11-10 14:06:23 +00:00
@staticmethod
def get_assets_amount(obj):
return obj.assets.count()
2016-11-06 13:29:04 +00:00
class AdminUserSerializer(serializers.ModelSerializer):
class Meta:
model = AdminUser
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
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()
type_display = serializers.SerializerMethodField()
2016-11-10 14:06:23 +00:00
class Meta(object):
model = Asset
list_serializer_class = BulkListSerializer
2016-11-17 11:28:45 +00:00
@staticmethod
def get_hardware(obj):
return '%s %s %s' % (obj.cpu, obj.memory, obj.disk)
@staticmethod
def get_type_display(obj):
if obj.type:
return obj.type.value
else:
return ''
2016-11-10 14:06:23 +00:00
class AssetGrantedSerializer(serializers.ModelSerializer):
system_users = SystemUserSerializer(many=True, read_only=True)
is_inherited = serializers.SerializerMethodField()
system_users_join = serializers.SerializerMethodField()
class Meta(object):
model = Asset
fields = ("id", "hostname", "ip", "port", "system_users", "is_inherited",
"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):
return ', '.join([system_user.username for system_user in obj.system_users.all()])
2016-11-06 13:29:04 +00:00
class IDCSerializer(serializers.ModelSerializer):
assets_amount = serializers.SerializerMethodField()
class Meta:
model = IDC
@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