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-08-21 12:27:21 +00:00
|
|
|
from orgs.mixins.serializers 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
|
2019-07-11 10:44:16 +00:00
|
|
|
fields = [
|
|
|
|
'id', 'name', 'value', 'category', 'is_active', 'comment',
|
|
|
|
'date_created', 'asset_count', 'assets', 'get_category_display'
|
|
|
|
]
|
|
|
|
read_only_fields = (
|
|
|
|
'category', 'date_created', 'asset_count', 'get_category_display'
|
|
|
|
)
|
2020-06-12 03:45:03 +00:00
|
|
|
extra_kwargs = {
|
|
|
|
'assets': {'required': False}
|
|
|
|
}
|
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])
|