31 lines
876 B
Python
31 lines
876 B
Python
"""Search indexes serializers."""
|
|
from rest_framework import serializers
|
|
from django_elasticsearch_dsl_drf.serializers import DocumentSerializer
|
|
from search_indexes.documents.news import NewsDocument
|
|
from utils.models import get_current_language
|
|
|
|
|
|
class NewsDocumentSerializer(DocumentSerializer):
|
|
"""News document serializer."""
|
|
|
|
title_translated = serializers.SerializerMethodField(allow_null=True)
|
|
|
|
class Meta:
|
|
"""Meta class."""
|
|
|
|
document = NewsDocument
|
|
fields = (
|
|
'id',
|
|
'title',
|
|
'subtitle',
|
|
'description',
|
|
'web_url',
|
|
'title_translated',
|
|
)
|
|
|
|
def get_title_translated(self, obj):
|
|
title_dict = obj.title
|
|
if not isinstance(title_dict, dict):
|
|
title_dict = obj.title.to_dict()
|
|
return title_dict.get(get_current_language())
|