2018-02-07 15:25:15 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
from rest_framework import serializers
|
2019-04-25 02:11:50 +00:00
|
|
|
|
|
|
|
from common.serializers import AdaptedBulkListSerializer
|
2019-06-19 08:45:14 +00:00
|
|
|
from orgs.mixins import BulkOrgResourceModelSerializer
|
2018-02-07 15:25:15 +00:00
|
|
|
|
|
|
|
from ..models import Label
|
|
|
|
|
|
|
|
|
2019-06-19 08:45:14 +00:00
|
|
|
class LabelSerializer(BulkOrgResourceModelSerializer):
|
2018-02-07 15:25:15 +00:00
|
|
|
asset_count = serializers.SerializerMethodField()
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Label
|
|
|
|
fields = '__all__'
|
2019-04-25 02:11:50 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
|
2019-06-19 08:45:14 +00:00
|
|
|
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])
|