jumpserver/apps/assets/serializers.py

71 lines
2.2 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-10-22 12:21:36 +00:00
from common.mixins import BulkDeleteApiMixin
from rest_framework_bulk import BulkListSerializer, BulkSerializerMixin
2016-11-01 09:21:16 +00:00
2016-10-22 12:21:36 +00:00
class AssetBulkUpdateSerializer(BulkSerializerMixin, serializers.ModelSerializer):
# group_display = serializers.SerializerMethodField()
# active_display = serializers.SerializerMethodField()
#groups = serializers.PrimaryKeyRelatedField(many=True, queryset=AssetGroup.objects.all())
class Meta(object):
model = Asset
list_serializer_class = BulkListSerializer
2016-11-06 13:29:04 +00:00
fields = ('id', 'port', 'idc')
2016-10-22 12:21:36 +00:00
# def get_group_display(self, obj):
# return " ".join([group.name for group in obj.groups.all()])
#
# def get_active_display(self, obj):
# # TODO: user ative state
2016-11-06 13:29:04 +00:00
# return not (obj.is_expired and obj.is_active)
class AssetGroupSerializer(serializers.ModelSerializer):
class Meta:
model = AssetGroup
class AssetSerializer(serializers.ModelSerializer):
class Meta:
model = Asset
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-06 14:45:26 +00:00
def get_field_names(self, declared_fields, info):
fields = super(SystemUserSerializer, self).get_field_names(declared_fields, info)
fields.append('assets_amount')
return fields
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