96 lines
3.2 KiB
Python
96 lines
3.2 KiB
Python
"""Search indexes serializers."""
|
|
from rest_framework import serializers
|
|
from django_elasticsearch_dsl_drf.serializers import DocumentSerializer
|
|
from search_indexes.documents import EstablishmentDocument, NewsDocument
|
|
from search_indexes.utils import get_translated_value
|
|
|
|
|
|
class NewsDocumentSerializer(DocumentSerializer):
|
|
"""News document serializer."""
|
|
|
|
title_translated = serializers.SerializerMethodField(allow_null=True)
|
|
subtitle_translated = serializers.SerializerMethodField(allow_null=True)
|
|
description_translated = serializers.SerializerMethodField(allow_null=True)
|
|
|
|
class Meta:
|
|
"""Meta class."""
|
|
|
|
document = NewsDocument
|
|
fields = (
|
|
'id',
|
|
'title',
|
|
'subtitle',
|
|
'description',
|
|
'web_url',
|
|
'title_translated',
|
|
'subtitle_translated',
|
|
'description_translated',
|
|
)
|
|
|
|
@staticmethod
|
|
def get_title_translated(obj):
|
|
return get_translated_value(obj.title)
|
|
|
|
@staticmethod
|
|
def get_subtitle_translated(obj):
|
|
return get_translated_value(obj.subtitle)
|
|
|
|
@staticmethod
|
|
def get_description_translated(obj):
|
|
return get_translated_value(obj.description)
|
|
|
|
|
|
# todo: country_name_translated
|
|
class EstablishmentDocumentSerializer(DocumentSerializer):
|
|
"""Establishment document serializer."""
|
|
|
|
description_translated = serializers.SerializerMethodField(allow_null=True)
|
|
|
|
preview_image = serializers.URLField(source='preview_image_url')
|
|
class Meta:
|
|
"""Meta class."""
|
|
|
|
document = EstablishmentDocument
|
|
fields = (
|
|
'id',
|
|
'name',
|
|
'public_mark',
|
|
'toque_number',
|
|
'price_level',
|
|
'description_translated',
|
|
'tags',
|
|
'address',
|
|
'collections',
|
|
'establishment_type',
|
|
'establishment_subtypes',
|
|
'preview_image',
|
|
'slug',
|
|
)
|
|
|
|
@staticmethod
|
|
def get_description_translated(obj):
|
|
return get_translated_value(obj.description)
|
|
|
|
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 |