"""Search indexes serializers.""" from rest_framework import serializers from django_elasticsearch_dsl_drf.serializers import DocumentSerializer from news.serializers import NewsTypeSerializer from search_indexes.documents import EstablishmentDocument, NewsDocument from search_indexes.utils import get_translated_value class TagsDocumentSerializer(serializers.Serializer): """Tags serializer for ES Document.""" id = serializers.IntegerField() label_translated = serializers.SerializerMethodField() def get_label_translated(self, obj): return get_translated_value(obj.label) class AddressDocumentSerializer(serializers.Serializer): """Address serializer for ES Document.""" id = serializers.IntegerField() street_name_1 = serializers.CharField() street_name_2 = serializers.CharField() number = serializers.IntegerField() postal_code = serializers.CharField() latitude = serializers.FloatField(allow_null=True, source='coordinates.lat') longitude = serializers.FloatField(allow_null=True, source='coordinates.lon') geo_lon = serializers.FloatField(allow_null=True, source='coordinates.lon') geo_lat = serializers.FloatField(allow_null=True, source='coordinates.lat') class NewsDocumentSerializer(DocumentSerializer): """News document serializer.""" title_translated = serializers.SerializerMethodField(allow_null=True) subtitle_translated = serializers.SerializerMethodField(allow_null=True) news_type = NewsTypeSerializer() tags = TagsDocumentSerializer(many=True) class Meta: """Meta class.""" document = NewsDocument fields = ( 'id', 'title_translated', 'subtitle_translated', 'is_highlighted', 'image_url', 'preview_image_url', 'news_type', 'tags', 'slug', ) @staticmethod def get_title_translated(obj): return get_translated_value(obj.title) @staticmethod def get_subtitle_translated(obj): return get_translated_value(obj.subtitle) class EstablishmentDocumentSerializer(DocumentSerializer): """Establishment document serializer.""" address = AddressDocumentSerializer() tags = TagsDocumentSerializer(many=True) class Meta: """Meta class.""" document = EstablishmentDocument fields = ( 'id', 'name', 'name_translated', 'price_level', 'toque_number', 'public_mark', 'slug', 'preview_image', 'address', 'tags', # 'collections', # 'establishment_type', # 'establishment_subtypes', ) # def to_representation(self, instance): # ret = super().to_representation(instance) # dict_merge = lambda a, b: a.update(b) or a # # ret['tags'] = map(lambda tag: dict_merge(tag, {'label_translated': get_translated_value(tag.pop('label'))}), # ret['tags']) # ret['establishment_subtypes'] = map( # lambda subtype: dict_merge(subtype, {'name_translated': get_translated_value(subtype.pop('name'))}), # ret['establishment_subtypes']) # if ret.get('establishment_type'): # ret['establishment_type']['name_translated'] = get_translated_value(ret['establishment_type'].pop('name')) # if ret.get('address'): # ret['address']['city']['country']['name_translated'] = get_translated_value( # ret['address']['city']['country'].pop('name')) # location = ret['address'].pop('location') # if location: # ret['address']['geo_lon'] = location['lon'] # ret['address']['geo_lat'] = location['lat'] # # ret['type'] = ret.pop('establishment_type') # ret['subtypes'] = ret.pop('establishment_subtypes') # # return ret