jumpserver/apps/assets/serializers/label.py

40 lines
1.0 KiB
Python
Raw Normal View History

2018-02-07 15:25:15 +00:00
# -*- coding: utf-8 -*-
#
from rest_framework import serializers
from common.serializers import AdaptedBulkListSerializer
from orgs.mixins import BulkOrgResourceModelSerializer
2018-02-07 15:25:15 +00:00
from ..models import Label
class LabelSerializer(BulkOrgResourceModelSerializer):
2018-02-07 15:25:15 +00:00
asset_count = serializers.SerializerMethodField()
class Meta:
model = Label
fields = '__all__'
list_serializer_class = AdaptedBulkListSerializer
2018-02-07 15:25:15 +00:00
@staticmethod
def get_asset_count(obj):
return obj.assets.count()
def get_field_names(self, declared_fields, info):
fields = super().get_field_names(declared_fields, info)
fields.extend(['get_category_display'])
return fields
class LabelDistinctSerializer(BulkOrgResourceModelSerializer):
2018-02-07 15:25:15 +00:00
value = serializers.SerializerMethodField()
class Meta:
model = Label
fields = ("name", "value")
@staticmethod
def get_value(obj):
labels = Label.objects.filter(name=obj["name"])
return ', '.join([label.value for label in labels])